-
Notifications
You must be signed in to change notification settings - Fork 14k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
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
usestatement unused (e.g.,std::fsinuse std::fs;is unused)? Then remove the wholeusestatement. - Are only some of the items imported by the
usestatement unused (e.g.,Fileinuse std::fs::{File, copy};is unused)? Remove only these items but keep theusestatement.
A quick search found this relevant file in typeck:
rust/src/librustc_typeck/check_unused.rs
Lines 27 to 46 in 7051754
| 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.
alexheretic, pum-purum-pum-pum, aymericbeaumet, untitaker, cdekok and 26 morebrunobertoldi, nielsle, mre, pum-purum-pum-pum, aymericbeaumet and 9 more
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.