Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,16 @@ private void f() {
}
}

record MyRecord(int a) {
private static final int XXX = 3; // Noncompliant
// ^^^

// Non-private fields should not be reported.
static final int YYY = 4;

private static final int USED = 5;

int sum() {
return a + USED;
}
}
Comment on lines +197 to +209
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 👍 / 👎

Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class UnusedPrivateFieldCheck extends IssuableSubscriptionVisitor {

@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.CLASS, Tree.Kind.METHOD, Tree.Kind.EXPRESSION_STATEMENT, Tree.Kind.IDENTIFIER);
return Arrays.asList(Tree.Kind.CLASS, Tree.Kind.RECORD, Tree.Kind.METHOD, Tree.Kind.EXPRESSION_STATEMENT, Tree.Kind.IDENTIFIER);
}

@Override
Expand Down Expand Up @@ -129,7 +129,7 @@ public void visitNode(Tree tree) {
case METHOD:
checkIfNativeMethod((MethodTree) tree);
break;
case CLASS:
case CLASS, RECORD:
classes.add((ClassTree) tree);
break;
case EXPRESSION_STATEMENT:
Expand Down
Loading