Repository navigation
SONARJAVA-7307 Fix S1854 false positives on record compact constructor parameters - #6491
Conversation
bb339d1 to
b6c67d8
Compare
|
/pvf |
|
📊 Performance Validation complete |
| private static CFG buildCFG(Tree tree, Symbol.MethodSymbol methodSymbol) { | ||
| if (tree instanceof MethodTree methodTree) { | ||
| // Compact record constructors have no parameter-list parentheses. | ||
| if (methodTree.is(Tree.Kind.CONSTRUCTOR) && methodTree.openParenToken() == null) { | ||
| List<Tree> elements = new ArrayList<>(methodTree.block().body()); | ||
| Set<Symbol> parameters = new HashSet<>(); | ||
| methodTree.block().accept(new BaseTreeVisitor() { | ||
| @Override | ||
| public void visitIdentifier(IdentifierTree identifier) { | ||
| Symbol symbol = identifier.symbol(); | ||
| if (symbol.isParameter() && methodSymbol.equals(symbol.owner()) && parameters.add(symbol)) { | ||
| // Add an artificial read to model the implicit assignment to the record field. | ||
| elements.add(identifier); | ||
| } | ||
| } | ||
| }); | ||
| return CFG.buildCFG(elements); | ||
| } | ||
| return (CFG) methodTree.cfg(); | ||
| } | ||
| return (CFG) ((LambdaExpressionTree) tree).cfg(); | ||
| } | ||
|
|
There was a problem hiding this comment.
Could this logic be in
instead?There was a problem hiding this comment.
Yes that's a good point it would simplify the rule implementation and make it more correct if the cfg is ever used for an other rule. Thanks for noticing it.
76eaa97 to
e8ad79c
Compare
Code Review ✅ Approved🟡 Medium risk · Adding implicit parameter reads to compact-constructor CFGs changes dead-store analysis and could suppress or introduce incorrect assignment diagnostics. Fixes S1854 false positives on record compact constructor parameters by modeling their implicit field reassignment in Verification Rules 2/2 met PRs 1 inspectedFunctional validation No results Rules 2 passed 1 not applicable
Broader impact PRs 1 inspectedCross-PR 1 PR inspected 0 open impacts
Auto-approval Not enabledFor more verification details, comment OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
|




Summary by Gitar
DeadStoreCheckDeadStoreCheckSamplerecords covering compact, canonical, conditional, loop, and nested casesCFG.buildoverload accepting explicit elements, used byMethodTreeImplfor compact constructorsThis will update automatically on new commits.