-
Notifications
You must be signed in to change notification settings - Fork 61
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
address: LiteralsFirstInComparisons PMD rule #368
address: LiteralsFirstInComparisons PMD rule #368
Conversation
…isitorTest.java Co-authored-by: Tim te Beek <timtebeek@gmail.com>
…incomparisons_extend
// instanceof check
…iteralsfirstincomparisons_extend' into EqualsAvoidsNullVisitor-extend-literalsfirstincomparisons_extend
…isitorTest.java Co-authored-by: Tim te Beek <timtebeek@gmail.com>
This reverts commit 2c2b33a.
Thanks again @punkratz312 ! I've got an early flight tomorrow and Friday (mostly) off, so it might be Monday before I get to a review and merge. |
Have a happy flight and enjoy your trip! Thank you for your guidance and support. |
…isitor.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
* <a href="https://pmd.github.io/pmd/pmd_rules_java_bestpractices.html#LiteralsFirstInComparisons">Literals First in Comparisons</a> | ||
* | ||
* @param <P> The type of the parent context used for visiting the AST. | ||
*/ | ||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class EqualsAvoidsNullVisitor<P> extends JavaVisitor<P> { |
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.
Perhaps LiteralsFirstInComparisonsVisitor would be a better fit to align with the corresponding PMD rule name already in place, as it's no longer just about equality; both equality and comparison are now merely implementation details.
public class EqualsAvoidsNullVisitor<P> extends JavaVisitor<P> { | |
public class LiteralsFirstInComparisonsVisitor<P> extends JavaVisitor<P> { |
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'd perhaps hold off on renaming this just yet. While the visitor only appears to be used in this module, the recipe name is well established as part of our Common static analysis issues recipe, which folks have created custom copies off that might break if we similarly rename the recipe. I'll think this over as I go through the review.
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.
Yes thanks. Its just an idea and of course would be a new PR topic.
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.
Some suggestions could not be made:
- src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
- lines 36-38
src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNullVisitor.java
Outdated
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.
Some suggestions could not be made:
- src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
- lines 36-38
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.
Some suggestions could not be made:
- src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
- lines 36-38
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 like how you've expanded the existing visitor to cover more cases, and by extension the recipe to do more similar replacements without the need for users to adopt any new recipe!
I also appreciate you're looking to apply best practices to the visitor itself, even if those diverge from what's common elsewhere throughout our code base. It'll take some getting used to on both side on what to (not) pick up as part of your work on recipes, as it does lead to more work to review, and at times discussions as to what's the best way forward.
Typically we view recipes and visitors as one complete unit; there's little need to optimize within such a unit, and at times patterns like the four-fold requireNonNull(m.getSelect())
introduced here are a symptom of branching off separate methods, where the nested structure would have no such need. Going forward lets try to minimize such changes, or perhaps propose them first as part of a self-review such that there's no conflicts arising late in review.
I've left a final question about a removed earlier optimization; once that's answered we can merge this work; thanks again!
…iteralsfirstincomparisons_extend' into EqualsAvoidsNullVisitor-extend-literalsfirstincomparisons_extend
@@ -97,8 +92,8 @@ private static J.Binary literalsFirstInComparisonsNull(J.MethodInvocation m, Exp | |||
m.getPrefix(), | |||
Markers.EMPTY, | |||
requireNonNull(m.getSelect()), | |||
build(J.Binary.Type.Equal).withBefore(SINGLE_SPACE), | |||
firstArgument.withPrefix(SINGLE_SPACE), | |||
JLeftPadded.build(J.Binary.Type.Equal).withBefore(Space.SINGLE_SPACE), |
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.
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.
Often classes use a mix of the two constants in Space, only one of which has _SPACE
in the name for the repetition. I'd prefer then to be consistent with prior use, and i/fwhen we dedice to change that, do so through our enforced patterns in rewrite-recommendations. I hope that context helps!
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 suspect the idea with the name was to emphasize that the object represents a single space character. The class Space
represents any sequence of whitespace and comments, so Space.SINGLE
would be a bit ambiguous (a single space, tab, newline, or comment?), so Space.SINGLE_SPACE
uses "space" in two different meanings.
src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNullVisitor.java
Outdated
Show resolved
Hide resolved
Thanks for the feedback and correction. I think we might got it now 👍 |
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.
Some suggestions could not be made:
- src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
- lines 36-38
Thanks again @punkratz312 ! |
Thank you, @timtebeek, it was an honor and a great learning experience. I appreciate the support, guidance, and patience. |
You're more than welcome! Your change has just landed in the latest release: |
LiteralsFirstInComparisons
forcompareTo*
andcontentEquals
#362