Skip to content

Commit 81ca8eb

Browse files
committed
rustc_typeck: don't lint non-extern-prelude extern crate's in Rust 2018.
1 parent e90985a commit 81ca8eb

File tree

6 files changed

+34
-88
lines changed

6 files changed

+34
-88
lines changed

src/librustc_typeck/check_unused.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,13 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
130130
});
131131

132132
for extern_crate in &crates_to_lint {
133-
assert!(extern_crate.def_id.is_local());
133+
let id = tcx.hir.as_local_node_id(extern_crate.def_id).unwrap();
134+
let item = tcx.hir.expect_item(id);
134135

135136
// If the crate is fully unused, we suggest removing it altogether.
136137
// We do this in any edition.
137138
if extern_crate.warn_if_unused {
138139
if let Some(&span) = unused_extern_crates.get(&extern_crate.def_id) {
139-
assert_eq!(extern_crate.def_id.krate, LOCAL_CRATE);
140-
let hir_id = tcx.hir.definitions().def_index_to_hir_id(extern_crate.def_id.index);
141-
let id = tcx.hir.hir_to_node_id(hir_id);
142140
let msg = "unused extern crate";
143141
tcx.struct_span_lint_node(lint, id, span, msg)
144142
.span_suggestion_short_with_applicability(
@@ -157,6 +155,13 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
157155
continue;
158156
}
159157

158+
// If the extern crate isn't in the extern prelude,
159+
// there is no way it can be written as an `use`.
160+
let orig_name = extern_crate.orig_name.unwrap_or(item.name);
161+
if !tcx.sess.extern_prelude.contains(&orig_name) {
162+
continue;
163+
}
164+
160165
// If the extern crate has any attributes, they may have funky
161166
// semantics we can't faithfully represent using `use` (most
162167
// notably `#[macro_use]`). Ignore it.
@@ -165,9 +170,6 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
165170
}
166171

167172
// Otherwise, we can convert it into a `use` of some kind.
168-
let hir_id = tcx.hir.definitions().def_index_to_hir_id(extern_crate.def_id.index);
169-
let id = tcx.hir.hir_to_node_id(hir_id);
170-
let item = tcx.hir.expect_item(id);
171173
let msg = "`extern crate` is not idiomatic in the new edition";
172174
let help = format!(
173175
"convert it to a `{}`",

src/test/ui-fulldeps/unnecessary-extern-crate.rs

+4-18
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,23 @@ extern crate alloc as x;
2020
//~^ ERROR unused extern crate
2121
//~| HELP remove
2222

23+
extern crate proc_macro;
24+
2325
#[macro_use]
2426
extern crate test;
2527

2628
pub extern crate test as y;
27-
//~^ ERROR `extern crate` is not idiomatic in the new edition
28-
//~| HELP convert it to a `pub use`
2929

3030
pub extern crate libc;
31-
//~^ ERROR `extern crate` is not idiomatic in the new edition
32-
//~| HELP convert it to a `pub use`
3331

3432
pub(crate) extern crate libc as a;
35-
//~^ ERROR `extern crate` is not idiomatic in the new edition
36-
//~| HELP convert it to a `pub(crate) use`
3733

3834
crate extern crate libc as b;
39-
//~^ ERROR `extern crate` is not idiomatic in the new edition
40-
//~| HELP convert it to a `crate use`
4135

4236
mod foo {
4337
pub(in crate::foo) extern crate libc as c;
44-
//~^ ERROR `extern crate` is not idiomatic in the new edition
45-
//~| HELP convert it to a `pub(in crate::foo) use`
4638

4739
pub(super) extern crate libc as d;
48-
//~^ ERROR `extern crate` is not idiomatic in the new edition
49-
//~| HELP convert it to a `pub(super) use`
5040

5141
extern crate alloc;
5242
//~^ ERROR unused extern crate
@@ -57,12 +47,8 @@ mod foo {
5747
//~| HELP remove
5848

5949
pub extern crate test;
60-
//~^ ERROR `extern crate` is not idiomatic in the new edition
61-
//~| HELP convert it
6250

6351
pub extern crate test as y;
64-
//~^ ERROR `extern crate` is not idiomatic in the new edition
65-
//~| HELP convert it
6652

6753
mod bar {
6854
extern crate alloc;
@@ -74,8 +60,6 @@ mod foo {
7460
//~| HELP remove
7561

7662
pub(in crate::foo::bar) extern crate libc as e;
77-
//~^ ERROR `extern crate` is not idiomatic in the new edition
78-
//~| HELP convert it to a `pub(in crate::foo::bar) use`
7963

8064
fn dummy() {
8165
unsafe {
@@ -96,4 +80,6 @@ mod foo {
9680
fn main() {
9781
unsafe { a::getpid(); }
9882
unsafe { b::getpid(); }
83+
84+
proc_macro::TokenStream::new();
9985
}

src/test/ui-fulldeps/unnecessary-extern-crate.stderr

+5-59
Original file line numberDiff line numberDiff line change
@@ -16,83 +16,29 @@ error: unused extern crate
1616
LL | extern crate alloc as x;
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
1818

19-
error: `extern crate` is not idiomatic in the new edition
20-
--> $DIR/unnecessary-extern-crate.rs:26:1
21-
|
22-
LL | pub extern crate test as y;
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub use`
24-
25-
error: `extern crate` is not idiomatic in the new edition
26-
--> $DIR/unnecessary-extern-crate.rs:30:1
27-
|
28-
LL | pub extern crate libc;
29-
| ^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub use`
30-
31-
error: `extern crate` is not idiomatic in the new edition
32-
--> $DIR/unnecessary-extern-crate.rs:34:1
33-
|
34-
LL | pub(crate) extern crate libc as a;
35-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub(crate) use`
36-
37-
error: `extern crate` is not idiomatic in the new edition
38-
--> $DIR/unnecessary-extern-crate.rs:38:1
39-
|
40-
LL | crate extern crate libc as b;
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `crate use`
42-
43-
error: `extern crate` is not idiomatic in the new edition
44-
--> $DIR/unnecessary-extern-crate.rs:43:5
45-
|
46-
LL | pub(in crate::foo) extern crate libc as c;
47-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub(in crate::foo) use`
48-
49-
error: `extern crate` is not idiomatic in the new edition
50-
--> $DIR/unnecessary-extern-crate.rs:47:5
51-
|
52-
LL | pub(super) extern crate libc as d;
53-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub(super) use`
54-
5519
error: unused extern crate
56-
--> $DIR/unnecessary-extern-crate.rs:51:5
20+
--> $DIR/unnecessary-extern-crate.rs:41:5
5721
|
5822
LL | extern crate alloc;
5923
| ^^^^^^^^^^^^^^^^^^^ help: remove it
6024

6125
error: unused extern crate
62-
--> $DIR/unnecessary-extern-crate.rs:55:5
26+
--> $DIR/unnecessary-extern-crate.rs:45:5
6327
|
6428
LL | extern crate alloc as x;
6529
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
6630

67-
error: `extern crate` is not idiomatic in the new edition
68-
--> $DIR/unnecessary-extern-crate.rs:59:5
69-
|
70-
LL | pub extern crate test;
71-
| ^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub use`
72-
73-
error: `extern crate` is not idiomatic in the new edition
74-
--> $DIR/unnecessary-extern-crate.rs:63:5
75-
|
76-
LL | pub extern crate test as y;
77-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub use`
78-
7931
error: unused extern crate
80-
--> $DIR/unnecessary-extern-crate.rs:68:9
32+
--> $DIR/unnecessary-extern-crate.rs:54:9
8133
|
8234
LL | extern crate alloc;
8335
| ^^^^^^^^^^^^^^^^^^^ help: remove it
8436

8537
error: unused extern crate
86-
--> $DIR/unnecessary-extern-crate.rs:72:9
38+
--> $DIR/unnecessary-extern-crate.rs:58:9
8739
|
8840
LL | extern crate alloc as x;
8941
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
9042

91-
error: `extern crate` is not idiomatic in the new edition
92-
--> $DIR/unnecessary-extern-crate.rs:76:9
93-
|
94-
LL | pub(in crate::foo::bar) extern crate libc as e;
95-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `pub(in crate::foo::bar) use`
96-
97-
error: aborting due to 15 previous errors
43+
error: aborting due to 6 previous errors
9844

src/test/ui/rust-2018/remove-extern-crate.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// aux-build:remove-extern-crate.rs
1515
// compile-flags:--extern remove_extern_crate
1616

17+
#![feature(alloc)]
1718
#![warn(rust_2018_idioms)]
1819

1920

@@ -22,11 +23,16 @@ use remove_extern_crate;
2223
#[macro_use]
2324
extern crate remove_extern_crate as something_else;
2425

26+
// Shouldn't suggest changing to `use`, as the `alloc`
27+
// crate is not in the extern prelude - see #54381.
28+
extern crate alloc;
29+
2530
fn main() {
2631
another_name::mem::drop(3);
2732
another::foo();
2833
remove_extern_crate::foo!();
2934
bar!();
35+
alloc::vec![5];
3036
}
3137

3238
mod another {

src/test/ui/rust-2018/remove-extern-crate.rs

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// aux-build:remove-extern-crate.rs
1515
// compile-flags:--extern remove_extern_crate
1616

17+
#![feature(alloc)]
1718
#![warn(rust_2018_idioms)]
1819

1920
extern crate core;
@@ -22,11 +23,16 @@ use remove_extern_crate;
2223
#[macro_use]
2324
extern crate remove_extern_crate as something_else;
2425

26+
// Shouldn't suggest changing to `use`, as the `alloc`
27+
// crate is not in the extern prelude - see #54381.
28+
extern crate alloc;
29+
2530
fn main() {
2631
another_name::mem::drop(3);
2732
another::foo();
2833
remove_extern_crate::foo!();
2934
bar!();
35+
alloc::vec![5];
3036
}
3137

3238
mod another {

src/test/ui/rust-2018/remove-extern-crate.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
warning: unused extern crate
2-
--> $DIR/remove-extern-crate.rs:19:1
2+
--> $DIR/remove-extern-crate.rs:20:1
33
|
44
LL | extern crate core;
55
| ^^^^^^^^^^^^^^^^^^ help: remove it
66
|
77
note: lint level defined here
8-
--> $DIR/remove-extern-crate.rs:17:9
8+
--> $DIR/remove-extern-crate.rs:18:9
99
|
1010
LL | #![warn(rust_2018_idioms)]
1111
| ^^^^^^^^^^^^^^^^
1212
= note: #[warn(unused_extern_crates)] implied by #[warn(rust_2018_idioms)]
1313

1414
warning: `extern crate` is not idiomatic in the new edition
15-
--> $DIR/remove-extern-crate.rs:20:1
15+
--> $DIR/remove-extern-crate.rs:21:1
1616
|
1717
LL | extern crate core as another_name;
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`
1919

2020
warning: `extern crate` is not idiomatic in the new edition
21-
--> $DIR/remove-extern-crate.rs:33:5
21+
--> $DIR/remove-extern-crate.rs:39:5
2222
|
2323
LL | extern crate core;
2424
| ^^^^^^^^^^^^^^^^^^ help: convert it to a `use`

0 commit comments

Comments
 (0)