Skip to content

Commit 274ef46

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 786ccc3 + e2651be commit 274ef46

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

src/librustc_typeck/check_unused.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,17 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
132132

133133
// If the crate is fully unused, we suggest removing it altogether.
134134
// We do this in any edition.
135-
if let Some(&span) = unused_extern_crates.get(&extern_crate.def_id) {
136-
assert_eq!(extern_crate.def_id.krate, LOCAL_CRATE);
137-
let hir_id = tcx.hir.definitions().def_index_to_hir_id(extern_crate.def_id.index);
138-
let id = tcx.hir.hir_to_node_id(hir_id);
139-
let msg = "unused extern crate";
140-
tcx.struct_span_lint_node(lint, id, span, msg)
141-
.span_suggestion_short(span, "remove it", "".to_string())
142-
.emit();
143-
continue;
135+
if extern_crate.warn_if_unused {
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(span, "remove it", "".to_string())
143+
.emit();
144+
continue;
145+
}
144146
}
145147

146148
// If we are not in Rust 2018 edition, then we don't make any further
@@ -192,6 +194,10 @@ struct ExternCrateToLint {
192194
/// crate_name`), and -- perhaps surprisingly -- this stores the
193195
/// *original* name (`item.name` will contain the new name)
194196
orig_name: Option<ast::Name>,
197+
198+
/// if `false`, the original name started with `_`, so we shouldn't lint
199+
/// about it going unused (but we should still emit idiom lints).
200+
warn_if_unused: bool,
195201
}
196202

197203
impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for CollectExternCrateVisitor<'a, 'tcx> {
@@ -203,6 +209,7 @@ impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for CollectExternCrateVisitor<'a, 'tcx> {
203209
def_id: extern_crate_def_id,
204210
span: item.span,
205211
orig_name,
212+
warn_if_unused: !item.name.as_str().starts_with('_'),
206213
}
207214
);
208215
}

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 {

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)