Skip to content
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

WIP: Fix PMD rule LiteralsFirstInComparisons for compareTo* and contentEquals #448

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

punkratz312
Copy link
Contributor

@punkratz312 punkratz312 force-pushed the Fix-PMD-rule-LiteralsFirstInComparisons-for-compareTo-and-contentEquals branch from 1f33b73 to b38ff0a Compare January 23, 2025 21:08
@punkratz312
Copy link
Contributor Author

I need to fix my setup to get this running. It should be almost there, right? I think it's better to deliver something now rather than waiting longer. Feel free to take over and provide early feedback if this seems like the right direction. Thanks

Vincent Potucek added 4 commits January 25, 2025 17:26
…ns-for-compareTo-and-contentEquals' into Fix-PMD-rule-LiteralsFirstInComparisons-for-compareTo-and-contentEquals
Copy link
Contributor Author

@punkratz312 punkratz312 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel free to takeover and give feedback, thx.

style = Checkstyle.equalsAvoidsNull();
return Preconditions.check(
// new UsesMethod<>("java.lang.String *quals*(..)"),
new UsesMethod<>("java.lang.String *(..)"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @timtebeek,
I need help finding a better regex for equals and compareTo. The current one is just a temporary fix—it might work, but it's inefficient.

}
};
return Preconditions.check(
new UsesMethod<>("java.lang.String *quals*(..)"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this by accident while wondering why the recipe wasn’t applied. I think it would be better to prioritize a functional approach—putting the most important things first. In a functional style, it’s easier to see and understand the flow right away.

Thanks for considering this!

@@ -447,6 +499,15 @@ public class A {
System.out.println(s.compareToIgnoreCase("test"));
}
}
""",
"""
public class A {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure why we don’t have a second comparison method here—you guys probably know better. Looking at the code, it seems like the goal is to avoid an NPE.

This test was the only one failing, which caught my attention, so I decided to conclude it and ask the team for their thoughts.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

2 participants