Skip to content

Commit

Permalink
Rename "BooleanConstantChecker" to "ComplexBooleanConstant", and add
Browse files Browse the repository at this point in the history
more explanatory text.

RELNOTES: BooleanConstantChecker is renamed to ComplexBooleanConstant

MOE_MIGRATED_REVID=162748154
  • Loading branch information
nick-someone authored and cushon committed Aug 10, 2017
1 parent 99ecceb commit 3ad5c9c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
Expand Up @@ -28,12 +28,12 @@

/** @author Sumit Bhagwani (bhagwani@google.com) */
@BugPattern(
name = "BooleanConstantChecker",
summary = "Non-trivial compile time constant boolean expressions shouldn't be used",
name = "ComplexBooleanConstant",
summary = "Non-trivial compile time constant boolean expressions shouldn't be used.",
category = Category.JDK,
severity = SeverityLevel.ERROR
)
public class BooleanConstantChecker extends BugChecker implements BinaryTreeMatcher {
public class ComplexBooleanConstant extends BugChecker implements BinaryTreeMatcher {

@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
Expand Down
Expand Up @@ -32,7 +32,6 @@
import com.google.errorprone.bugpatterns.BadComparable;
import com.google.errorprone.bugpatterns.BadShiftAmount;
import com.google.errorprone.bugpatterns.BigDecimalLiteralDouble;
import com.google.errorprone.bugpatterns.BooleanConstantChecker;
import com.google.errorprone.bugpatterns.BoxedPrimitiveConstructor;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.CannotMockFinalClass;
Expand All @@ -46,6 +45,7 @@
import com.google.errorprone.bugpatterns.ComparisonContractViolated;
import com.google.errorprone.bugpatterns.ComparisonOutOfRange;
import com.google.errorprone.bugpatterns.CompileTimeConstantChecker;
import com.google.errorprone.bugpatterns.ComplexBooleanConstant;
import com.google.errorprone.bugpatterns.ConstantField;
import com.google.errorprone.bugpatterns.ConstantOverflow;
import com.google.errorprone.bugpatterns.ConstructorInvokesOverridable;
Expand Down Expand Up @@ -307,14 +307,14 @@ public static ScannerSupplier errorChecks() {
AsyncCallableReturnsNull.class,
AsyncFunctionReturnsNull.class,
BadShiftAmount.class,
BooleanConstantChecker.class,
BundleDeserializationCast.class,
ChainingConstructorIgnoresParameter.class,
CheckReturnValue.class,
CollectionIncompatibleType.class,
CompatibleWithMisuse.class,
ComparisonOutOfRange.class,
CompileTimeConstantChecker.class,
ComplexBooleanConstant.class,
ConstantOverflow.class,
DeadException.class,
DoNotCallChecker.class,
Expand Down
Expand Up @@ -23,17 +23,17 @@
import org.junit.runners.JUnit4;

/**
* Unit tests for {@link BooleanConstantChecker}.
* Unit tests for {@link ComplexBooleanConstantTest}.
*
* @author Sumit Bhagwani (bhagwani@google.com)
*/
@RunWith(JUnit4.class)
public class BooleanConstantCheckerTest {
private BugCheckerRefactoringTestHelper refactoringHelper =
BugCheckerRefactoringTestHelper.newInstance(new BooleanConstantChecker(), getClass());
public class ComplexBooleanConstantTest {
private final BugCheckerRefactoringTestHelper refactoringHelper =
BugCheckerRefactoringTestHelper.newInstance(new ComplexBooleanConstant(), getClass());

@Test
public void unusedTypeVariableRefactoringWhitespace() throws IOException {
public void refactorTest() throws IOException {
refactoringHelper
.addInputLines(
"in/Foo.java",
Expand Down
39 changes: 39 additions & 0 deletions docs/bugpattern/ComplexBooleanConstant.md
@@ -0,0 +1,39 @@
When a boolean expression is a compile-time constant (e.g.: `2 < 1`, `1 == 1`,
`'a' < 'A'`), these expressions can be directly replaced with `true` or `false`,
as appropriate. In any context where these expressions are used, `true` or
`false` is a more readable alternative:

```java
if (2 < 1) {
// Some code I don't want to run right now
}

while (1 == 1) {
// Some loop that I will manually break out of
}

assert 1 != 2; // I want to force an AssertionFailure if assertions are enabled
```

```java
if (false) {
// Some code I don't want to run right now
}

while (true) {
// Some loop that I will manually break out of
}

assert false; // I want to force an AssertionFailure if assertions are enabled
```

When some boolean expression is a compile-time constant unexpectedly, it
generally represents a bug in the code:

```java
for (int i = 0; i < 100; i++) {
System.out.println("Is " + i + " greater than 50?: " + (1 > 50));
}

// Prints "... true" 100 times, since i > 50 is mistyped as 1 > 50
```

0 comments on commit 3ad5c9c

Please sign in to comment.