Skip to content

Don't suggest changing extern crate w/ alias to use. #60252

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No 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? No Sign in to your account

Merged
merged 2 commits into from
May 18, 2019
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
7 changes: 7 additions & 0 deletions src/librustc_typeck/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
continue;
}

// If the extern crate is renamed, then we cannot suggest replacing it with a use as this
// would not insert the new name into the prelude, where other imports in the crate may be
// expecting it.
if extern_crate.orig_name.is_some() {
continue;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I would have liked to come up with a more robust check here that would only omit the lint if there was an instance of the extern crate alias being used from the prelude, but I couldn't work out a nice way to achieve this.

Copy link
Member

Choose a reason for hiding this comment

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

Personally that doesn't seem necessary, and somewhat out of scope for this lint -- that seems more like an "unused extern crate" lint rather than an idiom lint.


// If the extern crate has any attributes, they may have funky
// semantics we can't faithfully represent using `use` (most
// notably `#[macro_use]`). Ignore it.
Expand Down
12 changes: 8 additions & 4 deletions src/test/ui/imports/extern-crate-used.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@

#![deny(unused_extern_crates)]

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

// Doesn't introduce its extern prelude entry, so it's still considered unused.
extern crate core; //~ ERROR unused extern crate
Expand Down
34 changes: 5 additions & 29 deletions src/test/ui/imports/extern-crate-used.stderr
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
error: `extern crate` is not idiomatic in the new edition
--> $DIR/extern-crate-used.rs:8:1
error: unused extern crate
--> $DIR/extern-crate-used.rs:18:1
|
LL | extern crate core as iso1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`
LL | extern crate core;
| ^^^^^^^^^^^^^^^^^^ help: remove it
|
note: lint level defined here
--> $DIR/extern-crate-used.rs:6:9
|
LL | #![deny(unused_extern_crates)]
| ^^^^^^^^^^^^^^^^^^^^

error: `extern crate` is not idiomatic in the new edition
--> $DIR/extern-crate-used.rs:9:1
|
LL | extern crate core as iso2;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`

error: `extern crate` is not idiomatic in the new edition
--> $DIR/extern-crate-used.rs:10:1
|
LL | extern crate core as iso3;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`

error: `extern crate` is not idiomatic in the new edition
--> $DIR/extern-crate-used.rs:11:1
|
LL | extern crate core as iso4;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`

error: unused extern crate
--> $DIR/extern-crate-used.rs:14:1
|
LL | extern crate core;
| ^^^^^^^^^^^^^^^^^^ help: remove it

error: aborting due to 5 previous errors
error: aborting due to previous error

7 changes: 5 additions & 2 deletions src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@

//~^ ERROR unused extern crate

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

fn main() {
// This is not considered to *use* the `extern crate` in Rust 2018:
Expand Down
5 changes: 4 additions & 1 deletion src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
extern crate edition_lint_paths;
//~^ ERROR unused extern crate

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

fn main() {
// This is not considered to *use* the `extern crate` in Rust 2018:
Expand Down
8 changes: 1 addition & 7 deletions src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,5 @@ LL | #![deny(rust_2018_idioms)]
| ^^^^^^^^^^^^^^^^
= note: #[deny(unused_extern_crates)] implied by #[deny(rust_2018_idioms)]

error: `extern crate` is not idiomatic in the new edition
--> $DIR/extern-crate-idiomatic-in-2018.rs:15:1
|
LL | extern crate edition_lint_paths as bar;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`

error: aborting due to 2 previous errors
error: aborting due to previous error

6 changes: 5 additions & 1 deletion src/test/ui/rust-2018/remove-extern-crate.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
#![warn(rust_2018_idioms)]


use core as another_name;
// Shouldn't suggest changing to `use`, as `another_name`
// would no longer be added to the prelude which could cause
// compilation errors for imports that use `another_name` in other
// modules. See #57672.
extern crate core as another_name;
use remove_extern_crate;
#[macro_use]
extern crate remove_extern_crate as something_else;
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/rust-2018/remove-extern-crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#![warn(rust_2018_idioms)]

extern crate core;
// Shouldn't suggest changing to `use`, as `another_name`
// would no longer be added to the prelude which could cause
// compilation errors for imports that use `another_name` in other
// modules. See #57672.
extern crate core as another_name;
use remove_extern_crate;
#[macro_use]
Expand Down
8 changes: 1 addition & 7 deletions src/test/ui/rust-2018/remove-extern-crate.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ LL | #![warn(rust_2018_idioms)]
= note: #[warn(unused_extern_crates)] implied by #[warn(rust_2018_idioms)]

warning: `extern crate` is not idiomatic in the new edition
--> $DIR/remove-extern-crate.rs:10:1
|
LL | extern crate core as another_name;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`

warning: `extern crate` is not idiomatic in the new edition
--> $DIR/remove-extern-crate.rs:28:5
--> $DIR/remove-extern-crate.rs:32:5
|
LL | extern crate core;
| ^^^^^^^^^^^^^^^^^^ help: convert it to a `use`
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/suggestions/issue-57672.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// aux-build:foo.rs
// compile-flags:--extern foo
// compile-pass
// edition:2018

#![deny(unused_extern_crates)]

extern crate foo as foo_renamed;

pub mod m {
pub use foo_renamed::Foo;
}

fn main() {}