Skip to content

Lint against imports that are dead code #80443

Closed
@CryZe

Description

@CryZe
Contributor

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.

Activity

jonas-schievink

jonas-schievink commented on Dec 28, 2020

@jonas-schievink
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

CryZe commented on Dec 28, 2020

@CryZe
ContributorAuthor

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.

added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
A-resolveArea: Name/path resolution done by `rustc_resolve` specifically
on Dec 28, 2020
added
C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
on Dec 28, 2020
ThePuzzlemaker

ThePuzzlemaker commented on Jan 2, 2021

@ThePuzzlemaker
Contributor

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

ThePuzzlemaker

ThePuzzlemaker commented on Jan 2, 2021

@ThePuzzlemaker
Contributor

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

petrochenkov commented on Jan 2, 2021

@petrochenkov
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

petrochenkov commented on Jan 2, 2021

@petrochenkov
Contributor

This is also a duplicate of #61640.

removed their assignment
on Jan 2, 2021
rylev

rylev commented on Jun 11, 2021

@rylev
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-resolveArea: Name/path resolution done by `rustc_resolve` specificallyC-feature-requestCategory: A feature request, i.e: not implemented / a PR.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @rylev@CryZe@jonas-schievink@petrochenkov@ThePuzzlemaker

        Issue actions

          Lint against imports that are dead code · Issue #80443 · rust-lang/rust