Skip to content

Commit

Permalink
UsafeLocaleUsage: update the proposed fix to use replace(char, char)
Browse files Browse the repository at this point in the history
The current `replace(CharSequence, CharSequence)` triggers a JavaOptionalSuggestions, so we might as well apply it directly.

PiperOrigin-RevId: 696138592
  • Loading branch information
java-team-github-bot authored and Error Prone Team committed Nov 15, 2024
1 parent 5d8adc5 commit da5ac78
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ public Description matchNewClass(NewClassTree tree, VisitorState state) {
String replacementArg =
arg instanceof JCLiteral
? String.format(
"\"%s\"", ASTHelpers.constValue(arg, String.class).replace("_", "-"))
"\"%s\"", ASTHelpers.constValue(arg, String.class).replace('_', '-'))
: String.format(
"%s.replace(\"_\", \"-\")",
state.getSourceForNode(constructorArguments.get(0)));
"%s.replace('_', '-')", state.getSourceForNode(constructorArguments.get(0)));

descriptionBuilder.addFix(
SuggestedFix.replace(tree, String.format("Locale.forLanguageTag(%s)", replacementArg)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Test {
static class Inner {
private Locale locale;
Inner(String a) {
locale = Locale.forLanguageTag(a.replace("_", "-"));
locale = Locale.forLanguageTag(a.replace('_', '-'));
}
}
Expand Down

0 comments on commit da5ac78

Please sign in to comment.