Auto merge of #53479 - joshtriplett:underscore-means-unused, r=eddyb
Don't emit "unused extern crate" warnings for `extern crate foo as _;` When importing a crate and renaming it to an underscore-prefixed name, suppress "unused extern crate" warnings (but not idiom lints).
This commit is contained in:
commit
6244625cb4
3 changed files with 21 additions and 26 deletions
|
|
@ -133,19 +133,21 @@ 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_with_applicability(
|
||||
span,
|
||||
"remove it",
|
||||
String::new(),
|
||||
Applicability::MachineApplicable)
|
||||
.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_with_applicability(
|
||||
span,
|
||||
"remove it",
|
||||
String::new(),
|
||||
Applicability::MachineApplicable)
|
||||
.emit();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// If we are not in Rust 2018 edition, then we don't make any further
|
||||
|
|
@ -202,6 +204,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> {
|
||||
|
|
@ -213,6 +219,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)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue