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

Lint against imports that are dead code #80443

Closed
CryZe opened this issue Dec 28, 2020 · 7 comments
Closed

Lint against imports that are dead code #80443

CryZe opened this issue Dec 28, 2020 · 7 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-resolve Area: Name/path resolution done by `rustc_resolve` specifically C-feature-request Category: A feature request, i.e: not implemented / a PR.

Comments

@CryZe
Copy link
Contributor

CryZe commented Dec 28, 2020

I'm very often seeing people write imports such as this one:
use regex;

Such an import is almost always dead code, because ever since Rust 2018 all external crates are already in scope. In fact pretty much any import that doesn't contain at least one of pub, as ... or :: is very likely dead code. Rust does not currently emit any warning for such an import.

@jonas-schievink
Copy link
Contributor

use regex; on the playground does emit a warning:

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

@CryZe
Copy link
Contributor Author

CryZe commented Dec 28, 2020

That's just the default warning of an unused import, not the specific case I'm talking about. Here's a proper reproduction:

use regex;

fn main() {
    regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
}

Playground

Here, the statement uses regex::Regex which works even without the import. So the import doesn't do anything, but there's no warning.

I've had multiple occurrences now where I had to teach beginners that this isn't necessary, because the compiler fails to point this out.

@jonas-schievink jonas-schievink added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-resolve Area: Name/path resolution done by `rustc_resolve` specifically labels Dec 28, 2020
@jyn514 jyn514 added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Dec 28, 2020
@ThePuzzlemaker
Copy link
Contributor

I think I'll take a shot at this. @rustbot claim

@ThePuzzlemaker
Copy link
Contributor

ThePuzzlemaker commented Jan 2, 2021

Should this be implemented as a new lint or just as an instance of dead_code? I personally think it'd be better as its own lint (something like useless_imports) as dead code is described in rustc_lint_defs as "detect unused, unexported items"

@petrochenkov
Copy link
Contributor

We already have a lint for redundant imports (#58805), it's a part of unused_imports, but it conservatively doesn't fire on imports in mod items.
The reason is that such imports can be used by module-relative paths like super::regex from a child module or crate::regex/self::regex from the current module, but we don't currently track such uses. See the linked PR and issue for more details.

@petrochenkov
Copy link
Contributor

This is also a duplicate of #61640.

@ThePuzzlemaker ThePuzzlemaker removed their assignment Jan 2, 2021
@rylev
Copy link
Member

rylev commented Jun 11, 2021

Triage: going to close this in favor of #61640.

@rylev rylev closed this as completed Jun 11, 2021
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. A-resolve Area: Name/path resolution done by `rustc_resolve` specifically C-feature-request Category: A feature request, i.e: not implemented / a PR.
Projects
None yet
Development

No branches or pull requests

6 participants