Skip to content

Commit 6244625

Browse files
committed
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).
2 parents 02cb8f2 + 7cec516 commit 6244625

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

Diff for: src/librustc_typeck/check_unused.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,21 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
133133

134134
// If the crate is fully unused, we suggest removing it altogether.
135135
// We do this in any edition.
136-
if let Some(&span) = unused_extern_crates.get(&extern_crate.def_id) {
137-
assert_eq!(extern_crate.def_id.krate, LOCAL_CRATE);
138-
let hir_id = tcx.hir.definitions().def_index_to_hir_id(extern_crate.def_id.index);
139-
let id = tcx.hir.hir_to_node_id(hir_id);
140-
let msg = "unused extern crate";
141-
tcx.struct_span_lint_node(lint, id, span, msg)
142-
.span_suggestion_short_with_applicability(
143-
span,
144-
"remove it",
145-
String::new(),
146-
Applicability::MachineApplicable)
147-
.emit();
148-
continue;
136+
if extern_crate.warn_if_unused {
137+
if let Some(&span) = unused_extern_crates.get(&extern_crate.def_id) {
138+
assert_eq!(extern_crate.def_id.krate, LOCAL_CRATE);
139+
let hir_id = tcx.hir.definitions().def_index_to_hir_id(extern_crate.def_id.index);
140+
let id = tcx.hir.hir_to_node_id(hir_id);
141+
let msg = "unused extern crate";
142+
tcx.struct_span_lint_node(lint, id, span, msg)
143+
.span_suggestion_short_with_applicability(
144+
span,
145+
"remove it",
146+
String::new(),
147+
Applicability::MachineApplicable)
148+
.emit();
149+
continue;
150+
}
149151
}
150152

151153
// If we are not in Rust 2018 edition, then we don't make any further
@@ -202,6 +204,10 @@ struct ExternCrateToLint {
202204
/// crate_name`), and -- perhaps surprisingly -- this stores the
203205
/// *original* name (`item.name` will contain the new name)
204206
orig_name: Option<ast::Name>,
207+
208+
/// if `false`, the original name started with `_`, so we shouldn't lint
209+
/// about it going unused (but we should still emit idiom lints).
210+
warn_if_unused: bool,
205211
}
206212

207213
impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for CollectExternCrateVisitor<'a, 'tcx> {
@@ -213,6 +219,7 @@ impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for CollectExternCrateVisitor<'a, 'tcx> {
213219
def_id: extern_crate_def_id,
214220
span: item.span,
215221
orig_name,
222+
warn_if_unused: !item.name.as_str().starts_with('_'),
216223
}
217224
);
218225
}

Diff for: src/test/ui/rfc-2166-underscore-imports/basic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ mod m {
3030
mod unused {
3131
use m::Tr1 as _; //~ WARN unused import
3232
use S as _; //~ WARN unused import
33-
extern crate core as _; //~ WARN unused extern crate
33+
extern crate core as _; // OK
3434
}
3535

3636
mod outer {

Diff for: src/test/ui/rfc-2166-underscore-imports/basic.stderr

-12
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,3 @@ warning: unused import: `S as _`
1616
LL | use S as _; //~ WARN unused import
1717
| ^^^^^^
1818

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

0 commit comments

Comments
 (0)