-
Notifications
You must be signed in to change notification settings - Fork 772
Update UnsafeLocaleUsage with detection for Locale.of
#5279
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
Closed
+270
−52
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; | ||
| import static com.google.errorprone.matchers.Matchers.constructor; | ||
| import static com.google.errorprone.matchers.Matchers.instanceMethod; | ||
| import static com.google.errorprone.matchers.Matchers.staticMethod; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.errorprone.BugPattern; | ||
|
|
@@ -44,6 +45,8 @@ public final class UnsafeLocaleUsage extends BugChecker | |
|
|
||
| private static final Matcher<ExpressionTree> LOCALE_TO_STRING = | ||
| instanceMethod().onExactClass("java.util.Locale").named("toString"); | ||
| private static final Matcher<ExpressionTree> LOCALE_OF = | ||
| staticMethod().onClass("java.util.Locale").named("of"); | ||
| private static final Matcher<ExpressionTree> LOCALE_CONSTRUCTOR = | ||
| constructor().forClass("java.util.Locale"); | ||
|
|
||
|
|
@@ -54,11 +57,27 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState | |
| .setMessage( | ||
| "Avoid using Locale.toString() since it produces a value that" | ||
| + " misleadingly looks like a locale identifier. Prefer using" | ||
| + " Locale.toLanguageTag() since it produces an IETF BCP 47-formatted string that" | ||
| + " can be deserialized back into a Locale.") | ||
| + " Locale.toLanguageTag() since it produces an IETF BCP 47-formatted string" | ||
| + " that can be deserialized back into a Locale.") | ||
| .addFix(SuggestedFixes.renameMethodInvocation(tree, "toLanguageTag", state)) | ||
| .build(); | ||
| } | ||
| if (LOCALE_OF.matches(tree, state)) { | ||
| Description.Builder descriptionBuilder = | ||
| buildDescription(tree) | ||
| .setMessage( | ||
| "Avoid using Locale.of static methods (which do not check their arguments for" | ||
| + " well-formedness). Prefer using Locale.forLanguageTag(String)" | ||
| + " (which takes in an IETF BCP 47-formatted string) or a Locale.Builder" | ||
| + " (which throws exceptions when the input is not well-formed).\n" | ||
| + "Please read the Error Prone documentation page for UnsafeLocaleUsage for" | ||
| + " more information."); | ||
|
||
|
|
||
| fixCallableWithArguments( | ||
| descriptionBuilder, ImmutableList.copyOf(tree.getArguments()), tree, state); | ||
|
|
||
| return descriptionBuilder.build(); | ||
| } | ||
| return Description.NO_MATCH; | ||
| } | ||
|
|
||
|
|
@@ -68,39 +87,42 @@ public Description matchNewClass(NewClassTree tree, VisitorState state) { | |
| Description.Builder descriptionBuilder = | ||
| buildDescription(tree) | ||
| .setMessage( | ||
| "Avoid using Locale constructors, and prefer using" | ||
| + " Locale.forLanguageTag(String) which takes in an IETF BCP 47-formatted" | ||
| + " string or a Locale Builder."); | ||
| "Avoid using Locale constructors (which do not check their arguments for" | ||
| + " well-formedness). Prefer using Locale.forLanguageTag(String)" | ||
| + " (which takes in an IETF BCP 47-formatted string) or a Locale.Builder" | ||
| + " (which throws exceptions when the input is not well-formed).\n" | ||
| + "Please read the Error Prone documentation page for UnsafeLocaleUsage for" | ||
| + " more information."); | ||
|
|
||
| // Only suggest a fix for constructor calls with one or two parameters since there's | ||
| // too much variance in multi-parameter calls to be able to make a confident suggestion | ||
| ImmutableList<ExpressionTree> constructorArguments = | ||
| ImmutableList.copyOf(tree.getArguments()); | ||
| if (constructorArguments.size() == 1) { | ||
| // Locale.forLanguageTag() doesn't support underscores in language tags. We can replace this | ||
| // ourselves when the constructor arg is a string literal. Otherwise, we can only append a | ||
| // .replace() to it. | ||
| ExpressionTree arg = constructorArguments.get(0); | ||
| String replacementArg = | ||
| arg instanceof JCLiteral | ||
| ? String.format( | ||
| "\"%s\"", ASTHelpers.constValue(arg, String.class).replace('_', '-')) | ||
| : String.format( | ||
| "%s.replace('_', '-')", state.getSourceForNode(constructorArguments.get(0))); | ||
| fixCallableWithArguments( | ||
| descriptionBuilder, ImmutableList.copyOf(tree.getArguments()), tree, state); | ||
|
|
||
| descriptionBuilder.addFix( | ||
| SuggestedFix.replace(tree, String.format("Locale.forLanguageTag(%s)", replacementArg))); | ||
| } else if (constructorArguments.size() == 2) { | ||
| descriptionBuilder.addFix( | ||
| SuggestedFix.replace( | ||
| tree, | ||
| String.format( | ||
| "new Locale.Builder().setLanguage(%s).setRegion(%s).build()", | ||
| state.getSourceForNode(constructorArguments.get(0)), | ||
| state.getSourceForNode(constructorArguments.get(1))))); | ||
| } | ||
| return descriptionBuilder.build(); | ||
| } | ||
| return Description.NO_MATCH; | ||
| } | ||
|
|
||
| // Something that can be called with arguments, for example a method or constructor. | ||
| private static void fixCallableWithArguments( | ||
| Description.Builder descriptionBuilder, | ||
| ImmutableList<? extends ExpressionTree> arguments, | ||
| ExpressionTree tree, | ||
| VisitorState state) { | ||
|
|
||
| // Only suggest a fix for constructor or Locale.of calls with one parameter since there's | ||
| // too much variance in multi-parameter calls to be able to make a confident suggestion | ||
| if (arguments.size() == 1) { | ||
| // Locale.forLanguageTag() doesn't support underscores in language tags. We can replace this | ||
| // ourselves when the constructor arg is a string literal. Otherwise, we can only append a | ||
| // .replace() to it. | ||
| ExpressionTree arg = arguments.get(0); | ||
| String replacementArg = | ||
| arg instanceof JCLiteral | ||
| ? String.format( // Something like `new Locale("en_US")` or `Locale.of("en_US")` | ||
| "\"%s\"", ASTHelpers.constValue(arg, String.class).replace('_', '-')) | ||
| : String.format("%s.replace('_', '-')", state.getSourceForNode(arguments.get(0))); | ||
| descriptionBuilder.addFix( | ||
| SuggestedFix.replace(tree, String.format("Locale.forLanguageTag(%s)", replacementArg))); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
By default, the message is the
BugPattern.summary. Is there information here that applies to all findings that could go in the summary instead?If you want to customize this to mention e.g.
Locale.ofvs.Locale constructors, WDYT about refactoring and using a format string to share the text that's the same for both?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.
Between constructors and
Locale.ofthere is a lot of sharing (they are basically the same).I refactored the messages to share, as suggested.
toStringthrows a wrench in reusing much in the summary.But while reviewing it I discovered that
java.util.Localeis not a library :-), and I fixed that.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.
Done