rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.31k stars 1.53k forks source link

Lint against `quote::quote!` #12397

Open workingjubilee opened 7 months ago

workingjubilee commented 7 months ago

What it does

Lints against quote::quote! and recommends quote::quote_spanned!

Advantage

Proc macros have essentially zero diagnostics unless they provide such themselves, and tracking Spans, feeding them forward, is a bare-minimum requirement for this. There is a massive number of crates in the ecosystem which use quote but have absolutely execrable diagnostics because they don't bother. Because quote! is easy to use but doesn't ask for a span, they probably don't even realize they need to do this. Proc macros are somewhat of a "dark art" of Rust and people often regard them as unapproachable, and this is probably part of why.

Drawbacks

In some cases, there is genuinely no useful span, so you would just be calling Span::call_site() and feeding it in to satisfy the lint. Oh well.

Example

quote! {
    pub extern "C" fn #new_wrapper_fn(#(#args)) -> #ret_ty {
        #wrapped_fn(#(#args_idents))
    }
}

Could be written as:

quote_spanned! { wrapped_fn_span => 
    pub extern "C" fn #new_wrapper_fn(#(#args)) -> #ret_ty {
        #wrapped_fn(#(#args_idents))
    }
}
samueltardieu commented 7 months ago

Why don't you suggest to put quote!() behind a feature flag of the quote crate instead?

workingjubilee commented 7 months ago

Hmm. To do that is either

While I have had many thoughts about what would be better designs for the macro crates that David Tolnay maintains, and have considered posing them to him, that one seemed obviously untenable to suggest.

matthiaskrgr commented 7 months ago

Seems like one might as well git grep "quote\! {" or am I missing something? "this clippy lint might as well be a bash oneliner" so not sure if its worth coding out. Iirc there also was a stance of not adding too many crate-specific lints at some point as far as I recall.