Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A warning about redundant imports should be separate from unused_imports #121825

Closed
anforowicz opened this issue Mar 1, 2024 · 1 comment
Closed
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@anforowicz
Copy link
Contributor

anforowicz commented Mar 1, 2024

Problem

There is a set of related problems:

  • Because the same warning name covers both, it is impossible to ignore redundant imports, while still warning for unused imports.
  • It is difficult to author libraries that work with std and with no_std, because the prelude contents are different. In particular an unconditional use alloc::vec::Vec causes a warning (or error with -Dwarnings): error: the item Vec is imported redundantly

AFAIU the new warning has been introduced for issue #117448 in the changes here: #117772

This problem has been originally found in https://crbug.com/326247202. (In https://crbug.com/326247202#comment10 and https://crbug.com/326247202#comment11 we discussed disabling the new warning, but it doesn't seem possible without also disabling unused_imports.)

Steps

$ cat src/main.rs
mod my_library {
    #[derive(Debug)]
    pub struct Foo(pub i32);

    #[derive(Debug)]
    pub struct Bar(pub i32);
}

mod prelude_from_my_library {
    pub use super::my_library::Foo;

    // The `main` below compiles fine without the next `use`.
    // But adding reimport of `Bar` to the prelude will trigger
    // a redundant-import warning.
    //
    // Comment out the next line to simulate a previous version of the prelude
    // (where the warning doesn't trigger).
    pub use super::my_library::Bar;
}

use prelude_from_my_library::*;
use my_library::Bar;

fn main() {
    dbg!(Foo(123));
    dbg!(Bar(456));
}

$ cargo run
warning: unused import: `super::my_library::Bar`
  --> src/main.rs:15:13
   |
15 |     pub use super::my_library::Bar;
   |             ^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

warning: `redundant-import-repro` (bin "redundant-import-repro") generated 1 warning (run `cargo fix --bin "redundant-import-repro"` to apply 1 suggestion)
[...]

Possible Solution(s)

Maybe it would be helpful to have 2 separate warnings: the old unused_imports warning, and a separate redundant_imports warning?

Version

$ rustc --version --verbose
rustc 1.78.0-nightly (ee9c7c940 2024-02-14)
binary: rustc
commit-hash: ee9c7c940c07d8b67c9a6b2ec930db70dcd23a46
commit-date: 2024-02-14
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 1, 2024
@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 1, 2024
@petrochenkov
Copy link
Contributor

I'm going to close this to centralize the discussion in #121708.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants