Skip to content

Commit 114154c

Browse files
Introduce REDUNDANT_IMPORTS lint
1 parent 0db2a40 commit 114154c

27 files changed

+325
-57
lines changed

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 24 additions & 0 deletions
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

Lines changed: 2 additions & 4 deletions
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

compiler/rustc_resolve/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ enum ImplTraitContext {
177177

178178
/// Used for tracking import use types which will be used for redundant import checking.
179179
/// ### Used::Scope Example
180-
/// ```rust,ignore (redundant_imports)
181-
/// #![deny(unused_imports)]
180+
/// ```rust,compile_fail
181+
/// #![deny(redundant_imports)]
182182
/// use std::mem::drop;
183183
/// fn main() {
184184
/// let s = Box::new(32);
Lines changed: 4 additions & 3 deletions
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
}
Lines changed: 14 additions & 0 deletions
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+
Lines changed: 2 additions & 3 deletions
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
}
Lines changed: 17 additions & 0 deletions
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+
Lines changed: 4 additions & 3 deletions
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() {}
Lines changed: 14 additions & 0 deletions
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

Lines changed: 4 additions & 3 deletions
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

0 commit comments

Comments
 (0)