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 identifier to unused import warnings #37394

Merged
merged 1 commit into from
Oct 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/librustc_resolve/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ impl<'a, 'b> UnusedImportCheckVisitor<'a, 'b> {
// Check later.
return;
}
self.session.add_lint(lint::builtin::UNUSED_IMPORTS,
id,
span,
"unused import".to_string());
let msg = if let Ok(snippet) = self.session.codemap().span_to_snippet(span) {
format!("unused import: `{}`", snippet)
} else {
"unused import".to_string()
};
self.session.add_lint(lint::builtin::UNUSED_IMPORTS, id, span, msg);
} else {
// This trait import is definitely used, in a way other than
// method resolution.
Expand Down
11 changes: 7 additions & 4 deletions src/librustc_typeck/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ impl<'a, 'tcx> UnusedTraitImportVisitor<'a, 'tcx> {
if self.tcx.used_trait_imports.borrow().contains(&id) {
return;
}
self.tcx.sess.add_lint(lint::builtin::UNUSED_IMPORTS,
id,
span,
"unused import".to_string());

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.sess.add_lint(lint::builtin::UNUSED_IMPORTS, id, span, msg);
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/test/compile-fail/lint-unused-imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ use std::mem::*; // shouldn't get errors for not using
// everything imported

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

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

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

mod test2 {
pub fn foo() {}
Expand Down Expand Up @@ -57,7 +57,7 @@ mod bar {

pub mod c {
use foo::Point;
use foo::Square; //~ ERROR unused import
use foo::Square; //~ ERROR unused import: `foo::Square`
pub fn cc(_p: Point) -> super::Square {
fn f() -> super::Square {
super::Square
Expand All @@ -73,7 +73,7 @@ mod bar {
}

fn g() {
use self::g; //~ ERROR unused import
use self::g; //~ ERROR unused import: `self::g`
fn f() {
self::g();
}
Expand All @@ -82,7 +82,7 @@ fn g() {
// c.f. issue #35135
#[allow(unused_variables)]
fn h() {
use test2::foo; //~ ERROR unused import
use test2::foo; //~ ERROR unused import: `test2::foo`
let foo = 0;
}

Expand Down