Skip to content

Commit da79ff3

Browse files
alexcrichtonMark-Simulacrum
authored andcommitted
rustc: Only suggest deleting extern crate if it works
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!
1 parent 2885632 commit da79ff3

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

src/librustc_lint/builtin.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,9 @@ impl LintPass for ExternCrate {
15481548

15491549
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExternCrate {
15501550
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
1551+
if !cx.tcx.features().extern_absolute_paths {
1552+
return
1553+
}
15511554
if let hir::ItemExternCrate(ref orig) = it.node {
15521555
if it.attrs.iter().any(|a| a.check_name("macro_use")) {
15531556
return
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// intentionally empty
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:edition-extern-crate-allowed.rs
12+
// compile-flags: --edition 2015
13+
// compile-pass
14+
15+
#![deny(rust_2018_idioms)]
16+
17+
extern crate edition_extern_crate_allowed;
18+
19+
fn main() {}

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

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: --edition 2018
12+
1113
#![deny(unnecessary_extern_crate)]
1214
#![feature(alloc, test, libc)]
1315

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
11
error: `extern crate` is unnecessary in the new edition
2-
--> $DIR/unnecessary-extern-crate.rs:14:1
2+
--> $DIR/unnecessary-extern-crate.rs:16:1
33
|
44
LL | extern crate alloc;
55
| ^^^^^^^^^^^^^^^^^^^ help: remove it
66
|
77
note: lint level defined here
8-
--> $DIR/unnecessary-extern-crate.rs:11:9
8+
--> $DIR/unnecessary-extern-crate.rs:13:9
99
|
1010
LL | #![deny(unnecessary_extern_crate)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^
1212

1313
error: `extern crate` is unnecessary in the new edition
14-
--> $DIR/unnecessary-extern-crate.rs:17:1
14+
--> $DIR/unnecessary-extern-crate.rs:19:1
1515
|
1616
LL | extern crate alloc as x;
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `use`: `use alloc as x`
1818

1919
error: `extern crate` is unnecessary in the new edition
20-
--> $DIR/unnecessary-extern-crate.rs:23:1
20+
--> $DIR/unnecessary-extern-crate.rs:25:1
2121
|
2222
LL | pub extern crate test as y;
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `pub use`: `pub use test as y`
2424

2525
error: `extern crate` is unnecessary in the new edition
26-
--> $DIR/unnecessary-extern-crate.rs:26:1
26+
--> $DIR/unnecessary-extern-crate.rs:28:1
2727
|
2828
LL | pub extern crate libc;
2929
| ^^^^^^^^^^^^^^^^^^^^^^ help: use `pub use`: `pub use libc`
3030

3131
error: `extern crate` is unnecessary in the new edition
32-
--> $DIR/unnecessary-extern-crate.rs:32:5
32+
--> $DIR/unnecessary-extern-crate.rs:34:5
3333
|
3434
LL | extern crate alloc;
3535
| ^^^^^^^^^^^^^^^^^^^ help: use `use`: `use alloc`
3636

3737
error: `extern crate` is unnecessary in the new edition
38-
--> $DIR/unnecessary-extern-crate.rs:35:5
38+
--> $DIR/unnecessary-extern-crate.rs:37:5
3939
|
4040
LL | extern crate alloc as x;
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `use`: `use alloc as x`
4242

4343
error: `extern crate` is unnecessary in the new edition
44-
--> $DIR/unnecessary-extern-crate.rs:38:5
44+
--> $DIR/unnecessary-extern-crate.rs:40:5
4545
|
4646
LL | pub extern crate test;
4747
| ^^^^^^^^^^^^^^^^^^^^^^ help: use `pub use`: `pub use test`
4848

4949
error: `extern crate` is unnecessary in the new edition
50-
--> $DIR/unnecessary-extern-crate.rs:41:5
50+
--> $DIR/unnecessary-extern-crate.rs:43:5
5151
|
5252
LL | pub extern crate test as y;
5353
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `pub use`: `pub use test as y`
5454

5555
error: `extern crate` is unnecessary in the new edition
56-
--> $DIR/unnecessary-extern-crate.rs:45:9
56+
--> $DIR/unnecessary-extern-crate.rs:47:9
5757
|
5858
LL | extern crate alloc;
5959
| ^^^^^^^^^^^^^^^^^^^ help: use `use`: `use alloc`
6060

6161
error: `extern crate` is unnecessary in the new edition
62-
--> $DIR/unnecessary-extern-crate.rs:48:9
62+
--> $DIR/unnecessary-extern-crate.rs:50:9
6363
|
6464
LL | extern crate alloc as x;
6565
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `use`: `use alloc as x`

0 commit comments

Comments
 (0)