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

Introduce StringCaseLocaleUsage check #376

Merged
merged 9 commits into from
Dec 5, 2022
Merged

Conversation

mlrprananta
Copy link
Contributor

@mlrprananta mlrprananta commented Nov 28, 2022

PR for this Issue.

In short, a default Locale should be specified when using either String#toLowerCase() or String#toUpperCase().

Suggested commit message

Introduce `StringCaseLocaleUsage` check (#376)

@github-actions
Copy link

github-actions bot commented Dec 1, 2022

  • Surviving mutants in this change: 1
  • Killed mutants in this change: 3
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.SpecifyLocale 1 3

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member

@rickie rickie left a 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 {
Copy link
Member

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 😅.

Copy link
Contributor Author

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

Copy link
Member

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.

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 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? 😅

String localeToSpecify, MethodInvocationTree tree, VisitorState state) {
return SuggestedFix.builder()
.replace(
tree, SourceCode.treeToString(tree, state).replace("()", "(" + localeToSpecify + ")"))
Copy link
Member

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 🤔.

Copy link
Contributor Author

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

compilationTestHelper
.addSourceLines(
"A.java",
"class A {",
Copy link
Member

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.

Copy link
Member

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 :).

Copy link
Contributor Author

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?

@github-actions
Copy link

github-actions bot commented Dec 2, 2022

Looks good. All 4 mutations in this change were killed.

class surviving killed
🎉tech.picnic.errorprone.bugpatterns.SpecifyLocale 0 4

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member

@Stephan202 Stephan202 left a 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.

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 + ")"))
Copy link
Member

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:

Suggested change
.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:

Suggested change
.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) {
Copy link
Member

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.

@github-actions
Copy link

github-actions bot commented Dec 2, 2022

Looks good. All 4 mutations in this change were killed.

class surviving killed
🎉tech.picnic.errorprone.bugpatterns.SpecifyLocale 0 4

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member

@Stephan202 Stephan202 left a 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 {
Copy link
Member

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.

@github-actions
Copy link

github-actions bot commented Dec 2, 2022

Looks good. All 4 mutations in this change were killed.

class surviving killed
🎉tech.picnic.errorprone.bugpatterns.ImplicitLocaleUsage 0 4

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202 Stephan202 added this to the 0.6.0 milestone Dec 2, 2022
Copy link
Member

@Stephan202 Stephan202 left a 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)

@github-actions
Copy link

github-actions bot commented Dec 2, 2022

Looks good. All 4 mutations in this change were killed.

class surviving killed
🎉tech.picnic.errorprone.bugpatterns.ImplicitLocaleUsage 0 4

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))
Copy link
Member

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.

Copy link
Contributor Author

@mlrprananta mlrprananta Dec 5, 2022

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?

Copy link
Member

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.

@github-actions
Copy link

github-actions bot commented Dec 3, 2022

Looks good. All 4 mutations in this change were killed.

class surviving killed
🎉tech.picnic.errorprone.bugpatterns.ImplicitLocaleUsage 0 4

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member

@rickie rickie left a 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 XXXes, 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

Copy link
Member

@Stephan202 Stephan202 left a 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))
Copy link
Member

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.

@rickie
Copy link
Member

rickie commented Dec 5, 2022

W.r.t. the checker name: StringCaseLocaleUsage work for me. Or perhaps ImplicitStringCaseLocale? 🤔

To be honest, I prefer the Usage one as it is more in line with some other names we used till now.

@github-actions
Copy link

github-actions bot commented Dec 5, 2022

Looks good. All 11 mutations in this change were killed.

class surviving killed
🎉tech.picnic.errorprone.bugpatterns.StringCaseLocaleUsage 0 11

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

1 similar comment
@github-actions
Copy link

github-actions bot commented Dec 5, 2022

Looks good. All 11 mutations in this change were killed.

class surviving killed
🎉tech.picnic.errorprone.bugpatterns.StringCaseLocaleUsage 0 11

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member

@rickie rickie left a 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 🎄 🎅🏻 !

@rickie
Copy link
Member

rickie commented Dec 5, 2022

@oxkitsune anything from your side :)?

@rickie rickie changed the title Specify Locale when calling String#to{Lower,Upper}Case Introduce StringCaseLocaleUsage check Dec 5, 2022
@rickie rickie merged commit 4f9aba8 into master Dec 5, 2022
@rickie rickie deleted the mlrprananta/specify-locale branch December 5, 2022 12:49
@rickie rickie linked an issue Dec 6, 2022 that may be closed by this pull request
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Specify Locale when calling String#to{Lower,Upper}Case
4 participants