Closed
Description
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.
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
jonas-schievink commentedon Dec 28, 2020
use regex;
on the playground does emit a warning:CryZe commentedon 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:
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.
ThePuzzlemaker commentedon Jan 2, 2021
I think I'll take a shot at this. @rustbot claim
ThePuzzlemaker commentedon 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 likeuseless_imports
) as dead code is described inrustc_lint_defs
as "detect unused, unexported items"petrochenkov commentedon Jan 2, 2021
We already have a lint for redundant imports (#58805), it's a part of
unused_imports
, but it conservatively doesn't fire on imports inmod
items.The reason is that such imports can be used by module-relative paths like
super::regex
from a child module orcrate::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 commentedon Jan 2, 2021
This is also a duplicate of #61640.
rylev commentedon Jun 11, 2021
Triage: going to close this in favor of #61640.