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.39k stars 1.54k forks source link

Cargo clippy report warnings from macros when using quote_spanned #13264

Open oddcoder opened 2 months ago

oddcoder commented 2 months ago

Summary

Cargo clippy captures warnings from inside macros when using quote_spanned!. Attempting --fix results in: warning: failed to automatically apply fixes suggested by rustc to crate ..... after fixes were automatically applied the compiler reported errors within these files ....

Replacing quote_spanned with quote here made the bug disappear.

Reproducer

The code does not fit in a snippet, unfortunately multiple crates are required But a small reproducer may be found here

I expected to see no warnings when running cargo clippy.

Instead, this happened:

warning: this let-binding has unit value
  --> marshalable/src/lib.rs:15:5
   |
15 |     _b: (),
   |     ^^ help: omit the `let` binding: `_b;`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value
   = note: `#[warn(clippy::let_unit_value)]` on by default
warning: `marshalable` (lib) generated 1 warning (run `cargo clippy --fix --lib -p marshalable` to apply 1 suggestion)

Futhermore attempting `--fix causes clippy error:

warning: failed to automatically apply fixes suggested by rustc to crate `marshalable`

after fixes were automatically applied the compiler reported errors within these files:

  * marshalable/src/lib.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

But this might be a side effect.

Version

rustc 1.80.1 (3f5fd8dd4 2024-08-06)
binary: rustc
commit-hash: 3f5fd8dd41153bc5fdca9427e9e05be2c767ba23
commit-date: 2024-08-06
host: x86_64-unknown-linux-gnu
release: 1.80.1
LLVM version: 18.1.7

Additional Labels

No response

y21 commented 1 week ago

This is something that could be fixed on the clippy side, but FWIW, it would be better to just use the proper span APIs to begin with, i.e. use Span::call_site().located_at(field.span()) here:

https://github.com/oddcoder/clippy-macros-bug/blob/main/marshalable-derive/src/lib.rs#L40

That should also get clippy to stop emitting warnings there already, because the lint already does have macro checks.

When passing an input span directly to quote_spanned!, the spans have no indication that they're from a macro, so detecting these kinds of things require an additional check in the lint. It works as expected with quote! because that uses Span::call_site() for the let statement, which preserves the macro information