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

Warn about useless imports #68177

Closed
CryZe opened this issue Jan 13, 2020 · 4 comments
Closed

Warn about useless imports #68177

CryZe opened this issue Jan 13, 2020 · 4 comments
Labels
A-edition-2018-lints Area: lints supporting the 2018 edition A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@CryZe
Copy link
Contributor

CryZe commented Jan 13, 2020

I'm seeing quite a lot of people do the following:

use some_crate;

However due to uniform paths, this doesn't do anything, as the crate is already accessible like that in the current module. This neither disambiguates nor does it have any meaningful effect, yet Rust does not emit a warning.

All of this only applies to uniform paths. So in Rust 2015 this actually is a meaningful import.

@mati865
Copy link
Contributor

mati865 commented Jan 13, 2020

Can you provide better example?
Works as expected for:

use rand;

pub fn foo() {}

Warning:

warning: unused import: `rand`
 --> src/lib.rs:1:5
  |
1 | use rand;
  |     ^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

@jonas-schievink jonas-schievink added A-edition-2018-lints Area: lints supporting the 2018 edition A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Jan 13, 2020
@CryZe
Copy link
Contributor Author

CryZe commented Jan 13, 2020

Yours is the warning about rand not being used at all, which is different from using rand, but importing it anyway when you don't need to.

Here's an example with std on the playground

use std;

fn main() {
    std::iter::once(5);
}

The use std; there doesn't do anything, as std::iter::once(5) works regardless, as std is already accessible in the module. Yet there is no warning.

@CryZe
Copy link
Contributor Author

CryZe commented Jan 13, 2020

Here is where I most recently encountered this in the wild: wasmerio/wasmer#1140

There they have code that roughly looks like this:

use blake3;

fn main() {
    let mut hasher = blake3::Hasher::new();
    ...
}

They use blake3; despite blake3::Hasher::new() working just fine without the use blake3;.

I've seen people make this mistake quite often by now, as this used to be required in Rust 2015 but is now completely redundant in Rust 2018.

@petrochenkov
Copy link
Contributor

This is a duplicate of #61640 and #10178 (which contains some implementation hints), and the lint was partially implemented in #58805.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edition-2018-lints Area: lints supporting the 2018 edition A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants