Skip to content

Update UnsafeLocaleUsage with detection for Locale.of#5279

Closed
mihnita wants to merge 3 commits into
google:masterfrom
mihnita:mihai_locale_of
Closed

Update UnsafeLocaleUsage with detection for Locale.of#5279
mihnita wants to merge 3 commits into
google:masterfrom
mihnita:mihai_locale_of

Conversation

@mihnita

@mihnita mihnita commented Oct 19, 2025

Copy link
Copy Markdown
Member

Fix for #5278

@Stephan202 Stephan202 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just something I noticed on a quick skim.

Comment thread docs/bugpattern/UnsafeLocaleUsage.md
Co-authored-by: Stephan Schroevers <stephan202@gmail.com>

@cushon cushon left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

+ " (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.");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generated diagnostics automatically include the link to the full 'explanation', so writing this out here isn't necessary

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Description.Builder descriptionBuilder =
buildDescription(tree)
.setMessage(
"Avoid using Locale.of static methods (which do not check their arguments for"

Copy link
Copy Markdown
Collaborator

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.of vs. Locale constructors, WDYT about refactoring and using a format string to share the text that's the same for both?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Between constructors and Locale.of there is a lot of sharing (they are basically the same).
I refactored the messages to share, as suggested.

toString throws a wrench in reusing much in the summary.
But while reviewing it I discovered that java.util.Locale is not a library :-), and I fixed that.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@mihnita mihnita left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thank you.

Description.Builder descriptionBuilder =
buildDescription(tree)
.setMessage(
"Avoid using Locale.of static methods (which do not check their arguments for"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Between constructors and Locale.of there is a lot of sharing (they are basically the same).
I refactored the messages to share, as suggested.

toString throws a wrench in reusing much in the summary.
But while reviewing it I discovered that java.util.Locale is not a library :-), and I fixed that.

+ " (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.");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@mihnita
mihnita requested a review from cushon October 20, 2025 15:44
copybara-service Bot pushed a commit that referenced this pull request Oct 27, 2025
Fix for #5278

Fixes #5279

FUTURE_COPYBARA_INTEGRATE_REVIEW=#5279 from mihnita:mihai_locale_of 3cd7fea
PiperOrigin-RevId: 822051091
copybara-service Bot pushed a commit that referenced this pull request Oct 27, 2025
Fix for #5278

Fixes #5279

FUTURE_COPYBARA_INTEGRATE_REVIEW=#5279 from mihnita:mihai_locale_of 3cd7fea
PiperOrigin-RevId: 822051091
@mihnita
mihnita deleted the mihai_locale_of branch October 28, 2025 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants