Skip to content

Commit da7be27

Browse files
graememorganError Prone Team
authored and
Error Prone Team
committed
Descend into VariableTrees when looking for variables to check.
I think this `return null`'d on the assumption that a `VariableTree` can't contain more variables to check... but of course it can, with anonymous classes etc. PiperOrigin-RevId: 590929955
1 parent affa37a commit da7be27

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/UnusedVariable.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -612,11 +612,16 @@ private VariableFinder(VisitorState state) {
612612

613613
@Override
614614
public Void visitVariable(VariableTree variableTree, Void unused) {
615+
handleVariable(variableTree);
616+
return super.visitVariable(variableTree, null);
617+
}
618+
619+
private void handleVariable(VariableTree variableTree) {
615620
if (exemptedByName(variableTree.getName())) {
616-
return null;
621+
return;
617622
}
618623
if (isSuppressed(variableTree, state)) {
619-
return null;
624+
return;
620625
}
621626
VarSymbol symbol = getSymbol(variableTree);
622627
var parent = getCurrentPath().getParentPath().getLeaf();
@@ -626,22 +631,22 @@ public Void visitVariable(VariableTree variableTree, Void unused) {
626631
unusedElements.put(symbol, getCurrentPath());
627632
usageSites.put(symbol, getCurrentPath());
628633
}
629-
return null;
634+
return;
630635
}
631636
if (symbol.getKind() == ElementKind.FIELD
632637
&& symbol.getSimpleName().contentEquals("CREATOR")
633638
&& isSubtype(symbol.type, PARCELABLE_CREATOR.get(state), state)) {
634-
return null;
639+
return;
635640
}
636641
if (symbol.getKind() == ElementKind.FIELD
637642
&& exemptedFieldBySuperType(getType(variableTree), state)) {
638-
return null;
643+
return;
639644
}
640645
super.visitVariable(variableTree, null);
641646
// Return if the element is exempted by an annotation.
642647
if (exemptedByAnnotation(variableTree.getModifiers().getAnnotations())
643648
|| shouldKeep(variableTree)) {
644-
return null;
649+
return;
645650
}
646651
switch (symbol.getKind()) {
647652
case FIELD:
@@ -658,14 +663,14 @@ && exemptedFieldBySuperType(getType(variableTree), state)) {
658663
case PARAMETER:
659664
// ignore the receiver parameter
660665
if (variableTree.getName().contentEquals("this")) {
661-
return null;
666+
return;
662667
}
663668
// Ignore if parameter is part of canonical record constructor; tree does not seem
664669
// to contain usage in that case, but parameter is always used implicitly
665670
// For compact canonical constructor parameters don't have record flag so need to
666671
// check constructor flags (`symbol.owner`) instead
667672
if (hasRecordFlag(symbol.owner)) {
668-
return null;
673+
return;
669674
}
670675
unusedElements.put(symbol, getCurrentPath());
671676
if (!isParameterSubjectToAnalysis(symbol)) {
@@ -675,7 +680,6 @@ && exemptedFieldBySuperType(getType(variableTree), state)) {
675680
default:
676681
break;
677682
}
678-
return null;
679683
}
680684

681685
private boolean exemptedFieldBySuperType(Type type, VisitorState state) {

core/src/test/java/com/google/errorprone/bugpatterns/UnusedVariableTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,6 +1616,25 @@ public void unusedFunctionalInterfaceParameter() {
16161616
.doTest();
16171617
}
16181618

1619+
@Test
1620+
public void unusedWithinAnotherVariableTree() {
1621+
helper
1622+
.addSourceLines(
1623+
"Test.java",
1624+
"import java.util.Collections;",
1625+
"import java.util.Comparator;",
1626+
"import java.util.List;",
1627+
"class Test {",
1628+
" public void test(List<Integer> xs) {",
1629+
" var unusedLocal = ",
1630+
" xs.stream().sorted(",
1631+
" // BUG: Diagnostic contains: 'b' is never read",
1632+
" (a, b) -> a > a ? 1 : 0);",
1633+
" }",
1634+
"}")
1635+
.doTest();
1636+
}
1637+
16191638
@Test
16201639
public void unusedFunctionalInterfaceParameter_noFix() {
16211640
refactoringHelper

0 commit comments

Comments
 (0)