fix(#5845): strip parentheses before matching in 6 bug checks to eliminate false positives/negatives#5889
Open
HarshMehta112 wants to merge 1 commit into
Open
fix(#5845): strip parentheses before matching in 6 bug checks to eliminate false positives/negatives#5889HarshMehta112 wants to merge 1 commit into
HarshMehta112 wants to merge 1 commit into
Conversation
Signed-off-by: Harsh Mehta <harshmehta010102@gmail.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.
Fixes: #5845
Problem
Six unrelated checks share a root cause: their AST matchers inspect raw
ExpressionTreeoperands without unwrapping enclosing
ParenthesizedTreenodes. Per JLS §15.8.5, aparenthesized expression denotes the same variable as its contained expression —
parentheses are semantically inert. Because
ASTHelpers.getSymbol(ParenthesizedTree)returns
null(no case forPARENTHESIZEDin the dispatch), wrapping or unwrapping onepair of redundant parentheses around an operand silently flips the verdict.
LoopConditionCheckerfor (int i = 0; i < 10; (i)++) {}NonAtomicVolatileUpdate(x)++on a volatile fieldPatternMatchingInstanceofString s = (String)(o)DuplicateBranchestrue ? 1 * 5 : (1 * 5)InlineTrivialConstantprivate static final String EMPTY = ("")AssignmentExpressiona = b = 0vsa = (b = 0)verdict driftFix
Apply
ASTHelpers.stripParentheses()at the point where each check extracts the operandit will inspect.
LoopConditionCheckerUpdateScanner.check(): strip beforegetSymbol. One change covers all four call sites(
visitUnary,visitAssignment,visitCompoundAssignment,visitMethodInvocation).NonAtomicVolatileUpdateStrip in all three extractor lambdas (
expressionFromUnaryTree,variableFromCompoundAssignmentTree,variableFromAssignmentTree) before thehasModifier(VOLATILE)check.PatternMatchingInstanceofStrip
node.getExpression()infindAllCasts.visitTypeCastbefore theconstantExpressionsymbol lookup.DuplicateBranchesStrip both arms before
toString()comparison. Source positions for the suggested fixstill use the original (possibly-parenthesized) tree, so the replacement is always
syntactically valid.
InlineTrivialConstantStrip
tree.getInitializer()before theSTRING_LITERALkind check and valueextraction.
AssignmentExpressionWalk
getParentPath()in a loop until the leaf is no longerParenthesizedTree, thenuse the resulting
effectiveParentfor all three downstream decisions:(foo = bar) != null)a = a = foo())x = y = 0)Tests
32 new test cases across all six checkers:
+=,-=),simple assignment
fields, synchronized blocks, different branch content after stripping)
assignments, inline cast use, duplicate assignment through parentheses