Skip to content

Commit

Permalink
Account for java records when checking for unused variables
Browse files Browse the repository at this point in the history
resolves #2515, resolves #1648

PiperOrigin-RevId: 400246379
  • Loading branch information
codylerum authored and Error Prone Team committed Oct 1, 2021
1 parent d4f2198 commit 47da3af
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,15 @@ private boolean isFieldEligibleForChecking(VariableTree variableTree, VarSymbol
&& ASTHelpers.hasDirectAnnotationWithSimpleName(variableTree, "Inject")) {
return true;
}
if ((symbol.flags() & RECORD_FLAG) == RECORD_FLAG) {
return false;
}
return variableTree.getModifiers().getFlags().contains(Modifier.PRIVATE)
&& !SPECIAL_FIELDS.contains(symbol.getSimpleName().toString());
}

private static final long RECORD_FLAG = 1L << 61;

/** Returns whether {@code sym} can be removed without updating call sites in other files. */
private boolean isParameterSubjectToAnalysis(Symbol sym) {
checkArgument(sym.getKind() == ElementKind.PARAMETER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@

package com.google.errorprone.bugpatterns;

import static org.junit.Assume.assumeTrue;

import com.google.common.collect.ImmutableList;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers;
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.ErrorProneFlags;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -1353,4 +1356,45 @@ public void unusedReassignment_keepSideEffectsFix() {
.setFixChooser(FixChoosers.SECOND)
.doTest();
}

@Test
public void simpleRecord() {
assumeTrue(RuntimeVersion.isAtLeast16());
helper
.addSourceLines(
"SimpleRecord.java", //
"public record SimpleRecord (Integer foo, Long bar) {}")
.expectNoDiagnostics()
.doTest();
}

@Test
public void nestedRecord() {
assumeTrue(RuntimeVersion.isAtLeast16());
helper
.addSourceLines(
"SimpleClass.java",
"public class SimpleClass {",
" public record SimpleRecord (Integer foo, Long bar) {}",
"}")
.expectNoDiagnostics()
.doTest();
}

@Test
public void unusedInRecord() {
assumeTrue(RuntimeVersion.isAtLeast16());
helper
.addSourceLines(
"SimpleClass.java",
"public class SimpleClass {",
" public record SimpleRecord (Integer foo, Long bar) {",
" void f() {",
" // BUG: Diagnostic contains: is never read",
" int x = foo;",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit 47da3af

Please sign in to comment.