Skip to content

Don't emit "unused extern crate" warnings for extern crate foo as _; #53479

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

Merged
merged 1 commit into from
Aug 30, 2018
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
33 changes: 20 additions & 13 deletions src/librustc_typeck/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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> {
Expand All @@ -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('_'),
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/rfc-2166-underscore-imports/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we add a test triggering the warning?

}

mod outer {
Expand Down
12 changes: 0 additions & 12 deletions src/test/ui/rfc-2166-underscore-imports/basic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
| ^^^^^^^^^^^^^^^^^^^^