Skip to content

Commit f6f587e

Browse files
Introduce REDUNDANT_IMPORTS lint
1 parent 006c8df commit f6f587e

28 files changed

+332
-62
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -700,10 +700,10 @@ lint_reason_must_be_string_literal = reason must be a string literal
700700
lint_reason_must_come_last = reason in lint attribute must come last
701701
702702
lint_redundant_import = the item `{$ident}` is imported redundantly
703-
.label_imported_here = the item `{ident}` is already imported here
704-
.label_defined_here = the item `{ident}` is already defined here
705-
.label_imported_prelude = the item `{ident}` is already imported by the extern prelude
706-
.label_defined_prelude = the item `{ident}` is already defined by the extern prelude
703+
.label_imported_here = the item `{$ident}` is already imported here
704+
.label_defined_here = the item `{$ident}` is already defined here
705+
.label_imported_prelude = the item `{$ident}` is already imported by the extern prelude
706+
.label_defined_prelude = the item `{$ident}` is already defined by the extern prelude
707707
708708
lint_redundant_import_visibility = glob import doesn't reexport anything with visibility `{$import_vis}` because no imported item is public enough
709709
.note = the most public imported item is `{$max_vis}`

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ declare_lint_pass! {
8282
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
8383
PTR_CAST_ADD_AUTO_TO_OBJECT,
8484
PUB_USE_OF_PRIVATE_EXTERN_CRATE,
85+
REDUNDANT_IMPORTS,
8586
REDUNDANT_LIFETIMES,
8687
REFINING_IMPL_TRAIT_INTERNAL,
8788
REFINING_IMPL_TRAIT_REACHABLE,
@@ -426,6 +427,31 @@ declare_lint! {
426427
"imports that are never used"
427428
}
428429

430+
declare_lint! {
431+
/// The `redundant_imports` lint detects imports that are redundant due to being
432+
/// imported already; either through a previous import, or being present in
433+
/// the prelude.
434+
///
435+
/// ### Example
436+
///
437+
/// ```rust,compile_fail
438+
/// #![deny(redundant_imports)]
439+
/// use std::option::Option::None;
440+
/// fn foo() -> Option<i32> { None }
441+
/// ```
442+
///
443+
/// {{produces}}
444+
///
445+
/// ### Explanation
446+
///
447+
/// Redundant imports are unnecessary and can be removed to simplify code.
448+
/// If you intended to re-export the item to make it available outside of the
449+
/// module, add a visibility modifier like `pub`.
450+
pub REDUNDANT_IMPORTS,
451+
Allow,
452+
"imports that are redundant due to being imported already"
453+
}
454+
429455
declare_lint! {
430456
/// The `must_not_suspend` lint guards against values that shouldn't be held across suspend points
431457
/// (`.await`)

compiler/rustc_resolve/src/imports.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::metadata::{ModChild, Reexport};
1414
use rustc_middle::{span_bug, ty};
1515
use rustc_session::lint::builtin::{
1616
AMBIGUOUS_GLOB_REEXPORTS, HIDDEN_GLOB_REEXPORTS, PUB_USE_OF_PRIVATE_EXTERN_CRATE,
17-
UNUSED_IMPORTS,
17+
REDUNDANT_IMPORTS, UNUSED_IMPORTS,
1818
};
1919
use rustc_session::lint::BuiltinLintDiag;
2020
use rustc_span::edit_distance::find_best_match_for_name;
@@ -1387,14 +1387,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13871387
let mut redundant_spans: Vec<_> = redundant_span.present_items().collect();
13881388
redundant_spans.sort();
13891389
redundant_spans.dedup();
1390-
/* FIXME(unused_imports): Add this back as a new lint
1391-
self.lint_buffer.buffer_lint_with_diagnostic(
1392-
UNUSED_IMPORTS,
1390+
self.lint_buffer.buffer_lint(
1391+
REDUNDANT_IMPORTS,
13931392
id,
13941393
import.span,
13951394
BuiltinLintDiag::RedundantImport(redundant_spans, source),
13961395
);
1397-
*/
13981396
return true;
13991397
}
14001398

compiler/rustc_resolve/src/lib.rs

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

179179
/// Used for tracking import use types which will be used for redundant import checking.
180180
/// ### Used::Scope Example
181-
/// ```rust,ignore (redundant_imports)
182-
/// #![deny(unused_imports)]
181+
/// ```rust,compile_fail
182+
/// #![deny(redundant_imports)]
183183
/// use std::mem::drop;
184184
/// fn main() {
185185
/// 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+

0 commit comments

Comments
 (0)