From 0db3360a331ee1f6f882f6e550d854a025337e56 Mon Sep 17 00:00:00 2001 From: Error Prone Team Date: Mon, 18 Nov 2024 00:50:46 -0800 Subject: [PATCH] UsafeLocaleUsage: update the proposed fix to use `replace(char, char)` The current `replace(CharSequence, CharSequence)` triggers a JavaOptionalSuggestions, so we might as well apply it directly. PiperOrigin-RevId: 697528702 --- .../com/google/errorprone/bugpatterns/UnsafeLocaleUsage.java | 5 ++--- .../google/errorprone/bugpatterns/UnsafeLocaleUsageTest.java | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/UnsafeLocaleUsage.java b/core/src/main/java/com/google/errorprone/bugpatterns/UnsafeLocaleUsage.java index 1746c86ab6f..442f01bab4e 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/UnsafeLocaleUsage.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/UnsafeLocaleUsage.java @@ -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))); diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/UnsafeLocaleUsageTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/UnsafeLocaleUsageTest.java index a690f816147..fbcee7a2c31 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/UnsafeLocaleUsageTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/UnsafeLocaleUsageTest.java @@ -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('_', '-')); } }