Repository navigation
Add PreferFormatMethod check for redundant String.format calls. - #6326
Open
copybara-service[bot] wants to merge 1 commit into
Open
copybara-service[bot] wants to merge 1 commit into
copybara-service[bot] wants to merge 1 commit into
Conversation
Callers of exceptions, precondition helpers, and builders that provide `@FormatMethod` overloads frequently wrap arguments in `String.format(...)` or `"..." .formatted(...)` and invoke the plain `String` overload instead. Pre-formatting the message is more verbose and prevents lazy formatting in APIs that only format on failure. Add `PreferFormatMethod` (initially disabled by default in `DISABLED_CHECKS` pending evaluation) to detect and unwrap redundant `String.format` and `String.formatted` arguments when an accessible, unambiguous `@FormatMethod` overload exists on the target class or receiver hierarchy. The check supports both direct positional unwrapping (`(..., String)` → `(..., @FormatString String, Object...)`) and cause-first argument swapping (`(String, Throwable)` → `(Throwable, @FormatString String, Object...)`), while validating format strings via `StrictFormatStringValidation` and guarding against self-delegation and fixed-arity overload shadowing. PiperOrigin-RevId: 1015021170
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Add PreferFormatMethod check for redundant String.format calls.
Callers of exceptions, precondition helpers, and builders that provide
@FormatMethodoverloads frequently wrap arguments inString.format(...)or"..." .formatted(...)and invoke the plainStringoverload instead. Pre-formatting the message is more verbose and prevents lazy formatting in APIs that only format on failure.Add
PreferFormatMethod(initially disabled by default inDISABLED_CHECKSpending evaluation) to detect and unwrap redundantString.formatandString.formattedarguments when an accessible, unambiguous@FormatMethodoverload exists on the target class or receiver hierarchy. The check supports both direct positional unwrapping ((..., String)→(..., @FormatString String, Object...)) and cause-first argument swapping ((String, Throwable)→(Throwable, @FormatString String, Object...)), while validating format strings viaStrictFormatStringValidationand guarding against self-delegation and fixed-arity overload shadowing.