Skip to content
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

Closed
killercup opened this issue Jan 30, 2018 · 13 comments
Closed

Add auto-fixable suggestion to remove unused imports #47888

killercup opened this issue Jan 30, 2018 · 13 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@killercup
Copy link
Member

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:

  • Are all items in the use statement unused (e.g., std::fs in use std::fs; is unused)? Then remove the whole use statement.
  • Are only some of the items imported by the 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:

impl<'a, 'tcx> CheckVisitor<'a, 'tcx> {
fn check_import(&self, id: ast::NodeId, span: Span) {
let def_id = self.tcx.hir.local_def_id(id);
if !self.tcx.maybe_unused_trait_import(def_id) {
return;
}
let import_def_id = self.tcx.hir.local_def_id(id);
if self.used_trait_imports.contains(&import_def_id) {
return;
}
let msg = if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(span) {
format!("unused import: `{}`", snippet)
} else {
"unused import".to_string()
};
self.tcx.lint_node(lint::builtin::UNUSED_IMPORTS, id, span, &msg);
}
}

Originally opened as https://github.com/killercup/rustfix/issues/56.

@gsollazzo gsollazzo added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 1, 2018
@JJJollyjim
Copy link

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 :)

@JJJollyjim
Copy link

JJJollyjim commented Mar 3, 2018

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};

@JJJollyjim
Copy link

JJJollyjim commented Mar 3, 2018

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.

@JJJollyjim
Copy link

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.

@euclio
Copy link
Contributor

euclio commented Dec 7, 2018

@JJJollyjim Any interest in reviving this?

@vors
Copy link

vors commented Feb 2, 2019

Oh please revive it, unused imports are killing me

@JJJollyjim
Copy link

@vors apparently someone else finished it

@JJJollyjim
Copy link

JJJollyjim commented Feb 3, 2019

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

@pietroalbini
Copy link
Member

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.

@pietroalbini
Copy link
Member

The issue about the corner case is #53934.

bors added a commit that referenced this issue Feb 11, 2019
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>
@steveklabnik
Copy link
Member

Hit this today.

@xue8
Copy link

xue8 commented Mar 20, 2022

Is there any progress on this issue?

@estebank
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

9 participants