Skip to content

Commit 2bd9418

Browse files
committed
Add identifier to unused import warnings
1 parent 7bd2427 commit 2bd9418

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

src/librustc_resolve/check_unused.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ impl<'a, 'b> UnusedImportCheckVisitor<'a, 'b> {
5959
// Check later.
6060
return;
6161
}
62-
self.session.add_lint(lint::builtin::UNUSED_IMPORTS,
63-
id,
64-
span,
65-
"unused import".to_string());
62+
let msg = if let Ok(snippet) = self.session.codemap().span_to_snippet(span) {
63+
format!("unused import: `{}`", snippet)
64+
} else {
65+
"unused import".to_string()
66+
};
67+
self.session.add_lint(lint::builtin::UNUSED_IMPORTS, id, span, msg);
6668
} else {
6769
// This trait import is definitely used, in a way other than
6870
// method resolution.

src/librustc_typeck/check_unused.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ impl<'a, 'tcx> UnusedTraitImportVisitor<'a, 'tcx> {
3030
if self.tcx.used_trait_imports.borrow().contains(&id) {
3131
return;
3232
}
33-
self.tcx.sess.add_lint(lint::builtin::UNUSED_IMPORTS,
34-
id,
35-
span,
36-
"unused import".to_string());
33+
34+
let msg = if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(span) {
35+
format!("unused import: `{}`", snippet)
36+
} else {
37+
"unused import".to_string()
38+
};
39+
self.tcx.sess.add_lint(lint::builtin::UNUSED_IMPORTS, id, span, msg);
3740
}
3841
}
3942

src/test/compile-fail/lint-unused-imports.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ use std::mem::*; // shouldn't get errors for not using
1717
// everything imported
1818

1919
// Should get errors for both 'Some' and 'None'
20-
use std::option::Option::{Some, None}; //~ ERROR unused import
21-
//~^ ERROR unused import
20+
use std::option::Option::{Some, None}; //~ ERROR unused import: `Some`
21+
//~^ ERROR unused import: `None`
2222

23-
use test::A; //~ ERROR unused import
23+
use test::A; //~ ERROR unused import: `test::A`
2424
// Be sure that if we just bring some methods into scope that they're also
2525
// counted as being used.
2626
use test::B;
2727
// But only when actually used: do not get confused by the method with the same name.
28-
use test::B2; //~ ERROR unused import
28+
use test::B2; //~ ERROR unused import: `test::B2`
2929

3030
// Make sure this import is warned about when at least one of its imported names
3131
// is unused
32-
use test2::{foo, bar}; //~ ERROR unused import
32+
use test2::{foo, bar}; //~ ERROR unused import: `bar`
3333

3434
mod test2 {
3535
pub fn foo() {}
@@ -57,7 +57,7 @@ mod bar {
5757

5858
pub mod c {
5959
use foo::Point;
60-
use foo::Square; //~ ERROR unused import
60+
use foo::Square; //~ ERROR unused import: `foo::Square`
6161
pub fn cc(_p: Point) -> super::Square {
6262
fn f() -> super::Square {
6363
super::Square
@@ -73,7 +73,7 @@ mod bar {
7373
}
7474

7575
fn g() {
76-
use self::g; //~ ERROR unused import
76+
use self::g; //~ ERROR unused import: `self::g`
7777
fn f() {
7878
self::g();
7979
}
@@ -82,7 +82,7 @@ fn g() {
8282
// c.f. issue #35135
8383
#[allow(unused_variables)]
8484
fn h() {
85-
use test2::foo; //~ ERROR unused import
85+
use test2::foo; //~ ERROR unused import: `test2::foo`
8686
let foo = 0;
8787
}
8888

0 commit comments

Comments
 (0)