Skip to content

Conversation

@nyurik
Copy link

@nyurik nyurik commented Jan 26, 2026

Since 1.85, Clippy supports #[clippy::format_args] attribute - see docs - this way all users of this macro will get the benefits of clippy format arg validations.

The only possible problem is that format_ident!("Foo{bar}") might not be edition2021+ compliant (i.e. may treat that string as verbatim). I suspect it might be ok to modify the macro implementation to always treat a single param macro as a format string because anything with curlies in it is not a valid identifier.

Fixes #290
Related #206

Since 1.85, Clippy supports `#[clippy::format_args]` attribute - see [docs](https://doc.rust-lang.org/clippy/attribs.html#clippyformat_args) - this way all users of this macro will get the benefits of clippy format arg validations.

The only possible problem is that `format_ident!("Foo{bar}")` might not be edition2021+ compliant (i.e. may treat that string as verbatim).  I suspect it might be ok to modify the macro implementation to always treat a single param macro as a format string because anything with curlies in it is not a valid identifier.
Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Could you share an example of a diagnostic output that this PR would cause clippy to emit?

@nyurik
Copy link
Author

nyurik commented Jan 26, 2026

From https://github.com/rust-lang/rust-clippy/blob/master/CHANGELOG.md#rust-185 (implemented in rust-lang/rust-clippy#9948) - many existing lints, namely format_in_format_args, recursive_format_impl, to_string_in_format_args, uninlined_format_args, and unused_format_specs will treat format_ident! exactly the same as they treat format!. So any diagnostic output these lints show would simply show up for format_ident!-using code.

Note that the above list is not complete - any Clippy lint that internally calls is_format_macro() will do it too.

@dtolnay
Copy link
Owner

dtolnay commented Jan 26, 2026

I know about that, and I have used this feature in dtolnay/anyhow#426. But specifically for quote::format_ident I want to see an actual diagnostic emitted by clippy as a result of this.

@dtolnay
Copy link
Owner

dtolnay commented Jan 26, 2026

Specifically, in the following part of the expansion, notice how any $arg would always be evaluated outside the context of a format_args!. It is not obvious that any of the lints you have named would apply to the resulting syntax tree, nor that if they apply, they would produce a suggestion that is meaningful and correct.

quote/src/format.rs

Lines 163 to 165 in ad9fb40

([$span:expr, $($fmt:tt)*] $arg:expr, $($rest:tt)*) => {
match $crate::__private::IdentFragmentAdapter(&$arg) {
arg => $crate::format_ident_impl!([$span.or(arg.span()), $($fmt)*, arg] $($rest)*),

@nyurik
Copy link
Author

nyurik commented Jan 26, 2026

You are right, it seems current Clippy logic either does not see this macro as a valid candidate for evaluation, or some other thing. I could try to look into Clippy logic itself, or what would you suggest?

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.

Add #[clippy::format_args] on format_ident!

2 participants