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.
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:
usestatement unused (e.g.,std::fsinuse std::fs;is unused)? Then remove the wholeusestatement.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
Originally opened as https://github.com/killercup/rustfix/issues/56.