-
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
Extend null
check Refaster rules
#523
Conversation
Looks good. No mutations were possible for these changes. |
3 similar comments
Looks good. No mutations were possible for these changes. |
Looks good. No mutations were possible for these changes. |
Looks good. No mutations were possible for these changes. |
0181a9a
to
942c604
Compare
Looks good. No mutations were possible for these changes. |
1 similar comment
Looks good. No mutations were possible for these changes. |
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.
Added a commit.
Changes LGTM 😄. Added an extra template to make our lives easier. We can make more assumptions about the code and we need to account for less cases.
@@ -78,7 +79,7 @@ void after(int index, int size, String message) { | |||
static final class RequireNonNull<T> { | |||
@BeforeTemplate | |||
void before(T object) { | |||
if (object == null) { | |||
if (Refaster.anyOf(object == null, null == object)) { |
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.
Instead of having to add the inverse check, we can add another Refaster rule that rewrites null == object
to object == null
. That allows us to simplify the code in many other places.
791ca72
to
1f2a953
Compare
Rebased and tweaked suggested commit message. |
Looks good. No mutations were possible for these changes. |
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. Open point is about the IllegalArgumentException
changes; IMHO those should be omitted.
checkArgument(clazz != null, "Visited node is not enclosed by a class"); | ||
requireNonNull(clazz, "Visited node is not enclosed by a class"); |
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 fact that we're validating the input though a null
check is a function of the findEnclosing
API; it could also have returned an Optional<ClassTree>
. In this case IllegalArgumentException
seems like a more appropriate exception to throw. (IllegalStateException
could perhaps also work.)
I don't have strong options for this particular bit of code, but IMHO it shows that we should not apply the checkArgument
rewrite below 👇
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 this is a difficult one. A GH code search shows there are quite some good improvements here. However, indeed some of them are not entirely correct.
There is only one heuristic that comes to mind, but that is probably a bit weird and odd. If the message contains "null" than it's a good check in all our cases. However, I see why this wouldn't fly for the OSS world 😋.
In that case, we should indeed drop it 🤔.
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.
Dropping is the safe way forward 👍
@@ -25,6 +25,11 @@ private NullRules() {} | |||
static final class IsNull { | |||
@BeforeTemplate | |||
boolean before(@Nullable Object object) { | |||
return null == object; |
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.
Nice one. Let's also update the documentation.
@BeforeTemplate | ||
void before3(T object) { | ||
checkArgument(object != null); | ||
} |
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.
As mentioned above, IMHO this change is too invasive/opinionated for this check. Note that impacted code may be part of a method with a documented contract of throwing IllegalArgumentException
rather than NullArgumentException
.
@@ -72,35 +74,55 @@ void after(int index, int size, String message) { | |||
} | |||
} | |||
|
|||
/** Prefer {@link Preconditions#checkNotNull(Object)} over more verbose alternatives. */ | |||
static final class CheckNotNull<T> { | |||
/** Prefer {@link Objects#requireNonNull(Object)} over more verbose alternatives. */ |
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.
checkNotNull
is not more verbose 👀
@BeforeTemplate | ||
void before(T object) { | ||
if (object == null) { | ||
throw new NullPointerException(); | ||
} | ||
} | ||
|
||
@BeforeTemplate | ||
void before2(T object) { | ||
checkNotNull(object); |
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 rule won't match expressions of this type. The way Renovate works, we'll need a separate rule for that. (And then drop the rule here, because expression rules match statements, but not vice versa.)
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.
Haha Renovate 😉. But good one, this is where I should improve with reviewing 🤔.
@BeforeTemplate | ||
boolean before2(@Nullable Object object) { | ||
return Objects.isNull(object); | ||
} |
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.
Here and below: we can use Refaster.anyOf
.
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.
You are right (I hear you say it hehe) we should automate this :).
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.
Indeed we should ;)
1f2a953
to
26c323f
Compare
Looks good. No mutations were possible for these changes. |
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.
Added a commit and answered the open points :).
checkArgument(clazz != null, "Visited node is not enclosed by a class"); | ||
requireNonNull(clazz, "Visited node is not enclosed by a class"); |
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 this is a difficult one. A GH code search shows there are quite some good improvements here. However, indeed some of them are not entirely correct.
There is only one heuristic that comes to mind, but that is probably a bit weird and odd. If the message contains "null" than it's a good check in all our cases. However, I see why this wouldn't fly for the OSS world 😋.
In that case, we should indeed drop it 🤔.
@BeforeTemplate | ||
boolean before2(@Nullable Object object) { | ||
return Objects.isNull(object); | ||
} |
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.
You are right (I hear you say it hehe) we should automate this :).
@BeforeTemplate | ||
void before(T object) { | ||
if (object == null) { | ||
throw new NullPointerException(); | ||
} | ||
} | ||
|
||
@BeforeTemplate | ||
void before2(T object) { | ||
checkNotNull(object); |
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.
Haha Renovate 😉. But good one, this is where I should improve with reviewing 🤔.
@BeforeTemplate | ||
void before(T object) { | ||
if (object == null) { | ||
throw new NullPointerException(); | ||
} | ||
} | ||
|
||
@BeforeTemplate | ||
void before3(T object) { |
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.
void before3(T object) { | |
void before2(T object) { |
/** Prefer {@link Objects#requireNonNull(Object, String)} over non-JDK alternatives. */ | ||
static final class RequireNonNullWithMessage<T> { | ||
@BeforeTemplate | ||
T before2(T object, String message) { |
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.
T before2(T object, String message) { | |
T before(T object, String message) { |
Looks good. No mutations were possible for these changes. |
} | ||
} | ||
|
||
/** Prefer {@link Objects#requireNonNull(Object)} over non-JDK alternatives. */ |
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.
/** Prefer {@link Objects#requireNonNull(Object)} over non-JDK alternatives. */ | |
/** Prefer {@link Objects#requireNonNull(Object)} over more verbose or non-JDK alternatives. */ |
But, assuming we drop the checkArgument
check:
/** Prefer {@link Objects#requireNonNull(Object)} over non-JDK alternatives. */ | |
/** Prefer {@link Objects#requireNonNull(Object)} over more verbose alternatives. */ |
checkArgument(clazz != null, "Visited node is not enclosed by a class"); | ||
requireNonNull(clazz, "Visited node is not enclosed by a class"); |
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.
Dropping is the safe way forward 👍
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.
Added a commit and rebased.
9d8141e
to
94900a4
Compare
One open question is how to address the "Statement" part of the Refaster rules in the suggested commit message. I'm not sure if it adds value to put it there. Any suggestions @Stephan202 ? |
Looks good. No mutations were possible for these changes. |
1 similar comment
Looks good. No mutations were possible for these changes. |
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.
Not sure either, but proposed something ;).
Code changes LGTM!
requireNonNull
null
check Refaster rules
Follow up of this issue.
Suggested commit message: