Fix FormatString false positive for switch expression arguments#5967
Open
arimu1 wants to merge 2 commits into
Open
Fix FormatString false positive for switch expression arguments#5967arimu1 wants to merge 2 commits into
arimu1 wants to merge 2 commits into
Conversation
When a switch expression is passed directly to printf, javac types the expression as Object. Infer the result type from case bodies so format validation uses the real primitive or reference type. Fixes google#5831
arimu1
force-pushed
the
fix/5831-formatstring-switch-expression
branch
2 times, most recently
from
July 27, 2026 09:17
b1f9f7e to
4c4b5d7
Compare
Use getExpressionType for arrow-case bodies and the body fallback so nested switch expressions are typed correctly. Handle colon/yield cases via CaseTree.getStatements when getBody() is null. Add lean regression tests for nested switch, block/colon yield, string cases, and mixed types.
arimu1
force-pushed
the
fix/5831-formatstring-switch-expression
branch
from
July 27, 2026 09:18
4c4b5d7 to
2c64580
Compare
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.
Summary
FormatStringreported a spuriousillegal format conversionwhen anint-typed switch expression was passed directly toprintf("%d", ...)because javac types the whole expression asObject, so validation fell back to a generic placeholder instance.vartemporary form and the inlined switch expression.Fixes #5831
Test plan
mvn -pl core clean test -Dtest=FormatStringTest(50/50 pass, JDK 25 toolchain)mvn -pl core test -Dtest=LenientFormatStringValidationTest,FormatStringTestFormatStringValidation.javamakesswitchExpressionArgumentfail with the originalFormatStringValidation$…/%ddiagnosticHuman-reviewed AI assistance (Cursor implementator).
Made with Cursor