-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Add auto-fixable suggestion to remove unused imports #47888
Comments
I'm taking a shot at writing this - it's my first attempt at touching rustc so there's a 50/50 chance that anything will come of it :) |
My thinking on various edge cases: When we encounter multiple unused items, like so: warning: unused imports: `io`, `net`
--> src/main.rs:1:22
|
1 | use std::os::{unix::{io, net}, linux};
| ^^ ^^^
|
= note: #[warn(unused_imports)] on by default I'm assuming we want just a single suggestion with every unused item in this In this case, should it be How about if we would end up with warning: unused imports: `io`, `net`
--> src/main.rs:1:22
|
1 | use std::os::unix::{io, self};
| ^^ Should the (In both cases I would lean towards applying these simplifications). If we are making these simplifications, presumably it would be desirable to avoid touching parts of the tree that didn't have unused imports: warning: unused imports: `io`, `net`
--> src/main.rs:1:22
|
1 | use std::os::{unix::{io, self}, linux::{self}};
| ^^ Would become |
One roadblock encountered, for these squiggly bracket situations: [skippable backgound] There are two stages of unused import checking (documented here). The first and main stage (in resolve) doesn't always have the knowledge to determine conclusively that an imported trait is actually unused. These candidates are saved for the later stage in typeck (the one killercup linked above). A problem arises when an uncertain Trait import is alongside a concrete import in a single https://play.rust-lang.org/?gist=a08ec0cd799f258f2a1c371cc3ca2f4c&version=nightly warning: unused imports: `Bar`, `Foo`
--> src/main.rs:3:12
|
3 | use test::{Bar, Foo, Trait1, Trait2, C};
| ^^^ ^^^
|
= note: #[warn(unused_imports)] on by default
warning: unused import: `Trait2`
--> src/main.rs:3:30
|
3 | use test::{Bar, Foo, Trait1, Trait2, C};
| ^^^^^^ These problems are found at different times, thus they produce two different diagnostics. This means that we can't emit a single suggestion to resolve all these issues (unless we instead buffer all the early warnings until the second stage of checking and report the issues together). (The reason I am proposing emitting a single suggestion, aside from user-convenience, is so that we don't get conflicts applying two suggestions which both, say, remove a comma). For now I will attempt to write a version which does not touch the uncertain traits, leaving them as is in the |
Okay, I have a tested implementation here, which does not touch late-decided trait imports. I will have a look tomorrow at would will be necessary to cover those cases as well. |
@JJJollyjim Any interest in reviving this? |
Oh please revive it, unused imports are killing me |
@vors apparently someone else finished it |
I had been working on it still, I will have a look at @pietroalbini's code later to see if there's anything I can contribute from my versison |
Yeah, the problem at the moment is that cargo and rustfix won't accept more suggestions from the same error messages due to a corner case in diagnostics emitting, so it's not possible at all to fix most of the unused_import warnings. I'll try to get things moving at the All Hands this week. |
The issue about the corner case is #53934. |
Initial implementation of rustfixable unused_imports lint This PR adds the initial implementation of rustfixable `unused_imports` lint. The implementation works, but rustfix is not able to apply all the suggestions until #53934 is fixed. It also needs #58296 to hide the suggested note since it's really useless. cc #47888 <details><summary><code>cargo fix</code> in action on the <code>unused_imports</code> lint</summary> ![screenshot from 2018-12-09 15-49-01](https://user-images.githubusercontent.com/2299951/49698874-3a026080-fbca-11e8-9bf1-24060b6c59c8.png) </details>
Hit this today. |
Is there any progress on this issue? |
This is fixed by now, as evidenced by the test added at https://github.com/rust-lang/rust/pull/120305/files#diff-cc1a791dd6a817de22fa7682b0d02a9247d789ae615c849893375af8f3a1c805 |
Warnings like the following should contain information in their diagnostics output that allows rustfix to remove the unused import.
The strategy is to suggest replacing the unused import with an empty string. There are two cases:
use
statement unused (e.g.,std::fs
inuse std::fs;
is unused)? Then remove the wholeuse
statement.use
statement unused (e.g.,File
inuse std::fs::{File, copy};
is unused)? Remove only these items but keep theuse
statement.A quick search found this relevant file in typeck:
rust/src/librustc_typeck/check_unused.rs
Lines 27 to 46 in 7051754
Originally opened as https://github.com/killercup/rustfix/issues/56.
The text was updated successfully, but these errors were encountered: