rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.86k stars 12.67k forks source link

unused import false positive with macro rules macro #130570

Closed ijackson closed 1 month ago

ijackson commented 1 month ago

Steps

git clone https://gitlab.torproject.org/Diziet/arti
cd arti
git checkout origin/report-unused-import-false-positive~0
cargo check -p tor-socksproto --locked

Current output

warning: unused import: `derive_deftly_template_Handshake`
   --> crates/tor-socksproto/src/handshake/framework.rs:440:5
    |
440 | use derive_deftly_template_Handshake; // for rustdoc's benefit
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: `#[warn(unused_imports)]` on by default

warning: `tor-socksproto` (lib) generated 1 warning (run `cargo fix --lib -p tor-socksproto` to apply 1 suggestion)

Desired output

No warning

Rationale and extra context

use on the name of a macro_rules macro is useful to give the macro path scope. In this case, the macro ends up being used by a rustdoc.

Other cases

To show that the warning is incorrect:

git checkout origin/report-unused-import-false-positive~1
cargo doc -p tor-socksproto --locked --all-features --document-private-items --no-deps

which produces

warning: unresolved link to `derive_deftly_template_Handshake`
  --> crates/tor-socksproto/src/handshake/framework.rs:10:29
   |
10 | //!  * Derive [`Handshake`](derive_deftly_template_Handshake).
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no item named `derive_deftly_template_Handshake` in scope
   |
   = note: `macro_rules` named `derive_deftly_template_Handshake` exists in this crate, but it is not in scope at this link's location
   = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default

Rust Version

rustc 1.81.0-beta.6 (b5fd9f6f1 2024-08-21)
binary: rustc
commit-hash: b5fd9f6f1061b79c045cc08fe03e00caad536800
commit-date: 2024-08-21
host: x86_64-unknown-linux-gnu
release: 1.81.0-beta.6
LLVM version: 18.1.7

Anything else?

clippy also complains about this; see https://github.com/rust-lang/rust-clippy/issues/13419

clubby789 commented 1 month ago

Seems like the same as #79542. I would argue this isn't really a false positive. You can import for rustdoc only by adding a #[cfg(doc)] to the import which resolves the warning.

jieyouxu commented 1 month ago

Closing as this is a "true" positive in the sense that rustc cannot know that the imports are used by doc comments via rustdoc. #[cfg(doc)] is one possible way to handle this. This also isn't actionable in the general case because rustc can't pull in rustdoc machinery to handle the doc usages.