diff --git a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StrictUnusedVariable.java b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StrictUnusedVariable.java index 25b34175b..503594529 100644 --- a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StrictUnusedVariable.java +++ b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StrictUnusedVariable.java @@ -91,6 +91,7 @@ import com.sun.source.tree.ReturnTree; import com.sun.source.tree.StatementTree; import com.sun.source.tree.Tree; +import com.sun.source.tree.Tree.Kind; import com.sun.source.tree.TryTree; import com.sun.source.tree.UnaryTree; import com.sun.source.tree.VariableTree; @@ -1054,7 +1055,9 @@ public Void visitIdentifier(IdentifierTree tree, Void unused) { @Override public Void visitMemberSelect(MemberSelectTree memberSelectTree, Void unused) { - usageSites.put(getSymbol(memberSelectTree), getCurrentPath()); + if (!memberSelectTree.getExpression().getKind().equals(Kind.IDENTIFIER)) { + usageSites.put(getSymbol(memberSelectTree), getCurrentPath()); + } return super.visitMemberSelect(memberSelectTree, null); } } diff --git a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StrictUnusedVariableTest.java b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StrictUnusedVariableTest.java index 251d91024..1a55c4d22 100644 --- a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StrictUnusedVariableTest.java +++ b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StrictUnusedVariableTest.java @@ -80,6 +80,20 @@ public void handles_classes() { .doTest(); } + @Test + public void handles_classes_ignoresIdentifierExpressions() { + compilationHelper + .addSourceLines( + "Test.java", + "class Test {", + " private final String _field;", + " public Test(String value) {", + " this._field = value;", + " }", + "}") + .doTest(); + } + @Test public void handles_enums() { compilationHelper diff --git a/changelog/@unreleased/pr-2304.v2.yml b/changelog/@unreleased/pr-2304.v2.yml new file mode 100644 index 000000000..b8741a561 --- /dev/null +++ b/changelog/@unreleased/pr-2304.v2.yml @@ -0,0 +1,6 @@ +type: fix +fix: + description: Don't count identifier expressions as variable usages in `StrictUnusedVariable` + check + links: + - https://github.com/palantir/gradle-baseline/pull/2304