Skip to content

Commit

Permalink
Add BugPattern to rewrite assertThat(...).isEqualTo(null) to `asser…
Browse files Browse the repository at this point in the history
…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
oxkitsune committed Jun 15, 2022
1 parent ce06396 commit 5ff3bad
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
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));
}
}
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();
}
}

0 comments on commit 5ff3bad

Please sign in to comment.