fix(cpp): silence unused-parameter warnings for empty objects#2982
Merged
Conversation
Empty JSON Schema objects generate C++ from_json/to_json functions whose j and/or x parameters go unreferenced, tripping -Wunused-parameter under -Werror. Emit (void)j;/(void)x; for classes with no properties. Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was broken
For a JSON Schema empty object (
{"type": "object", "additionalProperties": false}), quicktype's C++ target generatedfrom_json/to_jsonfree functions that don't reference all their parameters:from_jsonuses neitherjnorx;to_jsondoesn't usex. Compiling with-Wextra -Wunused-parameter -Werror(a common strict build configuration) turns these into hard errors, breaking consumer builds.Root cause
CPlusPlusRenderer.emitClassFunctions(inpackages/quicktype-core/src/language/CPlusPlus/CPlusPlusRenderer.ts) always names both parameters when emittingfrom_json/to_json, but for a class with zero properties nothing in the body ever touches them.Fix
In
emitClassFunctions, when a class has no properties, emit(void)j; (void)x;at the top offrom_json's body and(void)x;afterj = json::object();into_json's body — the standard idiom for silencing unused-parameter warnings while keeping the parameter names (needed to match the function signature) intact.Test coverage
The existing schema fixture
test/inputs/schema/constructor.schemaalready generates a nested empty class (additionalProperties: false, no properties) via aoneOfunion member. The C++ fixture's compile command intest/languages.tsnow passes-Werror=unused-parameter, so this fixture fails to compile on unfixed code (reproducing the reported warnings) and compiles cleanly after the fix.Verification
node dist/index.js --lang cpp --src-lang schema empty.schema.json -o out.hpp, then compiled the output withg++ -std=c++17 -Wextra -Wunused-parameter -Werror— reproduced all three reported errors (unusedjandxinfrom_json, unusedxinto_json) before the fix; no unused-parameter errors after.npm run buildpasses.schema-cplusplusfixture suite locally (QUICKTEST=true FIXTURE=schema-cplusplus script/test) to confirm no regressions across the many C++ renderer option combinations.Fixes #2362
🤖 Generated with Claude Code