Skip to content

Commit

Permalink
Don't peer at the identifiers on the LHS of assignments in AlreadyChe…
Browse files Browse the repository at this point in the history
…cked.

You need really weird circumstances to trigger this. Note the `if (false)` in the test, which renders `a` effectively final despite it having an assignment.

PiperOrigin-RevId: 585087842
  • Loading branch information
graememorgan authored and Error Prone Team committed Nov 24, 2023
1 parent 83d2ed5 commit 5395af4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
import com.google.errorprone.bugpatterns.threadsafety.ConstantExpressions.ConstantExpression;
import com.google.errorprone.bugpatterns.threadsafety.ConstantExpressions.Truthiness;
import com.google.errorprone.matchers.Description;
import com.sun.source.tree.AssignmentTree;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ConditionalExpressionTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.IfTree;
import com.sun.source.tree.LambdaExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
Expand Down Expand Up @@ -78,11 +80,9 @@ private final class IfScanner extends SuppressibleTreePathScanner<Void, Void> {

private final Multiset<ConstantExpression> truths = HashMultiset.create();
private final Multiset<ConstantExpression> falsehoods = HashMultiset.create();
private final VisitorState state;

private IfScanner(VisitorState state) {
super(state);
this.state = state;
}

@Override
Expand Down Expand Up @@ -210,6 +210,13 @@ public Void scan(Tree tree, Void unused) {
|| !isSameType(getType(tree), state.getSymtab().booleanType, state)) {
return super.scan(tree, null);
}

if (tree instanceof IdentifierTree
&& getCurrentPath().getLeaf() instanceof AssignmentTree
&& ((AssignmentTree) getCurrentPath().getLeaf()).getVariable().equals(tree)) {
return super.scan(tree, null);
}

constantExpressions
.constantExpression((ExpressionTree) tree, state)
.ifPresent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,22 @@ public void i3914() {
"}")
.doTest();
}

@Test
public void b312047923() {
helper
.addSourceLines(
"Test.java",
"class Test {",
" void test() {",
" boolean a = true;",
" loop: while (true) {",
" if (false && a) {",
" a = false;",
" } else {}",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit 5395af4

Please sign in to comment.