Skip to content

Commit f4e981c

Browse files
committed
Auto merge of #53684 - alexcrichton:suggest-remove, r=oli-obk
rustc: Suggest removing `extern crate` in 2018 This commit updates the `unused_extern_crates` lint to make automatic suggestions about removing `extern crate` annotations in the 2018 edition. This ended up being a little easier than originally though due to what's likely been fixed issues in the resolver! Closes #52829
2 parents 29e6aab + 51fd3bf commit f4e981c

File tree

5 files changed

+128
-1
lines changed

5 files changed

+128
-1
lines changed

src/librustc_typeck/check_unused.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
176176
};
177177
let replacement = visibility_qualified(&item.vis, &base_replacement);
178178
tcx.struct_span_lint_node(lint, id, extern_crate.span, msg)
179-
.span_suggestion_short(extern_crate.span, &help, replacement)
179+
.span_suggestion_short_with_applicability(
180+
extern_crate.span,
181+
&help,
182+
replacement,
183+
Applicability::MachineApplicable,
184+
)
180185
.emit();
181186
}
182187
}
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+
#[macro_export]
12+
macro_rules! foo {
13+
() => ()
14+
}
15+
16+
#[macro_export]
17+
macro_rules! bar {
18+
() => ()
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
// run-rustfix
12+
// edition:2018
13+
// compile-pass
14+
// aux-build:remove-extern-crate.rs
15+
16+
#![warn(rust_2018_idioms)]
17+
18+
19+
use core as another_name;
20+
use remove_extern_crate;
21+
#[macro_use]
22+
extern crate remove_extern_crate as something_else;
23+
24+
fn main() {
25+
another_name::mem::drop(3);
26+
another::foo();
27+
remove_extern_crate::foo!();
28+
bar!();
29+
}
30+
31+
mod another {
32+
use core;
33+
use remove_extern_crate;
34+
35+
pub fn foo() {
36+
core::mem::drop(4);
37+
remove_extern_crate::foo!();
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
// run-rustfix
12+
// edition:2018
13+
// compile-pass
14+
// aux-build:remove-extern-crate.rs
15+
16+
#![warn(rust_2018_idioms)]
17+
18+
extern crate core;
19+
extern crate core as another_name;
20+
use remove_extern_crate;
21+
#[macro_use]
22+
extern crate remove_extern_crate as something_else;
23+
24+
fn main() {
25+
another_name::mem::drop(3);
26+
another::foo();
27+
remove_extern_crate::foo!();
28+
bar!();
29+
}
30+
31+
mod another {
32+
extern crate core;
33+
use remove_extern_crate;
34+
35+
pub fn foo() {
36+
core::mem::drop(4);
37+
remove_extern_crate::foo!();
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
warning: unused extern crate
2+
--> $DIR/remove-extern-crate.rs:18:1
3+
|
4+
LL | extern crate core;
5+
| ^^^^^^^^^^^^^^^^^^ help: remove it
6+
|
7+
note: lint level defined here
8+
--> $DIR/remove-extern-crate.rs:16:9
9+
|
10+
LL | #![warn(rust_2018_idioms)]
11+
| ^^^^^^^^^^^^^^^^
12+
= note: #[warn(unused_extern_crates)] implied by #[warn(rust_2018_idioms)]
13+
14+
warning: `extern crate` is not idiomatic in the new edition
15+
--> $DIR/remove-extern-crate.rs:19: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:32:5
22+
|
23+
LL | extern crate core;
24+
| ^^^^^^^^^^^^^^^^^^ help: convert it to a `use`
25+

0 commit comments

Comments
 (0)