Skip to content

SONARJAVA-6425 Support records in S1068#5644

Open
tomasz-tylenda-sonarsource wants to merge 3 commits into
masterfrom
tt/S1068-records
Open

SONARJAVA-6425 Support records in S1068#5644
tomasz-tylenda-sonarsource wants to merge 3 commits into
masterfrom
tt/S1068-records

Conversation

@tomasz-tylenda-sonarsource
Copy link
Copy Markdown
Contributor

@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource commented May 29, 2026


Summary by Gitar

  • Check logic:
    • Updated UnusedPrivateFieldCheck to include Tree.Kind.RECORD in nodesToVisit and switch case.
  • Test updates:
    • Added test case for record private field usage in UnusedPrivateFieldCheck.java test sources.

This will update automatically on new commits.

@hashicorp-vault-sonar-prod
Copy link
Copy Markdown
Contributor

hashicorp-vault-sonar-prod Bot commented May 29, 2026

SONARJAVA-6425

Comment on lines +197 to +200
record MyRecord(int a) {
private static final int XYZ = 3; // Noncompliant
// ^^^
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Edge Case: Test missing: record instance field false positive coverage

The test only covers private static final fields in records. Records can also have explicitly declared private instance fields (though rare). More importantly, record components generate implicit private final fields (e.g., int a in record MyRecord(int a)). It would strengthen confidence to add a test verifying the rule does NOT flag record component fields as unused, since they are accessed via the implicit accessor method. For example:

record MyRecordCompliant(int a) {
  // 'a' should not be flagged
  private static final int USED = 1;
  int sum() { return a + USED; }
}

This would guard against future regressions if the AST representation changes.

Was this helpful? React with 👍 / 👎

Copy link
Copy Markdown
Contributor

@asya-vorobeva asya-vorobeva left a comment

Choose a reason for hiding this comment

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

I think gitar-bot's comment makes a perfect sense. Let's add this test case: int sum() { return a + XXX; }

@tomasz-tylenda-sonarsource
Copy link
Copy Markdown
Contributor Author

I think gitar-bot's comment makes a perfect sense. Let's add this test case: int sum() { return a + XXX; }

Done.

The bot also talks about "explicitly declared private instance fields", but I don't think it is possible.

@sonarqube-next
Copy link
Copy Markdown

@gitar-bot
Copy link
Copy Markdown

gitar-bot Bot commented May 29, 2026

Code Review ✅ Approved 1 resolved / 1 findings

Updates UnusedPrivateFieldCheck to support Java records, resolving the false positive coverage issue for record instance fields. No additional findings detected.

✅ 1 resolved
Edge Case: Test missing: record instance field false positive coverage

📄 java-checks-test-sources/default/src/main/java/checks/unused/UnusedPrivateFieldCheck.java:197-200
The test only covers private static final fields in records. Records can also have explicitly declared private instance fields (though rare). More importantly, record components generate implicit private final fields (e.g., int a in record MyRecord(int a)). It would strengthen confidence to add a test verifying the rule does NOT flag record component fields as unused, since they are accessed via the implicit accessor method. For example:

record MyRecordCompliant(int a) {
  // 'a' should not be flagged
  private static final int USED = 1;
  int sum() { return a + USED; }
}

This would guard against future regressions if the AST representation changes.

Options

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

Comment with these commands to change:

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

Was this helpful? React with 👍 / 👎 | Gitar

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