Skip to content

Commit 8869bc5

Browse files
committed
Do not suggest use over extern crate w/ alias.
This commit stops `unused_extern_crates` lints from occuring on `extern crate` statements that alias the crate as the suggestion to change to a `use` statement would result in the aliased name no longer being added to the prelude, thereby causing compilation errors if other imports expected this to be the case.
1 parent 8a47e08 commit 8869bc5

11 files changed

+41
-66
lines changed

src/librustc_typeck/check_unused.rs

+7
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
158158
continue;
159159
}
160160

161+
// If the extern crate is renamed, then we cannot suggest replacing it with a use as this
162+
// would not insert the new name into the prelude, where other imports in the crate may be
163+
// expecting it.
164+
if extern_crate.orig_name.is_some() {
165+
continue;
166+
}
167+
161168
// If the extern crate has any attributes, they may have funky
162169
// semantics we can't faithfully represent using `use` (most
163170
// notably `#[macro_use]`). Ignore it.

src/test/ui/imports/extern-crate-used.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55

66
#![deny(unused_extern_crates)]
77

8-
extern crate core as iso1; //~ ERROR `extern crate` is not idiomatic in the new edition
9-
extern crate core as iso2; //~ ERROR `extern crate` is not idiomatic in the new edition
10-
extern crate core as iso3; //~ ERROR `extern crate` is not idiomatic in the new edition
11-
extern crate core as iso4; //~ ERROR `extern crate` is not idiomatic in the new edition
8+
// Shouldn't suggest changing to `use`, as new name
9+
// would no longer be added to the prelude which could cause
10+
// compilation errors for imports that use the new name in
11+
// other modules. See #57672.
12+
extern crate core as iso1;
13+
extern crate core as iso2;
14+
extern crate core as iso3;
15+
extern crate core as iso4;
1216

1317
// Doesn't introduce its extern prelude entry, so it's still considered unused.
1418
extern crate core; //~ ERROR unused extern crate
+5-29
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,14 @@
1-
error: `extern crate` is not idiomatic in the new edition
2-
--> $DIR/extern-crate-used.rs:8:1
1+
error: unused extern crate
2+
--> $DIR/extern-crate-used.rs:18:1
33
|
4-
LL | extern crate core as iso1;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`
4+
LL | extern crate core;
5+
| ^^^^^^^^^^^^^^^^^^ help: remove it
66
|
77
note: lint level defined here
88
--> $DIR/extern-crate-used.rs:6:9
99
|
1010
LL | #![deny(unused_extern_crates)]
1111
| ^^^^^^^^^^^^^^^^^^^^
1212

13-
error: `extern crate` is not idiomatic in the new edition
14-
--> $DIR/extern-crate-used.rs:9:1
15-
|
16-
LL | extern crate core as iso2;
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`
18-
19-
error: `extern crate` is not idiomatic in the new edition
20-
--> $DIR/extern-crate-used.rs:10:1
21-
|
22-
LL | extern crate core as iso3;
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`
24-
25-
error: `extern crate` is not idiomatic in the new edition
26-
--> $DIR/extern-crate-used.rs:11:1
27-
|
28-
LL | extern crate core as iso4;
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`
30-
31-
error: unused extern crate
32-
--> $DIR/extern-crate-used.rs:14:1
33-
|
34-
LL | extern crate core;
35-
| ^^^^^^^^^^^^^^^^^^ help: remove it
36-
37-
error: aborting due to 5 previous errors
13+
error: aborting due to previous error
3814

src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212

1313
//~^ ERROR unused extern crate
1414

15-
use edition_lint_paths as bar;
16-
//~^ ERROR `extern crate` is not idiomatic in the new edition
15+
// Shouldn't suggest changing to `use`, as `bar`
16+
// would no longer be added to the prelude which could cause
17+
// compilation errors for imports that use `bar` in other
18+
// modules. See #57672.
19+
extern crate edition_lint_paths as bar;
1720

1821
fn main() {
1922
// This is not considered to *use* the `extern crate` in Rust 2018:

src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
extern crate edition_lint_paths;
1313
//~^ ERROR unused extern crate
1414

15+
// Shouldn't suggest changing to `use`, as `bar`
16+
// would no longer be added to the prelude which could cause
17+
// compilation errors for imports that use `bar` in other
18+
// modules. See #57672.
1519
extern crate edition_lint_paths as bar;
16-
//~^ ERROR `extern crate` is not idiomatic in the new edition
1720

1821
fn main() {
1922
// This is not considered to *use* the `extern crate` in Rust 2018:

src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,5 @@ LL | #![deny(rust_2018_idioms)]
1111
| ^^^^^^^^^^^^^^^^
1212
= note: #[deny(unused_extern_crates)] implied by #[deny(rust_2018_idioms)]
1313

14-
error: `extern crate` is not idiomatic in the new edition
15-
--> $DIR/extern-crate-idiomatic-in-2018.rs:15:1
16-
|
17-
LL | extern crate edition_lint_paths as bar;
18-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`
19-
20-
error: aborting due to 2 previous errors
14+
error: aborting due to previous error
2115

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
#![warn(rust_2018_idioms)]
88

99

10-
use core as another_name;
10+
// Shouldn't suggest changing to `use`, as `another_name`
11+
// would no longer be added to the prelude which could cause
12+
// compilation errors for imports that use `another_name` in other
13+
// modules. See #57672.
14+
extern crate core as another_name;
1115
use remove_extern_crate;
1216
#[macro_use]
1317
extern crate remove_extern_crate as something_else;

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

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#![warn(rust_2018_idioms)]
88

99
extern crate core;
10+
// Shouldn't suggest changing to `use`, as `another_name`
11+
// would no longer be added to the prelude which could cause
12+
// compilation errors for imports that use `another_name` in other
13+
// modules. See #57672.
1014
extern crate core as another_name;
1115
use remove_extern_crate;
1216
#[macro_use]

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

+1-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ LL | #![warn(rust_2018_idioms)]
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:10:1
16-
|
17-
LL | extern crate core as another_name;
18-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`
19-
20-
warning: `extern crate` is not idiomatic in the new edition
21-
--> $DIR/remove-extern-crate.rs:28:5
15+
--> $DIR/remove-extern-crate.rs:32:5
2216
|
2317
LL | extern crate core;
2418
| ^^^^^^^^^^^^^^^^^^ help: convert it to a `use`

src/test/ui/suggestions/issue-57672.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// aux-build:foo.rs
22
// compile-flags:--extern foo
3+
// compile-pass
34
// edition:2018
45

56
#![deny(unused_extern_crates)]
67

78
extern crate foo as foo_renamed;
8-
//~^ ERROR `extern crate` is not idiomatic in the new edition
99

1010
pub mod m {
1111
pub use foo_renamed::Foo;

src/test/ui/suggestions/issue-57672.stderr

-14
This file was deleted.

0 commit comments

Comments
 (0)