Skip to content

Add PreferFormatMethod check for redundant String.format calls. - #6326

Open
copybara-service[bot] wants to merge 1 commit into
masterfrom
test_1015021170
Open

copybara-service[bot] wants to merge 1 commit into
masterfrom
test_1015021170

Conversation

@copybara-service

Copy link
Copy Markdown
Contributor

Add PreferFormatMethod check for redundant String.format calls.

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.

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

No deployments
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.

1 participant