Skip to content

Commit d41f822

Browse files
graememorganError Prone Team
authored andcommitted
Make suggested edits and take a pass over the markdown from 5216a43
PiperOrigin-RevId: 834323965
1 parent 6db6f95 commit d41f822

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/UnsafeLocaleUsage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public final class UnsafeLocaleUsage extends BugChecker
5050
private static final Matcher<ExpressionTree> LOCALE_CONSTRUCTOR =
5151
constructor().forClass("java.util.Locale");
5252

53-
// Used for both Locale constructors and Locale.of static methods.
53+
/** Used for both Locale constructors and Locale.of static methods. */
5454
private static final String DESCRIPTION =
5555
" They do not check their arguments for"
5656
+ " well-formedness. Prefer using Locale.forLanguageTag(String)"
@@ -96,7 +96,7 @@ public Description matchNewClass(NewClassTree tree, VisitorState state) {
9696
return Description.NO_MATCH;
9797
}
9898

99-
// Something that can be called with arguments, for example a method or constructor.
99+
/** Something that can be called with arguments, for example a method or constructor. */
100100
private static void fixCallableWithArguments(
101101
Description.Builder descriptionBuilder,
102102
ImmutableList<? extends ExpressionTree> arguments,

docs/bugpattern/UnsafeLocaleUsage.md

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ examples of error prone issues below:
44
#### Constructors
55

66
The constructors don't validate the parameters at all, they just "trust" it
7-
100%. \
8-
This is also true for the static method `Locale.of`, introduced in JDK 19.
7+
100%. This is also true for the static method `Locale.of`, introduced in JDK 19.
98

109
For example:
1110

@@ -49,10 +48,9 @@ getVariant() :
4948
We can see that while the `toString()` value for both locales are equivalent,
5049
the individual parts are different. More specifically, the first locale is
5150
incorrect since `#Hant` is supposed to be the script for the locale rather than
52-
the variant. \
53-
There's no reliable way of getting a correct result through a `Locale`
54-
constructor, so we should prefer using `Locale.forLanguageTag()` (and the IETF
55-
BCP 47 format) for correctness.
51+
the variant. There's no reliable way of getting a correct result through a
52+
`Locale` constructor, so we should prefer using `Locale.forLanguageTag()` (and
53+
the IETF BCP 47 format) for correctness.
5654

5755
**Note:** You might see a `.replace('_', '-')` appended to a suggested fix for
5856
the error prone checker for this bug pattern. This is sanitization measure to
@@ -68,7 +66,6 @@ If the initial code started with a `String` that was split at `'_'` or `'-'`,
6866
just to be used for locale, the right fix is to use `toLanguageTag()`.
6967

7068
```java
71-
// Initial code
7269
void someMethod(String localeId) {
7370
String[] parts = localeId.split("_");
7471
Locale locale = switch (parts.size) {
@@ -78,10 +75,11 @@ void someMethod(String localeId) {
7875
}
7976
// use the locale
8077
}
78+
```
8179

82-
// Fixed code
80+
```java
8381
void someMethod(String localeId) {
84-
Locale locale = Locale.forLanguageTag.replace('_', '-');
82+
Locale locale = Locale.forLanguageTag(localeId.replace('_', '-'));
8583
// use the locale
8684
}
8785
```
@@ -90,15 +88,15 @@ If the initial code started separate "pieces" (language, region, variant) the
9088
right fix is to use a `Locale.Builder()`.
9189

9290
```java
93-
// Initial code
9491
void someMethod(@NotNull String langId, String regionId) {
9592
Locale locale (regionId == null)
9693
? new Locale(langId) // or Locale.of
9794
: new Locale(langId, regionId); // or Locale.of
9895
// use the locale
9996
}
97+
```
10098

101-
// Fixed code
99+
```java
102100
void someMethod(@NotNull String langId, String regionId) {
103101
Locale.Builder builder = new Locale.Builder();
104102
builder.setLanguage(langId);
@@ -110,7 +108,7 @@ void someMethod(@NotNull String langId, String regionId) {
110108
}
111109
```
112110

113-
#### toString()
111+
#### `toString()`
114112

115113
This poses the inverse of the constructor problem.
116114

@@ -121,11 +119,10 @@ Locale derivedLocale = ??? // Not clean way to get a correct locale from myLocal
121119
```
122120

123121
The `toString()` implementation for `Locale` isn't necessarily incorrect in
124-
itself. \
125-
It is intended to be _"concise but informative representation that is easy for a
126-
person to read"_ (see documentation at
122+
itself. It is intended to be *"concise but informative representation that is
123+
easy for a person to read"* (see documentation at
127124
[Object.toString()](https://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#toString\(\))).
128125

129126
So it is not intended to produce a value that can be turned back into a
130-
`Locale`. It is not a serialization format. \
131-
It often produces a value that _looks_ like a locale identifier, but it is not.
127+
`Locale`. It is not a serialization format. It often produces a value that
128+
*looks* like a locale identifier, but it is not.

0 commit comments

Comments
 (0)