-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Unused imports are too conservative #10178
Comments
Triage: This has not changed status, but would be very useful. |
Triage: no change, other than using |
Triage: no change. |
I would rather have a "redundant import" warning, because the import is technically used. Something like:
|
Started working on this. |
@fabric-and-ink Before looking into implementation we need to understand what it means for an import to be redundant from the language point of view. |
First, we assume that the import is used (otherwise it would already be reported). Used import is redundant if all its uses will reroute to some other name introduction (definition, import, Import
Right now we don't track whether items are used "module-relatively" or from current scope. Note however, that imports in nameless blocks can never be used module-relatively. If the import is not |
An import Instead of "every use" we can invent a single use located in the same position as the import in question and attempt to resolve it with In fact we already have this blacklisting mechanism (
|
Caveat: use something::*;
use something::Specific; is not always a redundant import due to "glob vs glob" ambiguities and restricted shadowing, and it's hard to detect the cases where it's actually not redundant. |
…petrochenkov Lint for redundant imports This is an attempt at solving rust-lang#10178. The changes are suggested by `@petrochenkov`. I need some feedback on how to continue, since I get a lot of false positives in macro invocations in `libcore`. I suspect that the test ``` ... if directive.parent_scope.expansion == Mark::root() { return; } ... ``` is wrong. But I don't know how to check correctly if I am at a macro expansion root – given that this is the reason for the errors in the first place. Todos: - [x] Solve problem with false positives - [x] Turn hard error into warning - [x] Add unit tests Closes rust-lang#10178
…petrochenkov Lint for redundant imports Add lint for redundant imports. The changes are suggested by @petrochenkov. Closes rust-lang#10178.
Right now that program has no unused import warnings, but the second one can be considered an unused import. The reason it's not unused is because it brings a name into the current scope, which is indeed then used. The catch is that the name it brings into scope is the exact same as what it was before.
It'd be nice if resolve caught this and basically didn't bring in any metadata about the inner
use foo::Bar
import (only because it's already in scope by a previous import).Not critical, but it would help us cut down on imports!
The text was updated successfully, but these errors were encountered: