diff --git a/compiler/rustc_lint/src/context/diagnostics.rs b/compiler/rustc_lint/src/context/diagnostics.rs index a0aa252bcdf2f..33dae0c6d0886 100644 --- a/compiler/rustc_lint/src/context/diagnostics.rs +++ b/compiler/rustc_lint/src/context/diagnostics.rs @@ -103,11 +103,21 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Di ); } } - BuiltinLintDiag::RedundantImport(spans, ident) => { + BuiltinLintDiag::RedundantImport(spans, ident, import_span) => { for (span, is_imported) in spans { let introduced = if is_imported { "imported" } else { "defined" }; - diag.span_label(span, format!("the item `{ident}` is already {introduced} here")); + let span_msg = if span.is_dummy() { "by prelude" } else { "here" }; + diag.span_label( + span, + format!("the item `{ident}` is already {introduced} {span_msg}"), + ); } + diag.span_suggestion( + import_span, + "remove this import", + "", + Applicability::MachineApplicable, + ); } BuiltinLintDiag::DeprecatedMacro(suggestion, span) => { stability::deprecation_suggestion(diag, "macro", suggestion, span) diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index 7f200a7b623d6..322a1a2cf8f4c 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -577,7 +577,7 @@ pub enum BuiltinLintDiag { ElidedLifetimesInPaths(usize, Span, bool, Span), UnknownCrateTypes(Span, String, String), UnusedImports(String, Vec<(Span, String)>, Option), - RedundantImport(Vec<(Span, bool)>, Ident), + RedundantImport(Vec<(Span, bool)>, Ident, Span), DeprecatedMacro(Option, Span), MissingAbi(Span, Abi), UnusedDocComment(Span), diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 0cd925c5ba6ae..94430fd526826 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -1334,9 +1334,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { } let mut is_redundant = true; - let mut redundant_span = PerNS { value_ns: None, type_ns: None, macro_ns: None }; - self.per_ns(|this, ns| { if is_redundant && let Ok(binding) = source_bindings[ns].get() { if binding.res() == Res::Err { @@ -1371,9 +1369,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { self.lint_buffer.buffer_lint_with_diagnostic( UNUSED_IMPORTS, id, - import.span, + import.use_span, format!("the item `{source}` is imported redundantly"), - BuiltinLintDiag::RedundantImport(redundant_spans, source), + BuiltinLintDiag::RedundantImport(redundant_spans, source, import.use_span), ); } } diff --git a/tests/ui/imports/auxiliary/aux-issue-121915.rs b/tests/ui/imports/auxiliary/aux-issue-121915.rs new file mode 100644 index 0000000000000..7f9f5bda79ffd --- /dev/null +++ b/tests/ui/imports/auxiliary/aux-issue-121915.rs @@ -0,0 +1 @@ +pub fn item() {} diff --git a/tests/ui/imports/redundant-import-issue-121915-2015.rs b/tests/ui/imports/redundant-import-issue-121915-2015.rs new file mode 100644 index 0000000000000..d41d190bb58c4 --- /dev/null +++ b/tests/ui/imports/redundant-import-issue-121915-2015.rs @@ -0,0 +1,11 @@ +//@ compile-flags: --extern aux_issue_121915 --edition 2015 +//@ aux-build: aux-issue-121915.rs + +extern crate aux_issue_121915; + +#[deny(unused_imports)] +fn main() { + use aux_issue_121915; + //~^ ERROR the item `aux_issue_121915` is imported redundantly + aux_issue_121915::item(); +} diff --git a/tests/ui/imports/redundant-import-issue-121915-2015.stderr b/tests/ui/imports/redundant-import-issue-121915-2015.stderr new file mode 100644 index 0000000000000..3678a6cf1d144 --- /dev/null +++ b/tests/ui/imports/redundant-import-issue-121915-2015.stderr @@ -0,0 +1,17 @@ +error: the item `aux_issue_121915` is imported redundantly + --> $DIR/redundant-import-issue-121915-2015.rs:8:5 + | +LL | extern crate aux_issue_121915; + | ------------------------------ the item `aux_issue_121915` is already imported here +... +LL | use aux_issue_121915; + | ^^^^^^^^^^^^^^^^^^^^^ help: remove this import + | +note: the lint level is defined here + --> $DIR/redundant-import-issue-121915-2015.rs:6:8 + | +LL | #[deny(unused_imports)] + | ^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error + diff --git a/tests/ui/imports/redundant-import-issue-121915.rs b/tests/ui/imports/redundant-import-issue-121915.rs new file mode 100644 index 0000000000000..237acc4af2565 --- /dev/null +++ b/tests/ui/imports/redundant-import-issue-121915.rs @@ -0,0 +1,9 @@ +//@ compile-flags: --extern aux_issue_121915 --edition 2018 +//@ aux-build: aux-issue-121915.rs + +#[deny(unused_imports)] +fn main() { + use aux_issue_121915; + //~^ ERROR the item `aux_issue_121915` is imported redundantly + aux_issue_121915::item(); +} diff --git a/tests/ui/imports/redundant-import-issue-121915.stderr b/tests/ui/imports/redundant-import-issue-121915.stderr new file mode 100644 index 0000000000000..eb23ee971d29d --- /dev/null +++ b/tests/ui/imports/redundant-import-issue-121915.stderr @@ -0,0 +1,17 @@ +error: the item `aux_issue_121915` is imported redundantly + --> $DIR/redundant-import-issue-121915.rs:6:5 + | +LL | use aux_issue_121915; + | ^^^^^^^^^^^^^^^^^^^^^ + | | + | the item `aux_issue_121915` is already defined by prelude + | help: remove this import + | +note: the lint level is defined here + --> $DIR/redundant-import-issue-121915.rs:4:8 + | +LL | #[deny(unused_imports)] + | ^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error + diff --git a/tests/ui/imports/suggest-remove-issue-121315.fixed b/tests/ui/imports/suggest-remove-issue-121315.fixed new file mode 100644 index 0000000000000..eb757ef39de39 --- /dev/null +++ b/tests/ui/imports/suggest-remove-issue-121315.fixed @@ -0,0 +1,8 @@ +//@ run-rustfix +//@ compile-flags: --edition 2021 +#[deny(unused_imports)] +//~^ ERROR the item `TryFrom` is imported redundantly + +fn main() { + let _ = u32::try_from(5i32); +} diff --git a/tests/ui/imports/suggest-remove-issue-121315.rs b/tests/ui/imports/suggest-remove-issue-121315.rs new file mode 100644 index 0000000000000..67fe5f63128f9 --- /dev/null +++ b/tests/ui/imports/suggest-remove-issue-121315.rs @@ -0,0 +1,9 @@ +//@ run-rustfix +//@ compile-flags: --edition 2021 +#[deny(unused_imports)] +use std::convert::TryFrom; +//~^ ERROR the item `TryFrom` is imported redundantly + +fn main() { + let _ = u32::try_from(5i32); +} diff --git a/tests/ui/imports/suggest-remove-issue-121315.stderr b/tests/ui/imports/suggest-remove-issue-121315.stderr new file mode 100644 index 0000000000000..9851d6ca7d988 --- /dev/null +++ b/tests/ui/imports/suggest-remove-issue-121315.stderr @@ -0,0 +1,17 @@ +error: the item `TryFrom` is imported redundantly + --> $DIR/suggest-remove-issue-121315.rs:4:1 + | +LL | use std::convert::TryFrom; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import + --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL + | + = note: the item `TryFrom` is already defined here + | +note: the lint level is defined here + --> $DIR/suggest-remove-issue-121315.rs:3:8 + | +LL | #[deny(unused_imports)] + | ^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error + diff --git a/tests/ui/lint/unused/issue-59896.stderr b/tests/ui/lint/unused/issue-59896.stderr index 3e8298c6b72e6..c5474d29e52a1 100644 --- a/tests/ui/lint/unused/issue-59896.stderr +++ b/tests/ui/lint/unused/issue-59896.stderr @@ -1,11 +1,11 @@ error: the item `S` is imported redundantly - --> $DIR/issue-59896.rs:6:9 + --> $DIR/issue-59896.rs:6:5 | LL | struct S; | --------- the item `S` is already defined here ... LL | use S; - | ^ + | ^^^^^^ help: remove this import | note: the lint level is defined here --> $DIR/issue-59896.rs:1:9 diff --git a/tests/ui/lint/use-redundant/use-redundant-glob-parent.stderr b/tests/ui/lint/use-redundant/use-redundant-glob-parent.stderr index 2c3b33452702b..0a2310d65e1ea 100644 --- a/tests/ui/lint/use-redundant/use-redundant-glob-parent.stderr +++ b/tests/ui/lint/use-redundant/use-redundant-glob-parent.stderr @@ -1,11 +1,11 @@ warning: the item `Foo` is imported redundantly - --> $DIR/use-redundant-glob-parent.rs:12:9 + --> $DIR/use-redundant-glob-parent.rs:12:5 | LL | use bar::*; | ------ the item `Foo` is already imported here ... LL | use bar::Foo; - | ^^^^^^^^ + | ^^^^^^^^^^^^^ help: remove this import | note: the lint level is defined here --> $DIR/use-redundant-glob-parent.rs:2:9 diff --git a/tests/ui/lint/use-redundant/use-redundant-glob.stderr b/tests/ui/lint/use-redundant/use-redundant-glob.stderr index d3b406d82b6db..ab18badbfddf7 100644 --- a/tests/ui/lint/use-redundant/use-redundant-glob.stderr +++ b/tests/ui/lint/use-redundant/use-redundant-glob.stderr @@ -1,10 +1,10 @@ warning: the item `Foo` is imported redundantly - --> $DIR/use-redundant-glob.rs:11:9 + --> $DIR/use-redundant-glob.rs:11:5 | LL | use bar::*; | ------ the item `Foo` is already imported here LL | use bar::Foo; - | ^^^^^^^^ + | ^^^^^^^^^^^^^ help: remove this import | note: the lint level is defined here --> $DIR/use-redundant-glob.rs:2:9 diff --git a/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2015.stderr b/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2015.stderr index 1b09df911eb03..6811d39227df9 100644 --- a/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2015.stderr +++ b/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2015.stderr @@ -1,8 +1,8 @@ warning: the item `Some` is imported redundantly - --> $DIR/use-redundant-prelude-rust-2015.rs:5:5 + --> $DIR/use-redundant-prelude-rust-2015.rs:5:1 | LL | use std::option::Option::Some; - | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL | = note: the item `Some` is already defined here @@ -14,28 +14,28 @@ LL | #![warn(unused_imports)] | ^^^^^^^^^^^^^^ warning: the item `None` is imported redundantly - --> $DIR/use-redundant-prelude-rust-2015.rs:6:5 + --> $DIR/use-redundant-prelude-rust-2015.rs:6:1 | LL | use std::option::Option::None; - | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL | = note: the item `None` is already defined here warning: the item `Ok` is imported redundantly - --> $DIR/use-redundant-prelude-rust-2015.rs:8:5 + --> $DIR/use-redundant-prelude-rust-2015.rs:8:1 | LL | use std::result::Result::Ok; - | ^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL | = note: the item `Ok` is already defined here warning: the item `Err` is imported redundantly - --> $DIR/use-redundant-prelude-rust-2015.rs:9:5 + --> $DIR/use-redundant-prelude-rust-2015.rs:9:1 | LL | use std::result::Result::Err; - | ^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL | = note: the item `Err` is already defined here diff --git a/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2021.stderr b/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2021.stderr index 542356dc996df..4241d752e31f3 100644 --- a/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2021.stderr +++ b/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2021.stderr @@ -1,8 +1,8 @@ warning: the item `TryFrom` is imported redundantly - --> $DIR/use-redundant-prelude-rust-2021.rs:5:5 + --> $DIR/use-redundant-prelude-rust-2021.rs:5:1 | LL | use std::convert::TryFrom; - | ^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL | = note: the item `TryFrom` is already defined here @@ -14,10 +14,10 @@ LL | #![warn(unused_imports)] | ^^^^^^^^^^^^^^ warning: the item `TryInto` is imported redundantly - --> $DIR/use-redundant-prelude-rust-2021.rs:6:5 + --> $DIR/use-redundant-prelude-rust-2021.rs:6:1 | LL | use std::convert::TryInto; - | ^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL | = note: the item `TryInto` is already defined here diff --git a/tests/ui/lint/use-redundant/use-redundant.stderr b/tests/ui/lint/use-redundant/use-redundant.stderr index c861a1956e1d1..cdd95bbf93c7b 100644 --- a/tests/ui/lint/use-redundant/use-redundant.stderr +++ b/tests/ui/lint/use-redundant/use-redundant.stderr @@ -17,13 +17,13 @@ LL | use m2::*; | ^^^^^ warning: the item `Bar` is imported redundantly - --> $DIR/use-redundant.rs:21:9 + --> $DIR/use-redundant.rs:21:5 | LL | use crate::foo::Bar; | --------------- the item `Bar` is already imported here ... LL | use crate::foo::Bar; - | ^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^ help: remove this import warning: 3 warnings emitted