Closed
Description
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 withno_std
, because the prelude contents are different. In particular an unconditionaluse alloc::vec::Vec
causes a warning (or error with-Dwarnings
):error: the item
Vecis 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