Skip to content

Commit ddb8dda

Browse files
Introduce REDUNDANT_IMPORTS lint
1 parent 19d0226 commit ddb8dda

26 files changed

+323
-55
lines changed

compiler/rustc_lint_defs/src/builtin.rs

+24
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ declare_lint_pass! {
7979
PROC_MACRO_BACK_COMPAT,
8080
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
8181
PUB_USE_OF_PRIVATE_EXTERN_CRATE,
82+
REDUNDANT_IMPORTS,
8283
REDUNDANT_LIFETIMES,
8384
REFINING_IMPL_TRAIT_INTERNAL,
8485
REFINING_IMPL_TRAIT_REACHABLE,
@@ -419,6 +420,29 @@ declare_lint! {
419420
"imports that are never used"
420421
}
421422

423+
declare_lint! {
424+
/// The `unused_imports` lint detects imports that are redundant due to being
425+
/// imported already; either through a previous import, or being present in
426+
/// the prelude.
427+
///
428+
/// ### Example
429+
///
430+
/// ```rust
431+
/// use std::option::Option::None;
432+
/// ```
433+
///
434+
/// {{produces}}
435+
///
436+
/// ### Explanation
437+
///
438+
/// Redundant imports are unnecessary and can be removed to simplify code.
439+
/// If you intended to re-export the item to make it available outside of the
440+
/// module, add a visibility modifier like `pub`.
441+
pub REDUNDANT_IMPORTS,
442+
Allow,
443+
"imports that are redundant due to being imported already"
444+
}
445+
422446
declare_lint! {
423447
/// The `must_not_suspend` lint guards against values that shouldn't be held across suspend points
424448
/// (`.await`)

compiler/rustc_resolve/src/imports.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_middle::span_bug;
2626
use rustc_middle::ty;
2727
use rustc_session::lint::builtin::{
2828
AMBIGUOUS_GLOB_REEXPORTS, HIDDEN_GLOB_REEXPORTS, PUB_USE_OF_PRIVATE_EXTERN_CRATE,
29-
UNUSED_IMPORTS,
29+
REDUNDANT_IMPORTS, UNUSED_IMPORTS,
3030
};
3131
use rustc_session::lint::BuiltinLintDiag;
3232
use rustc_span::edit_distance::find_best_match_for_name;
@@ -1391,15 +1391,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13911391
let mut redundant_spans: Vec<_> = redundant_span.present_items().collect();
13921392
redundant_spans.sort();
13931393
redundant_spans.dedup();
1394-
/* FIXME(unused_imports): Add this back as a new lint
13951394
self.lint_buffer.buffer_lint_with_diagnostic(
1396-
UNUSED_IMPORTS,
1395+
REDUNDANT_IMPORTS,
13971396
id,
13981397
import.span,
13991398
format!("the item `{source}` is imported redundantly"),
14001399
BuiltinLintDiag::RedundantImport(redundant_spans, source),
14011400
);
1402-
*/
14031401
return true;
14041402
}
14051403

Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
//@ check-pass
21
// Check that we detect imports that are redundant due to the extern prelude
32
// and that we emit a reasonable diagnostic.
43
// issue: rust-lang/rust#121915
4+
//~^^^ NOTE the item `aux_issue_121915` is already defined by the extern prelude
55

66
// See also the discussion in <https://github.com/rust-lang/rust/pull/122954>.
77

88
//@ compile-flags: --extern aux_issue_121915 --edition 2018
99
//@ aux-build: aux-issue-121915.rs
1010

11-
#[deny(unused_imports)]
11+
#[deny(redundant_imports)]
12+
//~^ NOTE the lint level is defined here
1213
fn main() {
1314
use aux_issue_121915;
14-
//FIXME(unused_imports): ~^ ERROR the item `aux_issue_121915` is imported redundantly
15+
//~^ ERROR the item `aux_issue_121915` is imported redundantly
1516
aux_issue_121915::item();
1617
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: the item `aux_issue_121915` is imported redundantly
2+
--> $DIR/redundant-import-extern-prelude.rs:14:9
3+
|
4+
LL | use aux_issue_121915;
5+
| ^^^^^^^^^^^^^^^^ the item `aux_issue_121915` is already defined by the extern prelude
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/redundant-import-extern-prelude.rs:11:8
9+
|
10+
LL | #[deny(redundant_imports)]
11+
| ^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
//@ check-pass
21
//@ compile-flags: --extern aux_issue_121915 --edition 2015
32
//@ aux-build: aux-issue-121915.rs
43

54
extern crate aux_issue_121915;
65

7-
#[deny(unused_imports)]
6+
#[deny(redundant_imports)]
87
fn main() {
98
use aux_issue_121915;
10-
//FIXME(unused_imports): ~^ ERROR the item `aux_issue_121915` is imported redundantly
9+
//~^ ERROR the item `aux_issue_121915` is imported redundantly
1110
aux_issue_121915::item();
1211
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: the item `aux_issue_121915` is imported redundantly
2+
--> $DIR/redundant-import-issue-121915-2015.rs:8:9
3+
|
4+
LL | extern crate aux_issue_121915;
5+
| ------------------------------ the item `aux_issue_121915` is already imported here
6+
...
7+
LL | use aux_issue_121915;
8+
| ^^^^^^^^^^^^^^^^
9+
|
10+
note: the lint level is defined here
11+
--> $DIR/redundant-import-issue-121915-2015.rs:6:8
12+
|
13+
LL | #[deny(redundant_imports)]
14+
| ^^^^^^^^^^^^^^^^^
15+
16+
error: aborting due to 1 previous error
17+
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//@ check-pass
21
// Check that we detect imports (of built-in attributes) that are redundant due to
32
// the language prelude and that we emit a reasonable diagnostic.
3+
//~^^ NOTE the item `allow` is already defined by the extern prelude
44

55
// Note that we use the term "extern prelude" in the label even though "language prelude"
66
// would be more correct. However, it's not worth special-casing this.
@@ -9,9 +9,10 @@
99

1010
//@ edition: 2018
1111

12-
#![deny(unused_imports)]
12+
#![deny(redundant_imports)]
13+
//~^ NOTE the lint level is defined here
1314

14-
use allow; //FIXME(unused_imports): ~ ERROR the item `allow` is imported redundantly
15+
use allow; //~ ERROR the item `allow` is imported redundantly
1516

1617
#[allow(unused)]
1718
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: the item `allow` is imported redundantly
2+
--> $DIR/redundant-import-lang-prelude-attr.rs:15:5
3+
|
4+
LL | use allow;
5+
| ^^^^^ the item `allow` is already defined by the extern prelude
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/redundant-import-lang-prelude-attr.rs:12:9
9+
|
10+
LL | #![deny(redundant_imports)]
11+
| ^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+

tests/ui/imports/redundant-import-lang-prelude.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
//@ check-pass
21
// Check that we detect imports that are redundant due to the language prelude
32
// and that we emit a reasonable diagnostic.
3+
//~^^ NOTE the item `u8` is already defined by the extern prelude
44

55
// Note that we use the term "extern prelude" in the label even though "language prelude"
66
// would be more correct. However, it's not worth special-casing this.
77

88
// See also the discussion in <https://github.com/rust-lang/rust/pull/122954>.
99

10-
#![deny(unused_imports)]
10+
#![deny(redundant_imports)]
11+
//~^ NOTE the lint level is defined here
1112

1213
use std::primitive::u8;
13-
//FIXME(unused_imports): ~^ ERROR the item `u8` is imported redundantly
14+
//~^ ERROR the item `u8` is imported redundantly
1415

1516
const _: u8 = 0;
1617

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: the item `u8` is imported redundantly
2+
--> $DIR/redundant-import-lang-prelude.rs:13:5
3+
|
4+
LL | use std::primitive::u8;
5+
| ^^^^^^^^^^^^^^^^^^ the item `u8` is already defined by the extern prelude
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/redundant-import-lang-prelude.rs:10:9
9+
|
10+
LL | #![deny(redundant_imports)]
11+
| ^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+

tests/ui/imports/suggest-remove-issue-121315.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
//@ compile-flags: --edition 2021
2-
3-
#![deny(unused_imports)]
2+
#![deny(unused_imports, redundant_imports)]
43
#![allow(dead_code)]
54

65
fn test0() {
76
// Test remove FlatUnused
87
use std::convert::TryFrom;
9-
//FIXME(unused_imports): ~^ ERROR the item `TryFrom` is imported redundantly
8+
//~^ ERROR the item `TryFrom` is imported redundantly
109
let _ = u32::try_from(5i32);
1110
}
1211

1312
fn test1() {
1413
// FIXME(yukang) Test remove NestedFullUnused
1514
use std::convert::{TryFrom, TryInto};
16-
//FIXME(unused_imports): ~^ ERROR the item `TryFrom` is imported redundantly
17-
//FIXME(unused_imports): ~| ERROR the item `TryInto` is imported redundantly
15+
//~^ ERROR the item `TryFrom` is imported redundantly
16+
//~| ERROR the item `TryInto` is imported redundantly
1817

1918
let _ = u32::try_from(5i32);
2019
let _a: i32 = u32::try_into(5u32).unwrap();
@@ -24,7 +23,7 @@ fn test2() {
2423
// FIXME(yukang): Test remove both redundant and unused
2524
use std::convert::{AsMut, Into};
2625
//~^ ERROR unused import: `AsMut`
27-
//FIXME(unused_imports): ~| ERROR the item `Into` is imported redundantly
26+
//~| ERROR the item `Into` is imported redundantly
2827

2928
let _a: u32 = (5u8).into();
3029
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,62 @@
1+
error: the item `TryFrom` is imported redundantly
2+
--> $DIR/suggest-remove-issue-121315.rs:7:9
3+
|
4+
LL | use std::convert::TryFrom;
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
7+
|
8+
= note: the item `TryFrom` is already defined here
9+
|
10+
note: the lint level is defined here
11+
--> $DIR/suggest-remove-issue-121315.rs:2:25
12+
|
13+
LL | #![deny(unused_imports, redundant_imports)]
14+
| ^^^^^^^^^^^^^^^^^
15+
16+
error: the item `TryFrom` is imported redundantly
17+
--> $DIR/suggest-remove-issue-121315.rs:14:24
18+
|
19+
LL | use std::convert::{TryFrom, TryInto};
20+
| ^^^^^^^
21+
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
22+
|
23+
= note: the item `TryFrom` is already defined here
24+
25+
error: the item `TryInto` is imported redundantly
26+
--> $DIR/suggest-remove-issue-121315.rs:14:33
27+
|
28+
LL | use std::convert::{TryFrom, TryInto};
29+
| ^^^^^^^
30+
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
31+
|
32+
= note: the item `TryInto` is already defined here
33+
134
error: unused import: `AsMut`
2-
--> $DIR/suggest-remove-issue-121315.rs:25:24
35+
--> $DIR/suggest-remove-issue-121315.rs:24:24
336
|
437
LL | use std::convert::{AsMut, Into};
538
| ^^^^^
639
|
740
note: the lint level is defined here
8-
--> $DIR/suggest-remove-issue-121315.rs:3:9
41+
--> $DIR/suggest-remove-issue-121315.rs:2:9
942
|
10-
LL | #![deny(unused_imports)]
43+
LL | #![deny(unused_imports, redundant_imports)]
1144
| ^^^^^^^^^^^^^^
1245

46+
error: the item `Into` is imported redundantly
47+
--> $DIR/suggest-remove-issue-121315.rs:24:31
48+
|
49+
LL | use std::convert::{AsMut, Into};
50+
| ^^^^
51+
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
52+
|
53+
= note: the item `Into` is already defined here
54+
1355
error: unused import: `From`
14-
--> $DIR/suggest-remove-issue-121315.rs:34:24
56+
--> $DIR/suggest-remove-issue-121315.rs:33:24
1557
|
1658
LL | use std::convert::{From, Infallible};
1759
| ^^^^
1860

19-
error: aborting due to 2 previous errors
61+
error: aborting due to 6 previous errors
2062

tests/ui/lint/unused/issue-59896.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
//@ check-pass
2-
#![deny(unused_imports)]
1+
#![deny(redundant_imports)]
32

43
struct S;
54

65
fn main() {
7-
use S; //FIXME(unused_imports): ~ ERROR the item `S` is imported redundantly
6+
use S; //~ ERROR the item `S` is imported redundantly
87

98
let _s = S;
109
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: the item `S` is imported redundantly
2+
--> $DIR/issue-59896.rs:6:9
3+
|
4+
LL | struct S;
5+
| --------- the item `S` is already defined here
6+
...
7+
LL | use S;
8+
| ^
9+
|
10+
note: the lint level is defined here
11+
--> $DIR/issue-59896.rs:1:9
12+
|
13+
LL | #![deny(redundant_imports)]
14+
| ^^^^^^^^^^^^^^^^^
15+
16+
error: aborting due to 1 previous error
17+

tests/ui/lint/use-redundant/use-redundant-glob-parent.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ check-pass
2-
#![warn(unused_imports)]
2+
#![warn(redundant_imports)]
33

44
pub mod bar {
55
pub struct Foo(pub Bar);
@@ -9,7 +9,7 @@ pub mod bar {
99
use bar::*;
1010

1111
pub fn warning() -> Foo {
12-
use bar::Foo; //FIXME(unused_imports): ~ WARNING imported redundantly
12+
use bar::Foo; //~ WARNING imported redundantly
1313
Foo(Bar('a'))
1414
}
1515

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: the item `Foo` is imported redundantly
2+
--> $DIR/use-redundant-glob-parent.rs:12:9
3+
|
4+
LL | use bar::*;
5+
| ------ the item `Foo` is already imported here
6+
...
7+
LL | use bar::Foo;
8+
| ^^^^^^^^
9+
|
10+
note: the lint level is defined here
11+
--> $DIR/use-redundant-glob-parent.rs:2:9
12+
|
13+
LL | #![warn(redundant_imports)]
14+
| ^^^^^^^^^^^^^^^^^
15+
16+
warning: 1 warning emitted
17+

tests/ui/lint/use-redundant/use-redundant-glob.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ check-pass
2-
#![warn(unused_imports)]
2+
#![warn(redundant_imports)]
33

44
pub mod bar {
55
pub struct Foo(pub Bar);
@@ -8,7 +8,7 @@ pub mod bar {
88

99
pub fn warning() -> bar::Foo {
1010
use bar::*;
11-
use bar::Foo; //FIXME(unused_imports): ~ WARNING imported redundantly
11+
use bar::Foo; //~ WARNING imported redundantly
1212
Foo(Bar('a'))
1313
}
1414

0 commit comments

Comments
 (0)