Warnings like the following should contain information in their diagnostics output that allows rustfix to remove the unused import.
warning: unused import: `std::io::prelude::*`
--> src/tiso/tiso_msg.rs:10:5
|
10 | use std::io::prelude::*;
| ^^^^^^^^^^^^^^^^^^^
warning: unused import: `std::fmt`
--> src/tiso/tiso_msg.rs:13:5
|
13 | use std::fmt;
| ^^^^^^^^
The strategy is to suggest replacing the unused import with an empty string. There are two cases:
use statement unused (e.g., std::fs in use std::fs; is unused)? Then remove the whole use statement.use statement unused (e.g., File in use std::fs::{File, copy}; is unused)? Remove only these items but keep the use statement.A quick search found this relevant file in typeck:
Originally opened as https://github.com/killercup/rustfix/issues/56.
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 use tree removed.
In this case, should it be use std::os::{linux}; or use std::os::linux;?
How about if we would end up with unix::{self}/unix::self?
warning: unused imports: `io`, `net`
--> src/main.rs:1:22
|
1 | use std::os::unix::{io, self};
| ^^
Should the self component be removed from the path?
(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 use std::os::{unix, linux::{self}}; rather than use std::os::{unix, linux};
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 use:
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 use.
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 https://github.com/rust-lang/rust/issues/53934.
Most helpful comment
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.