Skip to content

SONARJAVA-7307 Fix S1854 false positives on record compact constructor parameters - #6491

Merged
david-schroeter merged 4 commits into
masterfrom
ds/SONARJAVA-7307
Nov 6, 2026
Merged

david-schroeter merged 4 commits into
masterfrom
ds/SONARJAVA-7307

Conversation

@david-schroeter

@david-schroeter david-schroeter commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary by Gitar

  • Bug fix:
    • Fixed S1854 false positives on record compact constructor parameters in DeadStoreCheck
    • Compact constructors now add artificial reads of parameters to model implicit field assignment
  • Tests:
    • Added DeadStoreCheckSample records covering compact, canonical, conditional, loop, and nested cases
  • CFG:
    • Added CFG.build overload accepting explicit elements, used by MethodTreeImpl for compact constructors

This will update automatically on new commits.

@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

SONARJAVA-7307

@david-schroeter

Copy link
Copy Markdown
Contributor Author

/pvf

@hashicorp-vault-sonar-prod

Copy link
Copy Markdown
Contributor

📊 Performance Validation complete
🆕 0 new · 🗑️ 47 lost issues
View dashboard

@rombirli rombirli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +89 to +111
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();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this logic be in

@Nullable
@Override
public CFG cfg() {
if (block == null) {
return null;
}
if (cfg == null) {
cfg = CFG.build(this);
}
return cfg;
}
instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@gitar-bot

gitar-bot Bot commented Nov 6, 2026

Copy link
Copy Markdown
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 DeadStoreCheck, with new CFG handling in MethodTreeImpl and sample records covering compact, canonical, conditional, loop, and nested cases. No issues found.

Verification Rules 2/2 met PRs 1 inspected

Functional validation No results

Rules 2 passed 1 not applicable

✅ Internal artifact registries
✅ Repository license compliance

Broader impact PRs 1 inspected
Cross-PR 1 PR inspected 0 open impacts

PR 6485: SONARJAVA-7300 Decouple the Sonar Java frontend from Eclipse JDT core Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

sonarqube-next Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

@david-schroeter
david-schroeter merged commit c04d1dc into master Nov 6, 2026
18 checks passed
@david-schroeter
david-schroeter deleted the ds/SONARJAVA-7307 branch November 6, 2026 13:41
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.

2 participants