rust-lang / rust-lang/rust

redundant-imports lint does not work in macro-defined modules

Open
#158,400 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug needs-triage
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

Similar to https://github.com/rust-lang/rust/issues/158371, I found that the redundant_imports lint does not work in modules that are defined in any macros. This is especially relevant since this also affects cfg_if! and cfg_select! which are often used to define modules (e.g., https://github.com/rust-lang/rust/pull/158252).

Interestingly, other lints seem to work just fine, so this seems specific to the work that redundant_imports needs to do to analyze imports.

Reproduction

lib.rs:

#![deny(redundant_imports)]

#[cfg(true)]
pub mod foo; // works

macro_rules! identity {
    ($($tt:tt)*) => {
        $($tt)*
    };
}

identity! {
    // pub mod foo; // does not work
}

cfg_if::cfg_if! {
    if #[cfg(true)] {
        // pub mod foo; //does not work
    }
}

cfg_select! {
    _ => {
        // pub mod foo; // does not work
    }
}

foo.rs:

use std::option::Option::None;
pub fn foo() -> Option<i32> { None }

Command:

cargo check

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the lib.rs and foo.rs reproduction and run cargo check to confirm the redundant_imports behavior in a normal module versus macro-defined modules. Trace the redundant_imports lint handling for macro-expanded modules; done means the lint reports the redundant import consistently in the identity!, cfg_if!, and cfg_select! cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.