For example
use std::mem::drop;
Should give a warning, because drop is in the std prelude and it's already in scope.
use std::io::prelude::*;
use std::io::Write;
Should also give a warning, because Write is in io's prelude and is already imported.
I would like to tackle this one, but I'm not sure about the interactions with the existing WILDCARD_IMPORTS lint.
I guess it would be coherent to only trigger this lint for prelude imports? Also, what would be the category of this lint? I've seen other A-unnecessary lints classified under complexity, but maybe this one should be pedantic as it kind of overlaps with WILDCARD_IMPORTS?
An overlap of these lints shouldn't matter that much, since WILDCARD_IMPORTS is pedantic. I would put this lint in style or complexity.
Someone correct me if I'm wrong, but I think this is not doable currently due to ty::TyCtxt::names_imported_by_glob_use only returning names actually imported by the glob use.
If there is a non-glob import introducing the name, that seems to get precedence, and there is no way to know from the lint if the glob also could have introduced that same name.
Is this a problem though? if there is an import use foo::* and an import use foo::bar, the bar is either used and gets returned by ty::TyCtxt::names_imported_by_glob_use or it is not used and the unused_imports lint will trigger. Or does it not get returned if there is a non-glob import?
It seems to be the latter (does not get returned if there is a non-glob import).
With this code...
use std::io::prelude::*;
use std::io::Write;
struct S {}
impl Write for S {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
Ok(42)
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
Adding a dbg!() call on the returned value from
cx.tcx.names_imported_by_glob_use(item.hir_id.owner.to_def_id());
I get:
[clippy_lints/src/redundant_imports.rs:43] &used_imports = {}
If I comment out the non-glob import, I get:
[clippy_lints/src/redundant_imports.rs:43] &used_imports = {
"Write",
}
Or yeah, in that case, this is really limited/hard to implement.
I feel then this is a bit out of my league for now, I will leave it so someone else can address it.
Most helpful comment
An overlap of these lints shouldn't matter that much, since
WILDCARD_IMPORTSispedantic. I would put this lint instyleorcomplexity.