-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add BugPattern to rewrite assertThat(...).isEqualTo(null)
to assertThat(...).isNull()
#133
Conversation
8741248
to
5ff3bad
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most important thing is that we don't flag only AssertJ usages of isEqualTo
, which should be the case 😄.
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullCheck.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullCheck.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullCheck.java
Outdated
Show resolved
Hide resolved
...rone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullCheckTest.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullCheck.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullCheck.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullCheck.java
Outdated
Show resolved
Hide resolved
I'm going to redo the Matcher as I learned some neat tricks to simplify this a lot! |
064606c
to
0c87b2b
Compare
07cf810
to
804bebd
Compare
804bebd
to
5bec7f6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rebased and added a commit. Suggested commit message:
Prefer AssertJ's `isNull()` assertion over `isEqualTo(null)` (#133)
...one-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullUsageCheck.java
Outdated
Show resolved
Hide resolved
...one-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullUsageCheck.java
Outdated
Show resolved
Hide resolved
...one-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullUsageCheck.java
Outdated
Show resolved
Hide resolved
...one-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullUsageCheck.java
Outdated
Show resolved
Hide resolved
...contrib/src/test/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullUsageCheckTest.java
Outdated
Show resolved
Hide resolved
...contrib/src/test/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullUsageCheckTest.java
Outdated
Show resolved
Hide resolved
...contrib/src/test/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullUsageCheckTest.java
Outdated
Show resolved
Hide resolved
...contrib/src/test/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullUsageCheckTest.java
Outdated
Show resolved
Hide resolved
...contrib/src/test/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullUsageCheckTest.java
Outdated
Show resolved
Hide resolved
...one-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/AssertThatIsNullUsageCheck.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/AssertJIsNullCheck.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, one open point: we should document why we can't use Refaster for this, e.g. with an example.
(Requesting changes so we don't forget.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all, there were like 6 comments from when I started reviewing before @Stephan202 made some changes, sorry about that. Can't delete them anymore 🤔.
Made a suggestion w.r.t. the explanation.
Overall, changes LGTM 😄.
|
||
/** | ||
* A {@link BugChecker} which flags {@code asserThat(someValue).isEqualTo(null)} for further | ||
* simplification |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* simplification | |
* simplification. |
Javadoc should end with a dot :).
" public void testAssertThat() {", | ||
" String nullValue = null;", | ||
" assertThat(12).isEqualTo(12);", | ||
" // BUG: Diagnostic contains: assertThat(...).isNull()", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
" // BUG: Diagnostic contains: assertThat(...).isNull()", | |
" // BUG: Diagnostic contains:", |
" isEqualTo(null);", | ||
" }", | ||
"", | ||
" private boolean isEqualTo (Object value) {", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure to follow the google-java-format
in the tests as well.
.addInputLines( | ||
"A.java", | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"import com.google.common.collect.ImmutableSortedSet;", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any usages of this 👀.
private static final long serialVersionUID = 1L; | ||
|
||
private static final Matcher<MethodInvocationTree> ASSERT_IS_EQUAL = | ||
Matchers.allOf( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Matcher
s we generally statically import. That reads a bit nicer :)
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/AssertJIsNullCheck.java
Show resolved
Hide resolved
import com.sun.source.tree.MethodInvocationTree; | ||
|
||
/** | ||
* A {@link BugChecker} which flags AssertJ {@code isEqualTo(null)} checks for simplification. <br> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first line of the Javadoc is a short summary. After that we have an empty line with optionally extra explanations.
Also <br>
is not used in the Javadoc (see the suggestion I'm about to push).
The google-java-format
plugin in IntelliJ automatically adds the <p>
in front of the text if there was an empty line between it and the summary.
/** | ||
* A {@link BugChecker} which flags AssertJ {@code isEqualTo(null)} checks for simplification. <br> | ||
* This cannot be done using a refaster template, as refaster is unable to match the abstraction | ||
* layers in AssertJ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* layers in AssertJ | |
* layers in AssertJ. |
Sentence should end with a dot.
|
||
/** | ||
* A {@link BugChecker} which flags AssertJ {@code isEqualTo(null)} checks for simplification. <br> | ||
* This cannot be done using a refaster template, as refaster is unable to match the abstraction |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* This cannot be done using a refaster template, as refaster is unable to match the abstraction | |
* This cannot be done using a Refaster template, as Refaster is unable to match the abstraction |
* This cannot be done using a refaster template, as refaster is unable to match the abstraction | ||
* layers in AssertJ | ||
* | ||
* <p>Example: <code>assertThat("foo").isEqualTo(null)</code> will not be matched by refaster. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although I completely understand the suggestion, some context is missing here. When trying to implement this using Refaster templates, the issue was a bit more specific. From the +- 10 before templates, only 5 or 6 matched. So a few (I don't remember which ones) didn't work for Refaster.
There is a good chance the proposed examples does work. That's why I proposed a slightly different test. Not sure if we should figure out which (sub)types didn't match to provide examples in the Javadoc. Perhaps this is good enough. WDYT?
…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
* <p>This cannot be done by using only Refaster templates, as Refaster is not able to match *all* | ||
* {@code AbstractAssert} subtypes correctly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This just raises questions for me. Do we have a concrete example that fails, so that I can identify the underlying cause (and then turn that into a concise explanation)? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, assertThat("foo").isEqualTo(null)
was mentioned in a preceding commit. Will check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The key point here is that there exist specialized overloads (not overrides) of isEqualTo
that we also wish to match. Added a commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure whether this suggestions_really_ describes problem. Adding all of these overloads to a Refaster template would not work. For some reason, Refaster fails to match some of the overloads. So I recall we had about 8 before templates but some specific overloads didn't do anything because Refaster couldn't correctly match all these types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would like to see such an example, then. OTOH, not sure it matters: the current description isn't wrong: even if Refaster would work it'd (a) be a lot of work and (b) not be future-proof.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH, I agree with your points. I don't think it is worth the hassle to get to the specific examples. Let's roll with the current state 🚀 .
172f4d4
to
dabe437
Compare
Implement BugPattern to rewrite
assertThat(...).isEqualTo(null)
toassertThat(...).isNull()
Example:
Suggested commit message: