]> git.lizzy.rs Git - rust.git/commitdiff
rustc: Only suggest deleting `extern crate` if it works
authorAlex Crichton <alex@alexcrichton.com>
Fri, 11 May 2018 18:31:08 +0000 (11:31 -0700)
committerMark Simulacrum <mark.simulacrum@gmail.com>
Sat, 12 May 2018 14:39:05 +0000 (08:39 -0600)
This commit updates one of the edition lints to only suggest deleting `extern
crate` if it actually works. Otherwise this can yield some confusing behavior
with rustfix specifically where if you accidentally deny the `rust_2018_idioms`
lint in the 2015 edition it's suggesting features that don't work!

src/librustc_lint/builtin.rs
src/test/compile-fail/auxiliary/edition-extern-crate-allowed.rs [new file with mode: 0644]
src/test/compile-fail/edition-extern-crate-allowed.rs [new file with mode: 0644]
src/test/ui-fulldeps/unnecessary-extern-crate.rs
src/test/ui-fulldeps/unnecessary-extern-crate.stderr

index 251b95a6fcb7933ac484cb9d613113ef8421f6a3..0fb78b9f1b81d9166a78c009693a240b3ee168c4 100644 (file)
@@ -1548,6 +1548,9 @@ fn get_lints(&self) -> LintArray {
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExternCrate {
     fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
+        if !cx.tcx.features().extern_absolute_paths {
+            return
+        }
         if let hir::ItemExternCrate(ref orig) =  it.node {
             if it.attrs.iter().any(|a| a.check_name("macro_use")) {
                 return
diff --git a/src/test/compile-fail/auxiliary/edition-extern-crate-allowed.rs b/src/test/compile-fail/auxiliary/edition-extern-crate-allowed.rs
new file mode 100644 (file)
index 0000000..d26ab6d
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// intentionally empty
diff --git a/src/test/compile-fail/edition-extern-crate-allowed.rs b/src/test/compile-fail/edition-extern-crate-allowed.rs
new file mode 100644 (file)
index 0000000..286ee89
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// aux-build:edition-extern-crate-allowed.rs
+// compile-flags: --edition 2015
+// compile-pass
+
+#![deny(rust_2018_idioms)]
+
+extern crate edition_extern_crate_allowed;
+
+fn main() {}
index 9d678d91578bc1027db26cba228d1a80489178ae..0f532d3da6a7307ad1f25993bb5705ee3be751c4 100644 (file)
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// compile-flags: --edition 2018
+
 #![deny(unnecessary_extern_crate)]
 #![feature(alloc, test, libc)]
 
index 7718808be58baea95df41a1088ea3bc83028fecd..b0b56f527e64f68c695c5a0141104de6ea91c75f 100644 (file)
@@ -1,65 +1,65 @@
 error: `extern crate` is unnecessary in the new edition
-  --> $DIR/unnecessary-extern-crate.rs:14:1
+  --> $DIR/unnecessary-extern-crate.rs:16:1
    |
 LL | extern crate alloc;
    | ^^^^^^^^^^^^^^^^^^^ help: remove it
    |
 note: lint level defined here
-  --> $DIR/unnecessary-extern-crate.rs:11:9
+  --> $DIR/unnecessary-extern-crate.rs:13:9
    |
 LL | #![deny(unnecessary_extern_crate)]
    |         ^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: `extern crate` is unnecessary in the new edition
-  --> $DIR/unnecessary-extern-crate.rs:17:1
+  --> $DIR/unnecessary-extern-crate.rs:19:1
    |
 LL | extern crate alloc as x;
    | ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `use`: `use alloc as x`
 
 error: `extern crate` is unnecessary in the new edition
-  --> $DIR/unnecessary-extern-crate.rs:23:1
+  --> $DIR/unnecessary-extern-crate.rs:25:1
    |
 LL | pub extern crate test as y;
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `pub use`: `pub use test as y`
 
 error: `extern crate` is unnecessary in the new edition
-  --> $DIR/unnecessary-extern-crate.rs:26:1
+  --> $DIR/unnecessary-extern-crate.rs:28:1
    |
 LL | pub extern crate libc;
    | ^^^^^^^^^^^^^^^^^^^^^^ help: use `pub use`: `pub use libc`
 
 error: `extern crate` is unnecessary in the new edition
-  --> $DIR/unnecessary-extern-crate.rs:32:5
+  --> $DIR/unnecessary-extern-crate.rs:34:5
    |
 LL |     extern crate alloc;
    |     ^^^^^^^^^^^^^^^^^^^ help: use `use`: `use alloc`
 
 error: `extern crate` is unnecessary in the new edition
-  --> $DIR/unnecessary-extern-crate.rs:35:5
+  --> $DIR/unnecessary-extern-crate.rs:37:5
    |
 LL |     extern crate alloc as x;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `use`: `use alloc as x`
 
 error: `extern crate` is unnecessary in the new edition
-  --> $DIR/unnecessary-extern-crate.rs:38:5
+  --> $DIR/unnecessary-extern-crate.rs:40:5
    |
 LL |     pub extern crate test;
    |     ^^^^^^^^^^^^^^^^^^^^^^ help: use `pub use`: `pub use test`
 
 error: `extern crate` is unnecessary in the new edition
-  --> $DIR/unnecessary-extern-crate.rs:41:5
+  --> $DIR/unnecessary-extern-crate.rs:43:5
    |
 LL |     pub extern crate test as y;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `pub use`: `pub use test as y`
 
 error: `extern crate` is unnecessary in the new edition
-  --> $DIR/unnecessary-extern-crate.rs:45:9
+  --> $DIR/unnecessary-extern-crate.rs:47:9
    |
 LL |         extern crate alloc;
    |         ^^^^^^^^^^^^^^^^^^^ help: use `use`: `use alloc`
 
 error: `extern crate` is unnecessary in the new edition
-  --> $DIR/unnecessary-extern-crate.rs:48:9
+  --> $DIR/unnecessary-extern-crate.rs:50:9
    |
 LL |         extern crate alloc as x;
    |         ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `use`: `use alloc as x`