Skip to content

fix(cpp): handle void return type in generated test harness#406

Merged
j178 merged 2 commits into
j178:masterfrom
diegohh0411:fix/cpp-void-return
Apr 4, 2026
Merged

fix(cpp): handle void return type in generated test harness#406
j178 merged 2 commits into
j178:masterfrom
diegohh0411:fix/cpp-void-return

Conversation

@diegohh0411

Copy link
Copy Markdown
Contributor

Summary

  • generateCallCode(): no longer emits auto res = obj->method() for void-returning methods — calls the method without assignment instead
  • generatePrintCode(): for void returns, prints the modified parameter via MetaData.Output.ParamIndex rather than res, or prints "null" when no output param is specified
  • Matches the existing behavior in the Python, Go, and Rust generators

Fixes #405

Test plan

  • go test ./lang/... -run "TestGenerateCallCode|TestGeneratePrintCode" -v — all 5 tests pass
  • Built patched binary and verified problem 283 harness compiles and passes both test cases
  • go test ./... — no regressions in full suite

🤖 Generated with Claude Code

diegohh0411 and others added 2 commits April 3, 2026 20:30
For problems where the solution returns void (in-place modification),
generateCallCode() now calls the method without assignment and
generatePrintCode() prints the modified parameter identified by
MetaData.Output.ParamIndex, matching the existing Python/Go/Rust behavior.

Fixes j178#405
Add TestGeneratePrintCode_VoidReturn_NoOutput to exercise the third
branch of the generatePrintCode conditional, where a void method has
no Output metadata and should print "null".

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@diegohh0411

Copy link
Copy Markdown
Contributor Author

Hey @j178 I've been using your tool for a bit and I love it! I use C++ mainly for Leetcode & I came across a bug while solving Leetcode problem no. 283. If you do leetgo pick 238, solve it and then test it locally via leetgo test 238 -L, you get this error:

$ leetgo pick 283
● reading leetcode cookies browser=firefox domain=leetcode.com
● generated file=cpp/0283.move-zeroes/solution.cpp
● generated file=cpp/0283.move-zeroes/testcases.txt
● generated file=cpp/0283.move-zeroes/question.md
● none editor is used, skip opening files
$ leetgo test 283 -L
● reading leetcode cookies browser=firefox domain=leetcode.com
● running test locally question=move-zeroes
● building file=cpp/0283.move-zeroes/solution.cpp
[redacted]/cpp/0283.move-zeroes/solution.cpp: In function ‘int main()’:
[redacted]/cpp/0283.move-zeroes/solution.cpp:32:14: error: deduced type ‘void’ for ‘res’ is incomplete
   32 |         auto res = obj->moveZeroes(nums);
      |              ^~~
× failed to run test locally err="compilation failed: exit status 1"

This happens because the repository still didn't account for Solutions that return void because they should modify a param in-place. All other languages (Python, Rust, etc...) did account for this scenario, except C++. And this PR fixes it.

I used Claude Code to solve the issue fast, but I still made the testing myself manually. So I'm confident this is quality code.

@j178 j178 merged commit 31e0c10 into j178:master Apr 4, 2026
2 checks passed
@j178

j178 commented Apr 4, 2026

Copy link
Copy Markdown
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C++ harness: compilation error for void return type methods

2 participants