Skip to content

Commit d5010ec

Browse files
committed
extend unused_extern_crates lint with a suggestion to remove
1 parent 577a5b2 commit d5010ec

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

src/librustc_typeck/check_unused.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
109109
let id = tcx.hir.hir_to_node_id(hir_id);
110110
let lint = lint::builtin::UNUSED_EXTERN_CRATES;
111111
let msg = "unused extern crate";
112-
tcx.lint_node(lint, id, span, msg);
112+
tcx.struct_span_lint_node(lint, id, span, msg)
113+
.span_suggestion_short(span, "remove it", "".to_string())
114+
.emit();
113115
}
114116
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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-lint-paths.rs
12+
// run-rustfix
13+
// compile-flags:--edition 2018
14+
15+
// The "normal case". Ideally we would remove the `extern crate` here,
16+
// but we don't.
17+
18+
#![feature(rust_2018_preview)]
19+
#![deny(absolute_path_not_starting_with_crate)]
20+
#![deny(unused_extern_crates)]
21+
#![allow(dead_code)]
22+
23+
24+
//~^ ERROR unused extern crate
25+
26+
extern crate edition_lint_paths as bar;
27+
28+
fn main() {
29+
// This is not considered to *use* the `extern crate` in Rust 2018:
30+
use edition_lint_paths::foo;
31+
foo();
32+
33+
// But this should be a use of the (renamed) crate:
34+
crate::bar::foo();
35+
}
36+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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-lint-paths.rs
12+
// run-rustfix
13+
// compile-flags:--edition 2018
14+
15+
// The "normal case". Ideally we would remove the `extern crate` here,
16+
// but we don't.
17+
18+
#![feature(rust_2018_preview)]
19+
#![deny(absolute_path_not_starting_with_crate)]
20+
#![deny(unused_extern_crates)]
21+
#![allow(dead_code)]
22+
23+
extern crate edition_lint_paths;
24+
//~^ ERROR unused extern crate
25+
26+
extern crate edition_lint_paths as bar;
27+
28+
fn main() {
29+
// This is not considered to *use* the `extern crate` in Rust 2018:
30+
use edition_lint_paths::foo;
31+
foo();
32+
33+
// But this should be a use of the (renamed) crate:
34+
crate::bar::foo();
35+
}
36+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: unused extern crate
2+
--> $DIR/extern-crate-idiomatic-in-2018.rs:23:1
3+
|
4+
LL | extern crate edition_lint_paths;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
6+
|
7+
note: lint level defined here
8+
--> $DIR/extern-crate-idiomatic-in-2018.rs:20:9
9+
|
10+
LL | #![deny(unused_extern_crates)]
11+
| ^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)