Skip to content

Commit 13a90b9

Browse files
authored
Unrolled build for rust-lang#123813
Rollup merge of rust-lang#123813 - compiler-errors:redundant-lint, r=petrochenkov Add `REDUNDANT_IMPORTS` lint for new redundant import detection Defaults to Allow for now. Stacked on rust-lang#123744 to avoid merge conflict, but much easier to review all as one. r? petrochenkov
2 parents 71b2116 + f6f587e commit 13a90b9

28 files changed

+332
-62
lines changed

compiler/rustc_lint/messages.ftl

+4-4
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

+26
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

+3-5
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

+2-2
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);
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+

0 commit comments

Comments
 (0)