]> git.lizzy.rs Git - rust.git/commitdiff
Do not suggest use over extern crate w/ alias.
authorDavid Wood <david@davidtw.co>
Thu, 25 Apr 2019 07:06:49 +0000 (08:06 +0100)
committerDavid Wood <david@davidtw.co>
Thu, 25 Apr 2019 07:06:49 +0000 (08:06 +0100)
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.

src/librustc_typeck/check_unused.rs
src/test/ui/imports/extern-crate-used.rs
src/test/ui/imports/extern-crate-used.stderr
src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed
src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.rs
src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.stderr
src/test/ui/rust-2018/remove-extern-crate.fixed
src/test/ui/rust-2018/remove-extern-crate.rs
src/test/ui/rust-2018/remove-extern-crate.stderr
src/test/ui/suggestions/issue-57672.rs
src/test/ui/suggestions/issue-57672.stderr [deleted file]

index cbb6d9b29f59f7f642588ffbe18ae3e0e18d7d22..3c3509f24ce1eb97000433d6245363ec14f114dd 100644 (file)
@@ -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;
+        }
+
         // 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.
index 2d91cbc00f27d492bb11387582a0bd4b829c35c4..26150c7d4a16b77de5f4a76803006e4b48e3bf24 100644 (file)
@@ -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
index c0783feb7947d2366c5b0a51d11d6b140609023e..397bfa02902c94df7528429b07a8e60dfd17e853 100644 (file)
@@ -1,8 +1,8 @@
-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
@@ -10,29 +10,5 @@ note: lint level defined here
 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
 
index ddd9abbd50ed8abed5a4853db71aeb8c2c9e5199..e51ce5d1d5b83e48bfc48e65be4538175ec9178b 100644 (file)
 
 //~^ 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:
index 47674bc19fc044614791f543f522f0d3005ae953..debbf085d618293d1ed901a4e535cf6f21414fbc 100644 (file)
 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:
index f3f9193948667173bc6d68a756c239cf7333f928..13980c70a82aa219082d4271dd59b17112c8f498 100644 (file)
@@ -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
 
index 17449caf84fb41ea835e42a5c56b9378ca93d0f9..14575d18c236aa3275b669bdaae93c6bd1f1c887 100644 (file)
@@ -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;
index fb2217df0005d4e8c7e250a54cea76473d8ea222..0ee85f34e40d99c98bbdb9bbc1649af77bcec8b5 100644 (file)
@@ -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]
index 549693201b70346a426bb61cb7a10723abcf3716..5de0dfe961338701d99e17fa1cfac2b5d848cf08 100644 (file)
@@ -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`
index c8ea6e59582f674a7f8439bbc0b42426cc0f9566..1773f72fc741c4476a7507f3d20b31abacaf83ea 100644 (file)
@@ -1,11 +1,11 @@
 // aux-build:foo.rs
 // compile-flags:--extern foo
+// compile-pass
 // edition:2018
 
 #![deny(unused_extern_crates)]
 
 extern crate foo as foo_renamed;
-//~^ ERROR `extern crate` is not idiomatic in the new edition
 
 pub mod m {
     pub use foo_renamed::Foo;
diff --git a/src/test/ui/suggestions/issue-57672.stderr b/src/test/ui/suggestions/issue-57672.stderr
deleted file mode 100644 (file)
index c9e4c37..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-error: `extern crate` is not idiomatic in the new edition
-  --> $DIR/issue-57672.rs:7:1
-   |
-LL | extern crate foo as foo_renamed;
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`
-   |
-note: lint level defined here
-  --> $DIR/issue-57672.rs:5:9
-   |
-LL | #![deny(unused_extern_crates)]
-   |         ^^^^^^^^^^^^^^^^^^^^
-
-error: aborting due to previous error
-