diff --git a/compiler/rustc_lint/src/context/diagnostics.rs b/compiler/rustc_lint/src/context/diagnostics.rs index a0be1c09c9a3d..129725ec4fa77 100644 --- a/compiler/rustc_lint/src/context/diagnostics.rs +++ b/compiler/rustc_lint/src/context/diagnostics.rs @@ -124,7 +124,7 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Di BuiltinLintDiag::UnknownCrateTypes(span, note, sugg) => { diag.span_suggestion(span, note, sugg, Applicability::MaybeIncorrect); } - BuiltinLintDiag::UnusedImports(message, replaces, in_test_module) => { + BuiltinLintDiag::UnusedImports(message, replaces, in_test_module, redundant_sources) => { if !replaces.is_empty() { diag.tool_only_multipart_suggestion( message, @@ -139,15 +139,15 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Di "if this is a test module, consider adding a `#[cfg(test)]` to the containing module", ); } - } - BuiltinLintDiag::RedundantImport(spans, ident) => { - for (span, is_imported) in spans { - let introduced = if is_imported { "imported" } else { "defined" }; - let span_msg = if span.is_dummy() { "by prelude" } else { "here" }; - diag.span_label( - span, - format!("the item `{ident}` is already {introduced} {span_msg}"), - ); + for (ident, spans) in redundant_sources { + for (span, is_imported) in spans { + let introduced = if is_imported { "imported" } else { "defined" }; + let span_msg = if span.is_dummy() { "by prelude" } else { "here" }; + diag.span_label( + span, + format!("the item `{ident}` is already {introduced} {span_msg}"), + ); + } } } BuiltinLintDiag::DeprecatedMacro(suggestion, span) => { diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index 7f200a7b623d6..636a4c378b487 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -576,8 +576,7 @@ pub enum BuiltinLintDiag { MacroExpandedMacroExportsAccessedByAbsolutePaths(Span), ElidedLifetimesInPaths(usize, Span, bool, Span), UnknownCrateTypes(Span, String, String), - UnusedImports(String, Vec<(Span, String)>, Option), - RedundantImport(Vec<(Span, bool)>, Ident), + UnusedImports(String, Vec<(Span, String)>, Option, Vec<(Ident, Vec<(Span, bool)>)>), DeprecatedMacro(Option, Span), MissingAbi(Span, Abi), UnusedDocComment(Span), diff --git a/compiler/rustc_resolve/src/check_unused.rs b/compiler/rustc_resolve/src/check_unused.rs index bf1ea2e270932..0793a063635bf 100644 --- a/compiler/rustc_resolve/src/check_unused.rs +++ b/compiler/rustc_resolve/src/check_unused.rs @@ -31,7 +31,6 @@ use crate::NameBindingKind; use rustc_ast as ast; use rustc_ast::visit::{self, Visitor}; use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet}; -use rustc_data_structures::unord::UnordSet; use rustc_errors::{pluralize, MultiSpan}; use rustc_hir::def::{DefKind, Res}; use rustc_session::lint::builtin::{MACRO_USE_EXTERN_CRATE, UNUSED_EXTERN_CRATES, UNUSED_IMPORTS}; @@ -43,7 +42,7 @@ struct UnusedImport { use_tree: ast::UseTree, use_tree_id: ast::NodeId, item_span: Span, - unused: UnordSet, + unused: FxIndexSet, } impl UnusedImport { @@ -96,7 +95,7 @@ impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> { // FIXME(#120456) - is `swap_remove` correct? self.r.maybe_unused_trait_imports.swap_remove(&def_id); if let Some(i) = self.unused_imports.get_mut(&self.base_id) { - i.unused.remove(&id); + i.unused.swap_remove(&id); } } } @@ -138,6 +137,16 @@ impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> { } } + fn merge_unused_import(&mut self, unused_import: UnusedImport) { + if let Some(import) = self.unused_imports.get_mut(&unused_import.use_tree_id) { + for id in unused_import.unused { + import.unused.insert(id); + } + } else { + self.unused_imports.entry(unused_import.use_tree_id).or_insert(unused_import); + } + } + fn report_unused_extern_crate_items( &mut self, maybe_unused_extern_crates: FxHashMap, @@ -265,6 +274,40 @@ impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> { } } +struct ImportFinderVisitor { + unused_import: Option, + root_node_id: ast::NodeId, + node_id: ast::NodeId, + item_span: Span, +} + +impl Visitor<'_> for ImportFinderVisitor { + fn visit_item(&mut self, item: &ast::Item) { + match item.kind { + ast::ItemKind::Use(..) if item.span.is_dummy() => return, + _ => {} + } + + self.item_span = item.span_with_attributes(); + visit::walk_item(self, item); + } + + fn visit_use_tree(&mut self, use_tree: &ast::UseTree, id: ast::NodeId, _nested: bool) { + if id == self.root_node_id { + let mut unused_import = UnusedImport { + use_tree: use_tree.clone(), + use_tree_id: id, + item_span: self.item_span, + unused: Default::default(), + }; + unused_import.unused.insert(self.node_id); + self.unused_import = Some(unused_import); + } + visit::walk_use_tree(self, use_tree, id); + } +} + +#[derive(Debug)] enum UnusedSpanResult { Used, FlatUnused(Span, Span), @@ -412,6 +455,46 @@ impl Resolver<'_, '_> { visitor.report_unused_extern_crate_items(maybe_unused_extern_crates); + let unused_imports = &visitor.unused_imports; + let mut check_redundant_imports = FxIndexSet::default(); + for module in visitor.r.arenas.local_modules().iter() { + for (_key, resolution) in visitor.r.resolutions(*module).borrow().iter() { + let resolution = resolution.borrow(); + + if let Some(binding) = resolution.binding + && let NameBindingKind::Import { import, .. } = binding.kind + && let ImportKind::Single { id, .. } = import.kind + { + if let Some(unused_import) = unused_imports.get(&import.root_id) + && unused_import.unused.contains(&id) + { + continue; + } + + check_redundant_imports.insert(import); + } + } + } + + let mut redundant_source_spans = FxHashMap::default(); + for import in check_redundant_imports { + if let Some(redundant_spans) = visitor.r.check_for_redundant_imports(import) + && let ImportKind::Single { source, id, .. } = import.kind + { + let mut finder = ImportFinderVisitor { + node_id: id, + root_node_id: import.root_id, + unused_import: None, + item_span: Span::default(), + }; + visit::walk_crate(&mut finder, krate); + if let Some(unused) = finder.unused_import { + visitor.merge_unused_import(unused); + redundant_source_spans.insert(id, (source, redundant_spans)); + } + } + } + for unused in visitor.unused_imports.values() { let mut fixes = Vec::new(); let spans = match calc_unused_spans(unused, &unused.use_tree, unused.use_tree_id) { @@ -432,9 +515,16 @@ impl Resolver<'_, '_> { } }; - let ms = MultiSpan::from_spans(spans); + let redundant_sources = unused + .unused + .clone() + .into_iter() + .filter_map(|id| redundant_source_spans.get(&id)) + .cloned() + .collect(); - let mut span_snippets = ms + let multi_span = MultiSpan::from_spans(spans); + let mut span_snippets = multi_span .primary_spans() .iter() .filter_map(|span| tcx.sess.source_map().span_to_snippet(*span).ok()) @@ -442,9 +532,18 @@ impl Resolver<'_, '_> { .collect::>(); span_snippets.sort(); + let remove_type = + if unused.unused.iter().all(|i| redundant_source_spans.get(i).is_none()) { + "unused import" + } else if unused.unused.iter().all(|i| redundant_source_spans.get(i).is_some()) { + "redundant import" + } else { + "unused or redundant import" + }; let msg = format!( - "unused import{}{}", - pluralize!(ms.primary_spans().len()), + "{}{}{}", + remove_type, + pluralize!(multi_span.primary_spans().len()), if !span_snippets.is_empty() { format!(": {}", span_snippets.join(", ")) } else { @@ -453,11 +552,11 @@ impl Resolver<'_, '_> { ); let fix_msg = if fixes.len() == 1 && fixes[0].0 == unused.item_span { - "remove the whole `use` item" - } else if ms.primary_spans().len() > 1 { - "remove the unused imports" + "remove the whole `use` item".to_owned() + } else if multi_span.primary_spans().len() > 1 { + format!("remove the {}s", remove_type) } else { - "remove the unused import" + format!("remove the {}", remove_type) }; // If we are in the `--test` mode, suppress a help that adds the `#[cfg(test)]` @@ -487,35 +586,15 @@ impl Resolver<'_, '_> { visitor.r.lint_buffer.buffer_lint_with_diagnostic( UNUSED_IMPORTS, unused.use_tree_id, - ms, + multi_span, msg, - BuiltinLintDiag::UnusedImports(fix_msg.into(), fixes, test_module_span), + BuiltinLintDiag::UnusedImports( + fix_msg.into(), + fixes, + test_module_span, + redundant_sources, + ), ); } - - let unused_imports = visitor.unused_imports; - let mut check_redundant_imports = FxIndexSet::default(); - for module in self.arenas.local_modules().iter() { - for (_key, resolution) in self.resolutions(*module).borrow().iter() { - let resolution = resolution.borrow(); - - if let Some(binding) = resolution.binding - && let NameBindingKind::Import { import, .. } = binding.kind - && let ImportKind::Single { id, .. } = import.kind - { - if let Some(unused_import) = unused_imports.get(&import.root_id) - && unused_import.unused.contains(&id) - { - continue; - } - - check_redundant_imports.insert(import); - } - } - } - - for import in check_redundant_imports { - self.check_for_redundant_imports(import); - } } } diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index ea08041f2aadd..aa619f0c238ea 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -1306,7 +1306,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { None } - pub(crate) fn check_for_redundant_imports(&mut self, import: Import<'a>) { + pub(crate) fn check_for_redundant_imports( + &mut self, + import: Import<'a>, + ) -> Option> { // This function is only called for single imports. let ImportKind::Single { source, target, ref source_bindings, ref target_bindings, id, .. @@ -1317,12 +1320,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { // Skip if the import is of the form `use source as target` and source != target. if source != target { - return; + return None; } // Skip if the import was produced by a macro. if import.parent_scope.expansion != LocalExpnId::ROOT { - return; + return None; } // Skip if we are inside a named module (in contrast to an anonymous @@ -1332,7 +1335,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { if import.used.get() == Some(Used::Other) || self.effective_visibilities.is_exported(self.local_def_id(id)) { - return; + return None; } let mut is_redundant = true; @@ -1368,14 +1371,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { let mut redundant_spans: Vec<_> = redundant_span.present_items().collect(); redundant_spans.sort(); redundant_spans.dedup(); - self.lint_buffer.buffer_lint_with_diagnostic( - UNUSED_IMPORTS, - id, - import.span, - format!("the item `{source}` is imported redundantly"), - BuiltinLintDiag::RedundantImport(redundant_spans, source), - ); + return Some(redundant_spans); } + None } fn resolve_glob_import(&mut self, import: Import<'a>) { diff --git a/tests/ui/imports/redundant-import-issue-121915-2015.rs b/tests/ui/imports/redundant-import-issue-121915-2015.rs index d41d190bb58c4..57a06570d0bb6 100644 --- a/tests/ui/imports/redundant-import-issue-121915-2015.rs +++ b/tests/ui/imports/redundant-import-issue-121915-2015.rs @@ -6,6 +6,6 @@ extern crate aux_issue_121915; #[deny(unused_imports)] fn main() { use aux_issue_121915; - //~^ ERROR the item `aux_issue_121915` is imported redundantly + //~^ ERROR redundant import 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 index 174ed4fb96b11..d11fba7841a68 100644 --- a/tests/ui/imports/redundant-import-issue-121915-2015.stderr +++ b/tests/ui/imports/redundant-import-issue-121915-2015.stderr @@ -1,4 +1,4 @@ -error: the item `aux_issue_121915` is imported redundantly +error: redundant import: `aux_issue_121915` --> $DIR/redundant-import-issue-121915-2015.rs:8:9 | LL | extern crate aux_issue_121915; diff --git a/tests/ui/imports/redundant-import-issue-121915.rs b/tests/ui/imports/redundant-import-issue-121915.rs index 237acc4af2565..b2d6348f4cb97 100644 --- a/tests/ui/imports/redundant-import-issue-121915.rs +++ b/tests/ui/imports/redundant-import-issue-121915.rs @@ -4,6 +4,6 @@ #[deny(unused_imports)] fn main() { use aux_issue_121915; - //~^ ERROR the item `aux_issue_121915` is imported redundantly + //~^ ERROR redundant import aux_issue_121915::item(); } diff --git a/tests/ui/imports/redundant-import-issue-121915.stderr b/tests/ui/imports/redundant-import-issue-121915.stderr index 0047d7c34207c..211be7a486432 100644 --- a/tests/ui/imports/redundant-import-issue-121915.stderr +++ b/tests/ui/imports/redundant-import-issue-121915.stderr @@ -1,4 +1,4 @@ -error: the item `aux_issue_121915` is imported redundantly +error: redundant import: `aux_issue_121915` --> $DIR/redundant-import-issue-121915.rs:6:9 | LL | use aux_issue_121915; 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..d0a7d7f9cd056 --- /dev/null +++ b/tests/ui/imports/suggest-remove-issue-121315.fixed @@ -0,0 +1,39 @@ +//@ run-rustfix +//@ compile-flags: --edition 2021 +#![deny(unused_imports)] +#![allow(dead_code)] + +fn test0() { + // Test remove FlatUnused + + //~^ ERROR redundant import + let _ = u32::try_from(5i32); +} + +fn test1() { + // Test remove NestedFullUnused + + //~^ ERROR redundant imports + + let _ = u32::try_from(5i32); + let _a: i32 = u32::try_into(5u32).unwrap(); +} + +fn test2() { + // Test remove both redundant and unused + + //~^ ERROR unused or redundant imports: `AsMut`, `Into` + + let _a: u32 = (5u8).into(); +} + +fn test3() { + // Test remove NestedPartialUnused + use std::convert::{Infallible}; + //~^ ERROR unused import: `From` + + trait MyTrait {} + impl MyTrait for fn() -> Infallible {} +} + +fn main() {} diff --git a/tests/ui/imports/suggest-remove-issue-121315.rs b/tests/ui/imports/suggest-remove-issue-121315.rs index 63533480ec127..dbe3fed7af049 100644 --- a/tests/ui/imports/suggest-remove-issue-121315.rs +++ b/tests/ui/imports/suggest-remove-issue-121315.rs @@ -1,3 +1,4 @@ +//@ run-rustfix //@ compile-flags: --edition 2021 #![deny(unused_imports)] #![allow(dead_code)] @@ -5,25 +6,23 @@ fn test0() { // Test remove FlatUnused use std::convert::TryFrom; - //~^ ERROR the item `TryFrom` is imported redundantly + //~^ ERROR redundant import let _ = u32::try_from(5i32); } fn test1() { - // FIXME(yukang) Test remove NestedFullUnused + // Test remove NestedFullUnused use std::convert::{TryFrom, TryInto}; - //~^ ERROR the item `TryFrom` is imported redundantly - //~| ERROR the item `TryInto` is imported redundantly + //~^ ERROR redundant imports let _ = u32::try_from(5i32); let _a: i32 = u32::try_into(5u32).unwrap(); } fn test2() { - // FIXME(yukang): Test remove both redundant and unused + // Test remove both redundant and unused use std::convert::{AsMut, Into}; - //~^ ERROR unused import: `AsMut` - //~| ERROR the item `Into` is imported redundantly + //~^ ERROR unused or redundant imports: `AsMut`, `Into` let _a: u32 = (5u8).into(); } diff --git a/tests/ui/imports/suggest-remove-issue-121315.stderr b/tests/ui/imports/suggest-remove-issue-121315.stderr index dbd742f6c781f..5c5c7ab774e8a 100644 --- a/tests/ui/imports/suggest-remove-issue-121315.stderr +++ b/tests/ui/imports/suggest-remove-issue-121315.stderr @@ -1,5 +1,5 @@ -error: the item `TryFrom` is imported redundantly - --> $DIR/suggest-remove-issue-121315.rs:7:9 +error: redundant import: `std::convert::TryFrom` + --> $DIR/suggest-remove-issue-121315.rs:8:9 | LL | use std::convert::TryFrom; | ^^^^^^^^^^^^^^^^^^^^^ @@ -8,49 +8,36 @@ LL | use std::convert::TryFrom; = note: the item `TryFrom` is already defined here | note: the lint level is defined here - --> $DIR/suggest-remove-issue-121315.rs:2:9 + --> $DIR/suggest-remove-issue-121315.rs:3:9 | LL | #![deny(unused_imports)] | ^^^^^^^^^^^^^^ -error: the item `TryFrom` is imported redundantly - --> $DIR/suggest-remove-issue-121315.rs:14:24 +error: redundant imports: `TryFrom`, `TryInto` + --> $DIR/suggest-remove-issue-121315.rs:15:24 | LL | use std::convert::{TryFrom, TryInto}; - | ^^^^^^^ + | ^^^^^^^ ^^^^^^^ --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL | = note: the item `TryFrom` is already defined here - -error: the item `TryInto` is imported redundantly - --> $DIR/suggest-remove-issue-121315.rs:14:33 - | -LL | use std::convert::{TryFrom, TryInto}; - | ^^^^^^^ - --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL | = note: the item `TryInto` is already defined here -error: unused import: `AsMut` +error: unused or redundant imports: `AsMut`, `Into` --> $DIR/suggest-remove-issue-121315.rs:24:24 | LL | use std::convert::{AsMut, Into}; - | ^^^^^ - -error: the item `Into` is imported redundantly - --> $DIR/suggest-remove-issue-121315.rs:24:31 - | -LL | use std::convert::{AsMut, Into}; - | ^^^^ + | ^^^^^ ^^^^ --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL | = note: the item `Into` is already defined here error: unused import: `From` - --> $DIR/suggest-remove-issue-121315.rs:33:24 + --> $DIR/suggest-remove-issue-121315.rs:32:24 | LL | use std::convert::{From, Infallible}; | ^^^^ -error: aborting due to 6 previous errors +error: aborting due to 4 previous errors diff --git a/tests/ui/lint/unused/issue-59896.rs b/tests/ui/lint/unused/issue-59896.rs index ff9f19acf8471..2cd8fa271e805 100644 --- a/tests/ui/lint/unused/issue-59896.rs +++ b/tests/ui/lint/unused/issue-59896.rs @@ -3,7 +3,7 @@ struct S; fn main() { - use S; //~ ERROR the item `S` is imported redundantly + use S; //~ ERROR redundant import let _s = S; } diff --git a/tests/ui/lint/unused/issue-59896.stderr b/tests/ui/lint/unused/issue-59896.stderr index 3e8298c6b72e6..6176d94c764cb 100644 --- a/tests/ui/lint/unused/issue-59896.stderr +++ b/tests/ui/lint/unused/issue-59896.stderr @@ -1,4 +1,4 @@ -error: the item `S` is imported redundantly +error: redundant import: `S` --> $DIR/issue-59896.rs:6:9 | LL | struct S; diff --git a/tests/ui/lint/use-redundant/use-redundant-glob-parent.rs b/tests/ui/lint/use-redundant/use-redundant-glob-parent.rs index 28d1fea98b582..4c15fba8a23f7 100644 --- a/tests/ui/lint/use-redundant/use-redundant-glob-parent.rs +++ b/tests/ui/lint/use-redundant/use-redundant-glob-parent.rs @@ -9,7 +9,7 @@ pub mod bar { use bar::*; pub fn warning() -> Foo { - use bar::Foo; //~ WARNING imported redundantly + use bar::Foo; //~ WARNING redundant import Foo(Bar('a')) } 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..cdafb248ee8c6 100644 --- a/tests/ui/lint/use-redundant/use-redundant-glob-parent.stderr +++ b/tests/ui/lint/use-redundant/use-redundant-glob-parent.stderr @@ -1,4 +1,4 @@ -warning: the item `Foo` is imported redundantly +warning: redundant import: `bar::Foo` --> $DIR/use-redundant-glob-parent.rs:12:9 | LL | use bar::*; diff --git a/tests/ui/lint/use-redundant/use-redundant-glob.rs b/tests/ui/lint/use-redundant/use-redundant-glob.rs index 3d3fe2579b54c..b7694d59a0a9b 100644 --- a/tests/ui/lint/use-redundant/use-redundant-glob.rs +++ b/tests/ui/lint/use-redundant/use-redundant-glob.rs @@ -7,8 +7,8 @@ pub mod bar { } pub fn warning() -> bar::Foo { + use bar::Foo; //~ WARNING redundant import: `bar::Foo` use bar::*; - use bar::Foo; //~ WARNING imported redundantly Foo(Bar('a')) } diff --git a/tests/ui/lint/use-redundant/use-redundant-glob.stderr b/tests/ui/lint/use-redundant/use-redundant-glob.stderr index d3b406d82b6db..1766e030c1ca7 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 +warning: redundant import: `bar::Foo` + --> $DIR/use-redundant-glob.rs:10:9 | -LL | use bar::*; - | ------ the item `Foo` is already imported here LL | use bar::Foo; | ^^^^^^^^ +LL | use bar::*; + | ------ the item `Foo` is already imported here | note: the lint level is defined here --> $DIR/use-redundant-glob.rs:2:9 diff --git a/tests/ui/lint/use-redundant/use-redundant-issue-71450.rs b/tests/ui/lint/use-redundant/use-redundant-issue-71450.rs index d0fb3454d3f2b..515a835c4cd8f 100644 --- a/tests/ui/lint/use-redundant/use-redundant-issue-71450.rs +++ b/tests/ui/lint/use-redundant/use-redundant-issue-71450.rs @@ -9,7 +9,7 @@ mod foo { impl String { pub fn new() -> String { - String{} + String {} } } @@ -21,9 +21,8 @@ mod foo { } fn main() { - { - use std::string::String; //~ WARNING the item `String` is imported redundantly + use std::string::String; //~ WARNING redundant import // 'String' from 'std::string::String'. let s = String::new(); println!("{}", s); @@ -41,5 +40,4 @@ fn main() { let s = String::new(); println!("{}", s); } - } diff --git a/tests/ui/lint/use-redundant/use-redundant-issue-71450.stderr b/tests/ui/lint/use-redundant/use-redundant-issue-71450.stderr index b8832a3178371..a4f3cb0645abc 100644 --- a/tests/ui/lint/use-redundant/use-redundant-issue-71450.stderr +++ b/tests/ui/lint/use-redundant/use-redundant-issue-71450.stderr @@ -1,5 +1,5 @@ -warning: the item `String` is imported redundantly - --> $DIR/use-redundant-issue-71450.rs:26:13 +warning: redundant import: `std::string::String` + --> $DIR/use-redundant-issue-71450.rs:25:13 | LL | use std::string::String; | ^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2015.rs b/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2015.rs index ae5118b2729b9..295310c23ab8e 100644 --- a/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2015.rs +++ b/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2015.rs @@ -1,17 +1,16 @@ //@ check-pass #![warn(unused_imports)] +use std::option::Option::None; //~ WARNING redundant import +use std::option::Option::Some; //~ WARNING redundant import -use std::option::Option::Some;//~ WARNING the item `Some` is imported redundantly -use std::option::Option::None; //~ WARNING the item `None` is imported redundantly - -use std::result::Result::Ok;//~ WARNING the item `Ok` is imported redundantly -use std::result::Result::Err;//~ WARNING the item `Err` is imported redundantly use std::convert::{TryFrom, TryInto}; +use std::result::Result::Err; //~ WARNING redundant import +use std::result::Result::Ok; //~ WARNING redundant import fn main() { let _a: Option = Some(1); - let _b: Option = None; + let _b: Option = None; let _c: Result = Ok(1); let _d: Result = Err("error"); let _e: Result = 8u8.try_into(); 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..c710458c9310e 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,11 +1,11 @@ -warning: the item `Some` is imported redundantly - --> $DIR/use-redundant-prelude-rust-2015.rs:5:5 +warning: redundant import: `std::option::Option::None` + --> $DIR/use-redundant-prelude-rust-2015.rs:4:5 | -LL | use std::option::Option::Some; +LL | use std::option::Option::None; | ^^^^^^^^^^^^^^^^^^^^^^^^^ --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL | - = note: the item `Some` is already defined here + = note: the item `None` is already defined here | note: the lint level is defined here --> $DIR/use-redundant-prelude-rust-2015.rs:2:9 @@ -13,32 +13,32 @@ note: the lint level is defined here LL | #![warn(unused_imports)] | ^^^^^^^^^^^^^^ -warning: the item `None` is imported redundantly - --> $DIR/use-redundant-prelude-rust-2015.rs:6:5 +warning: redundant import: `std::option::Option::Some` + --> $DIR/use-redundant-prelude-rust-2015.rs:5:5 | -LL | use std::option::Option::None; +LL | use std::option::Option::Some; | ^^^^^^^^^^^^^^^^^^^^^^^^^ --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL | - = note: the item `None` is already defined here + = note: the item `Some` is already defined here -warning: the item `Ok` is imported redundantly +warning: redundant import: `std::result::Result::Err` --> $DIR/use-redundant-prelude-rust-2015.rs:8:5 | -LL | use std::result::Result::Ok; - | ^^^^^^^^^^^^^^^^^^^^^^^ +LL | use std::result::Result::Err; + | ^^^^^^^^^^^^^^^^^^^^^^^^ --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL | - = note: the item `Ok` is already defined here + = note: the item `Err` is already defined here -warning: the item `Err` is imported redundantly +warning: redundant import: `std::result::Result::Ok` --> $DIR/use-redundant-prelude-rust-2015.rs:9:5 | -LL | use std::result::Result::Err; - | ^^^^^^^^^^^^^^^^^^^^^^^^ +LL | use std::result::Result::Ok; + | ^^^^^^^^^^^^^^^^^^^^^^^ --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL | - = note: the item `Err` is already defined here + = note: the item `Ok` is already defined here warning: 4 warnings emitted diff --git a/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2021.rs b/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2021.rs index cb4dcb6c0bd67..49e5316769ec1 100644 --- a/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2021.rs +++ b/tests/ui/lint/use-redundant/use-redundant-prelude-rust-2021.rs @@ -2,8 +2,8 @@ //@ edition:2021 #![warn(unused_imports)] -use std::convert::TryFrom;//~ WARNING the item `TryFrom` is imported redundantly -use std::convert::TryInto;//~ WARNING the item `TryInto` is imported redundantly +use std::convert::TryFrom; //~ WARNING redundant import +use std::convert::TryInto; //~ WARNING redundant import fn main() { let _e: Result = 8u8.try_into(); 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..fb9794bff26a6 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,4 +1,4 @@ -warning: the item `TryFrom` is imported redundantly +warning: redundant import: `std::convert::TryFrom` --> $DIR/use-redundant-prelude-rust-2021.rs:5:5 | LL | use std::convert::TryFrom; @@ -13,7 +13,7 @@ note: the lint level is defined here LL | #![warn(unused_imports)] | ^^^^^^^^^^^^^^ -warning: the item `TryInto` is imported redundantly +warning: redundant import: `std::convert::TryInto` --> $DIR/use-redundant-prelude-rust-2021.rs:6:5 | LL | use std::convert::TryInto; diff --git a/tests/ui/lint/use-redundant/use-redundant.rs b/tests/ui/lint/use-redundant/use-redundant.rs index 88d3ee75a3f2d..b8ecb6c07c32c 100644 --- a/tests/ui/lint/use-redundant/use-redundant.rs +++ b/tests/ui/lint/use-redundant/use-redundant.rs @@ -11,14 +11,18 @@ fn baz() -> Bar { 3 } -mod m1 { pub struct S {} } -mod m2 { pub struct S {} } +mod m1 { + pub struct S {} +} +mod m2 { + pub struct S {} +} use m1::*; //~ WARNING unused import use m2::*; //~ WARNING unused import fn main() { - use crate::foo::Bar; //~ WARNING imported redundantly + use crate::foo::Bar; //~ WARNING redundant import let _a: Bar = 3; baz(); diff --git a/tests/ui/lint/use-redundant/use-redundant.stderr b/tests/ui/lint/use-redundant/use-redundant.stderr index c861a1956e1d1..a9029570caeba 100644 --- a/tests/ui/lint/use-redundant/use-redundant.stderr +++ b/tests/ui/lint/use-redundant/use-redundant.stderr @@ -1,5 +1,5 @@ warning: unused import: `m1::*` - --> $DIR/use-redundant.rs:17:5 + --> $DIR/use-redundant.rs:21:5 | LL | use m1::*; | ^^^^^ @@ -11,13 +11,13 @@ LL | #![warn(unused_imports)] | ^^^^^^^^^^^^^^ warning: unused import: `m2::*` - --> $DIR/use-redundant.rs:18:5 + --> $DIR/use-redundant.rs:22:5 | LL | use m2::*; | ^^^^^ -warning: the item `Bar` is imported redundantly - --> $DIR/use-redundant.rs:21:9 +warning: redundant import: `crate::foo::Bar` + --> $DIR/use-redundant.rs:25:9 | LL | use crate::foo::Bar; | --------------- the item `Bar` is already imported here