Showing with 17 additions and 22 deletions.
  1. +16 −9 src/librustc_typeck/check_unused.rs
  2. +1 −1 src/test/ui/rfc-2166-underscore-imports/basic.rs
  3. +0 −12 src/test/ui/rfc-2166-underscore-imports/basic.stderr
@@ -132,15 +132,17 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {

// If the crate is fully unused, we suggest removing it altogether.
// We do this in any edition.
if let Some(&span) = unused_extern_crates.get(&extern_crate.def_id) {
assert_eq!(extern_crate.def_id.krate, LOCAL_CRATE);
let hir_id = tcx.hir.definitions().def_index_to_hir_id(extern_crate.def_id.index);
let id = tcx.hir.hir_to_node_id(hir_id);
let msg = "unused extern crate";
tcx.struct_span_lint_node(lint, id, span, msg)
.span_suggestion_short(span, "remove it", "".to_string())
.emit();
continue;
if extern_crate.warn_if_unused {
if let Some(&span) = unused_extern_crates.get(&extern_crate.def_id) {
assert_eq!(extern_crate.def_id.krate, LOCAL_CRATE);
let hir_id = tcx.hir.definitions().def_index_to_hir_id(extern_crate.def_id.index);
let id = tcx.hir.hir_to_node_id(hir_id);
let msg = "unused extern crate";
tcx.struct_span_lint_node(lint, id, span, msg)
.span_suggestion_short(span, "remove it", "".to_string())
.emit();
continue;
}
}

// If we are not in Rust 2018 edition, then we don't make any further
@@ -192,6 +194,10 @@ struct ExternCrateToLint {
/// crate_name`), and -- perhaps surprisingly -- this stores the
/// *original* name (`item.name` will contain the new name)
orig_name: Option<ast::Name>,

/// if `false`, the original name started with `_`, so we shouldn't lint
/// about it going unused (but we should still emit idiom lints).
warn_if_unused: bool,
}

impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for CollectExternCrateVisitor<'a, 'tcx> {
@@ -203,6 +209,7 @@ impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for CollectExternCrateVisitor<'a, 'tcx> {
def_id: extern_crate_def_id,
span: item.span,
orig_name,
warn_if_unused: !item.name.as_str().starts_with('_'),
}
);
}
@@ -30,7 +30,7 @@ mod m {
mod unused {
use m::Tr1 as _; //~ WARN unused import
use S as _; //~ WARN unused import
extern crate core as _; //~ WARN unused extern crate
extern crate core as _; // OK
}

mod outer {
@@ -16,15 +16,3 @@ warning: unused import: `S as _`
LL | use S as _; //~ WARN unused import
| ^^^^^^

warning: unused extern crate
--> $DIR/basic.rs:33:5
|
LL | extern crate core as _; //~ WARN unused extern crate
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
|
note: lint level defined here
--> $DIR/basic.rs:14:25
|
LL | #![warn(unused_imports, unused_extern_crates)]
| ^^^^^^^^^^^^^^^^^^^^