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

Feature request: warn about redundant imports #5416

Closed
PonasKovas opened this issue Apr 4, 2020 · 10 comments
Closed

Feature request: warn about redundant imports #5416

PonasKovas opened this issue Apr 4, 2020 · 10 comments
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-unnecessary Lint: Warn about unnecessary code

Comments

@PonasKovas
Copy link

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.

@flip1995 flip1995 added L-unnecessary Lint: Warn about unnecessary code E-medium Call for participation: Medium difficulty level problem and requires some initial experience. A-lint Area: New lints labels Apr 4, 2020
@ebroto
Copy link
Member

ebroto commented Apr 21, 2020

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?

@flip1995
Copy link
Member

An overlap of these lints shouldn't matter that much, since WILDCARD_IMPORTS is pedantic. I would put this lint in style or complexity.

@ebroto
Copy link
Member

ebroto commented Apr 24, 2020

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.

@flip1995
Copy link
Member

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?

@ebroto
Copy link
Member

ebroto commented Apr 25, 2020

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",
}

@flip1995
Copy link
Member

Or yeah, in that case, this is really limited/hard to implement.

@ebroto
Copy link
Member

ebroto commented Apr 26, 2020

I feel then this is a bit out of my league for now, I will leave it so someone else can address it.

@mmagician
Copy link

I believe this is already implemented.
E.g. the slightly modified code above:

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(())
    }
}

impl Read for S {
    fn read(&mut self, _buf: &mut [u8]) -> std::io::Result<usize> {
        Ok(42)
    }
}

results in:

warning: the item `Write` is imported redundantly
 --> src/main.rs:2:5
  |
1 | use std::io::prelude::*;
  |     ------------------- the item `Write` is already imported here
2 | use std::io::Write;
  |     ^^^^^^^^^^^^^^

What is missing is the ability to automatically --fix these. Is this on the roadmap?

@y21
Copy link
Member

y21 commented Mar 26, 2024

This is implemented as a lint in the compiler now, so there's nothing that can be done from the clippy side at this point.
The feature request for a machine applicable suggestion (--fixable) is tracked over at rust-lang/rust#121315

@y21
Copy link
Member

y21 commented Mar 27, 2024

Closing, since this doesn't really seem actionable in clippy anymore (see #5416 (comment) and #5416 (comment)), all the reproducers listed here are covered by unused_imports

@y21 y21 closed this as completed Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-unnecessary Lint: Warn about unnecessary code
Projects
None yet
Development

No branches or pull requests

5 participants