Skip to content

Commit

Permalink
Fix #1454: Matchers.booleanConstant(true) doesn't match
Browse files Browse the repository at this point in the history
Boolean.FALSE

Fixes #1455

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=286070502
  • Loading branch information
carterkozak authored and kluever committed Dec 18, 2019
1 parent e05fa7a commit 3ad1714
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,11 @@ public static Matcher<ExpressionTree> booleanConstant(boolean value) {
Symbol symbol = getSymbol(expressionTree);
if (symbol.isStatic()
&& state.getTypes().unboxedTypeOrType(symbol.type).getTag() == TypeTag.BOOLEAN) {
return ((value && symbol.getSimpleName().contentEquals("TRUE"))
|| symbol.getSimpleName().contentEquals("FALSE"));
if (value) {
return symbol.getSimpleName().contentEquals("TRUE");
} else {
return symbol.getSimpleName().contentEquals("FALSE");
}
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher;
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
import com.google.errorprone.matchers.method.MethodMatchers;
import com.google.errorprone.scanner.ErrorProneScanner;
import com.google.errorprone.scanner.ScannerSupplier;
import com.sun.source.tree.ClassTree;
Expand Down Expand Up @@ -402,6 +403,23 @@ public void sameArgumentGoesOutOfBounds() {
assertThat(thrown).hasMessageThat().contains("IndexOutOfBoundsException");
}

@Test
public void booleanConstantMatchesTrue() {
CompilationTestHelper.newInstance(BooleanConstantTrueChecker.class, getClass())
.addSourceLines(
"test/BooleanConstantTrueCheckerTest.java",
"package test;",
"public class BooleanConstantTrueCheckerTest {",
" public void function() {",
" // BUG: Diagnostic contains:",
" method(Boolean.TRUE);",
" method(Boolean.FALSE);",
" }",
" void method(Object value) {}",
"}")
.doTest();
}

@Test
public void packageNameChecker() {
CompilationTestHelper.newInstance(PackageNameChecker.class, getClass())
Expand Down Expand Up @@ -539,4 +557,25 @@ public Description matchClass(ClassTree tree, VisitorState state) {
return MATCHER.matches(tree, state) ? describeMatch(tree) : Description.NO_MATCH;
}
}

/** Checker that checks if an argument is 'Boolean.TRUE'. */
@BugPattern(
name = "BooleanConstantTrueChecker",
summary = "BooleanConstantTrueChecker",
severity = ERROR)
public static class BooleanConstantTrueChecker extends BugChecker
implements MethodInvocationTreeMatcher {

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (Matchers.methodInvocation(
MethodMatchers.anyMethod(),
ChildMultiMatcher.MatchType.AT_LEAST_ONE,
Matchers.booleanConstant(true))
.matches(tree, state)) {
return describeMatch(tree);
}
return Description.NO_MATCH;
}
}
}

0 comments on commit 3ad1714

Please sign in to comment.