-
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
Introduce StringCaseLocaleUsage
check
#376
Conversation
d1a6032
to
a817072
Compare
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/SpecifyLocale.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/SpecifyLocale.java
Outdated
Show resolved
Hide resolved
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
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.
Hey @mlrprananta thanks for opening the PR, it's a nice one 🚀 !
Made some suggestions and some open questions. Some of them I applied already but they are not final, as in still open for discussion 😄. Don't have time to fully finalize the review but it's in a good state.
The biggest point for me is the name of the check 🤔. Let's see :).
Added a commit.
linkType = CUSTOM, | ||
severity = WARNING, | ||
tags = LIKELY_ERROR) | ||
public final class SpecifyLocale extends BugChecker implements MethodInvocationTreeMatcher { |
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 yet about the naming of the check. The alternatives I have are way to long 😅.
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.
Likewise not sure about naming, I'm open for suggestions
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.
Also not sure. Best I got for now is ImplicitLocaleUsage
; will push, but also open to suggestions.
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 agree with @rickie that we should specify String
as well to make it more specific, unless we want to extend this rule to other methods/classes as well later?
Would StringCaseImplicitLocaleUsage
be too long? 😅
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/SpecifyLocale.java
Outdated
Show resolved
Hide resolved
String localeToSpecify, MethodInvocationTree tree, VisitorState state) { | ||
return SuggestedFix.builder() | ||
.replace( | ||
tree, SourceCode.treeToString(tree, state).replace("()", "(" + localeToSpecify + ")")) |
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.
Another way of doing this could be:
int endPos = state.getEndPosition(tree);
SuggestedFix.replace(endPos - 2, endPos, "(Locale.ROOT)")
Not sure if that is better though 🤔.
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 I don't have a huge preference, but I find my way a bit more clear in what's happening
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/SpecifyLocale.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/SpecifyLocale.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/SpecifyLocale.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/SpecifyLocale.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/SpecifyLocaleTest.java
Outdated
Show resolved
Hide resolved
compilationTestHelper | ||
.addSourceLines( | ||
"A.java", | ||
"class A {", |
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.
We are missing some examples that we wouldn't flag as well.
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.
Now that there are quite some characters of the alphabet I'm not sure if it's still the easiest / best way to use them in that way. Although at first it absolutely made sense :).
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.
Did you have an alternative in mind?
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/SpecifyLocale.java
Outdated
Show resolved
Hide resolved
Looks good. All 4 mutations in this change were killed.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
da233fe
to
a43b876
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.
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/SpecifyLocale.java
Outdated
Show resolved
Hide resolved
private static Fix suggestLocale(String locale, MethodInvocationTree tree, VisitorState state) { | ||
return SuggestedFix.builder() | ||
.addImport("java.util.Locale") | ||
.replace(tree, SourceCode.treeToString(tree, state).replace("()", "(" + locale + ")")) |
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 assumes that the code is properly formatted. More robust:
.replace(tree, SourceCode.treeToString(tree, state).replace("()", "(" + locale + ")")) | |
.replace(tree, SourceCode.treeToString(tree, state).replace("(", '(' + locale )) |
Due to auto-formatting of our test code we can't just add an extra space, but this case can be modelled using an extra comment. Will push something.
Edit: even this is too fragile. Consider str.toUpperCase( /* () */ )
. So it should be:
.replace(tree, SourceCode.treeToString(tree, state).replace("()", "(" + locale + ")")) | |
.replace(tree, SourceCode.treeToString(tree, state).replaceFirst("\\(", '(' + locale)) |
public SpecifyLocale() {} | ||
|
||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { |
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.
Next to a MethodInvocationTree
we should also handle the associated method reference.
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/SpecifyLocale.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/SpecifyLocale.java
Outdated
Show resolved
Hide resolved
Looks good. All 4 mutations in this change were killed.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
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.
Pushed renaming suggestion. Only open point from my side is the method reference handling. Could also be deferred (I already added an XXX
comment), but would like to make a habit of covering this case.
linkType = CUSTOM, | ||
severity = WARNING, | ||
tags = LIKELY_ERROR) | ||
public final class SpecifyLocale extends BugChecker implements MethodInvocationTreeMatcher { |
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.
Also not sure. Best I got for now is ImplicitLocaleUsage
; will push, but also open to suggestions.
Looks good. All 4 mutations in this change were killed.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
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.
Better to defer; expanded the comment. Suggested commit message:
Introduce `ImplicitLocaleUsage` check (#376)
Looks good. All 4 mutations in this change were killed.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
private static Fix suggestLocale(MethodInvocationTree tree, String locale, VisitorState state) { | ||
return SuggestedFix.builder() | ||
.addImport("java.util.Locale") | ||
.replace(tree, SourceCode.treeToString(tree, state).replaceFirst("\\(", '(' + locale)) |
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.
Even this isn't correct. Added another comment for further follow-up work.
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.
Just to be sure, should the XXX
be tackled in this PR?
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.
Just to be sure, should the
XXX
be tackled in this PR?
Nope :). W.r.t. filing issues as suggested by @rickie: I'd only do that if we have a serious intention of picking up the work.
Looks good. All 4 mutations in this change were killed.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
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.
Looked at the changes and they LGTM!
We could also decide to make GH issues out of the XXX
es, but not sure if necessary for both though.
I think deferring the XXX
'es is a smart thing to do as it indeed probably involves a new utility class which are usually not that easy.
One thing a like about ImplicitLocaleUsage
is the word implicit. What I do not really like is that String
is "missing". As in, it captures the essense, but the focus is clearly on Strings
which is not really clear from the name. Therefore I'm proposing two others:
StringLocaleUsage
StringCaseLocaleUsage
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.
W.r.t. the checker name: StringCaseLocaleUsage
work for me. Or perhaps ImplicitStringCaseLocale
? 🤔
private static Fix suggestLocale(MethodInvocationTree tree, String locale, VisitorState state) { | ||
return SuggestedFix.builder() | ||
.addImport("java.util.Locale") | ||
.replace(tree, SourceCode.treeToString(tree, state).replaceFirst("\\(", '(' + locale)) |
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.
Just to be sure, should the
XXX
be tackled in this PR?
Nope :). W.r.t. filing issues as suggested by @rickie: I'd only do that if we have a serious intention of picking up the work.
To be honest, I prefer the |
5790910
to
e5b7b25
Compare
Looks good. All 11 mutations in this change were killed.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
1 similar comment
Looks good. All 11 mutations in this change were killed.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
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, thanks for being fast with replying and applying @mlrprananta 😉.
You're probably the first one to receive the new Christmas sticker 🎄 🎅🏻 !
@oxkitsune anything from your side :)? |
String#to{Lower,Upper}Case
StringCaseLocaleUsage
check
PR for this Issue.
In short, a default Locale should be specified when using either
String#toLowerCase()
orString#toUpperCase()
.Suggested commit message