From a1b70a50a849b374bf590009c112265e09fb2525 Mon Sep 17 00:00:00 2001 From: Ilana Radinsky Date: Thu, 16 Jun 2022 14:50:23 -0400 Subject: [PATCH 1/3] Add test --- .../errorprone/StrictUnusedVariableTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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..2731a3648 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_constructorFields() { + compilationHelper + .addSourceLines( + "Test.java", + "class Test {", + " private final String _field;", + " public Test(String value) {", + " this._field = value;", + " }", + "}") + .doTest(); + } + @Test public void handles_enums() { compilationHelper From b33b96e83770f8f243d3e65ddaf9f313b61b4914 Mon Sep 17 00:00:00 2001 From: Ilana Radinsky Date: Thu, 16 Jun 2022 15:03:11 -0400 Subject: [PATCH 2/3] Don't count identifier expressions as variable usages --- .../palantir/baseline/errorprone/StrictUnusedVariable.java | 5 ++++- .../baseline/errorprone/StrictUnusedVariableTest.java | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) 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 2731a3648..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 @@ -81,7 +81,7 @@ public void handles_classes() { } @Test - public void handles_classes_constructorFields() { + public void handles_classes_ignoresIdentifierExpressions() { compilationHelper .addSourceLines( "Test.java", From 635526119d6e2898043d680e1168221695c2072f Mon Sep 17 00:00:00 2001 From: svc-changelog Date: Thu, 16 Jun 2022 19:12:24 +0000 Subject: [PATCH 3/3] Add generated changelog entries --- changelog/@unreleased/pr-2304.v2.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 changelog/@unreleased/pr-2304.v2.yml 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