-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BugPattern to rewrite
assertThat(...).isEqualTo(null)
to `asser…
…tThat(...).isNull()` Run google-java-format Add some XXXs Suggest fix in AsserThatIsNullCheck Add javadoc Make test class final Add typecheck for literal tree fmt Improve method invocation matching
- Loading branch information
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import static com.google.errorprone.BugPattern.LinkType.NONE; | ||
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION; | ||
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; | ||
import com.google.errorprone.fixes.SuggestedFixes; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.tools.javac.code.Symbol.MethodSymbol; | ||
import org.assertj.core.api.AbstractAssert; | ||
|
||
/** | ||
* A {@link BugChecker} which flags {@code asserThat(someValue).isEqualTo(null)} and suggests {@link | ||
* AbstractAssert#isNull()}. | ||
*/ | ||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "AssertThatIsNull", | ||
summary = "asserThat(...).isEqualTo(null) should be assertThat(...).isNull()", | ||
linkType = NONE, | ||
severity = SUGGESTION, | ||
tags = SIMPLIFICATION) | ||
public final class AssertThatIsNullCheck extends BugChecker implements MethodInvocationTreeMatcher { | ||
private static final long serialVersionUID = 1L; | ||
|
||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { | ||
MethodSymbol symbol = ASTHelpers.getSymbol(tree); | ||
if (symbol == null | ||
|| tree.getArguments().size() != 1 | ||
|| tree.getArguments().get(0).getKind() != Tree.Kind.NULL_LITERAL) { | ||
return Description.NO_MATCH; | ||
} | ||
|
||
return describeMatch(tree, SuggestedFixes.renameMethodInvocation(tree, "isNull()", state)); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...e-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import com.google.errorprone.BugCheckerRefactoringTestHelper; | ||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public final class AssertThatIsNullCheckTest { | ||
private final CompilationTestHelper compilationTestHelper = | ||
CompilationTestHelper.newInstance(AssertThatIsNullCheck.class, getClass()); | ||
|
||
private final BugCheckerRefactoringTestHelper refactoringTestHelper = | ||
BugCheckerRefactoringTestHelper.newInstance(AssertThatIsNullCheck.class, getClass()); | ||
|
||
// XXX: Methods always start with lowercase. I thought we had a Checkstyle thing for this. | ||
@Test | ||
void identification() { | ||
compilationTestHelper | ||
.addSourceLines( | ||
"A.java", | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"import com.google.common.collect.ImmutableSortedSet;", | ||
"import org.junit.jupiter.api.Test;", | ||
"public final class A {", | ||
" @Test", | ||
" public void testAssertThat() {", | ||
" String nullValue = null;", | ||
" assertThat(12).isEqualTo(12);", | ||
" // BUG: Diagnostic contains: assertThat(...).isNull()", | ||
" assertThat(\"value\").isEqualTo(null);", | ||
" // BUG: Diagnostic contains: assertThat(...).isNull()", | ||
" assertThat(nullValue).isEqualTo(null);", | ||
" }", | ||
"} ") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
void replacement() { | ||
refactoringTestHelper | ||
.addInputLines( | ||
"A.java", | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"import com.google.common.collect.ImmutableSortedSet;", | ||
"import org.junit.jupiter.api.Test;", | ||
"public final class A {", | ||
" @Test", | ||
" public void testAssertThat() {", | ||
" String nullValue = null;", | ||
" assertThat(12).isEqualTo(12);", | ||
" assertThat(\"value\").isEqualTo(null);", | ||
" assertThat(nullValue).isEqualTo(null);", | ||
" }", | ||
"} ") | ||
.addOutputLines( | ||
"A.java", | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"import com.google.common.collect.ImmutableSortedSet;", | ||
"import org.junit.jupiter.api.Test;", | ||
"public final class A {", | ||
" @Test", | ||
" public void testAssertThat() {", | ||
" String nullValue = null;", | ||
" assertThat(12).isEqualTo(12);", | ||
" assertThat(\"value\").isNull();", | ||
" assertThat(nullValue).isNull();", | ||
" }", | ||
"} ") | ||
.doTest(); | ||
} | ||
} |