From 2245d10fac9b37c55a286f48d8560fc9537cbc3e Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sat, 16 Mar 2019 17:38:12 +0100 Subject: [PATCH] Improve warning --- src/librustc/lint/builtin.rs | 9 +++++++++ src/librustc_resolve/resolve_imports.rs | 15 +++++---------- src/test/ui/lint/use-redundant.stderr | 11 ++++------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 615bc0af37a..002ee599400 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -489,6 +489,7 @@ pub enum BuiltinLintDiagnostics { UnknownCrateTypes(Span, String, String), UnusedImports(String, Vec<(Span, String)>), NestedImplTrait { outer_impl_trait_span: Span, inner_impl_trait_span: Span }, + RedundantImport(Vec, ast::Ident), } impl BuiltinLintDiagnostics { @@ -585,6 +586,14 @@ impl BuiltinLintDiagnostics { db.span_label(outer_impl_trait_span, "outer `impl Trait`"); db.span_label(inner_impl_trait_span, "nested `impl Trait` here"); } + BuiltinLintDiagnostics::RedundantImport(spans, ident) => { + for span in spans { + db.span_label( + span, + format!("the item `{}` was already imported here", ident) + ); + } + } } } } diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index 534e5e0ff45..cd018d8eb9e 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -1306,21 +1306,16 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { if !is_redundant.is_empty() && is_redundant.present_items().all(|is_redundant| is_redundant) { - self.session.buffer_lint( + self.session.buffer_lint_with_diagnostic( REDUNDANT_IMPORT, directive.id, directive.span, &format!("the item `{}` is imported redundantly", ident), + BuiltinLintDiagnostics::RedundantImport( + redundant_span.present_items().collect(), + ident, + ), ); - - for span in redundant_span.present_items() { - self.session.buffer_lint( - REDUNDANT_IMPORT, - directive.id, - span, - "another import" - ); - } } } diff --git a/src/test/ui/lint/use-redundant.stderr b/src/test/ui/lint/use-redundant.stderr index 6a6becc5e61..c843ed160df 100644 --- a/src/test/ui/lint/use-redundant.stderr +++ b/src/test/ui/lint/use-redundant.stderr @@ -1,14 +1,11 @@ warning: the item `Bar` is imported redundantly --> $DIR/use-redundant.rs:14:9 | -LL | use crate::foo::Bar; //~ WARNING redundant import +LL | use crate::foo::Bar; + | --------------- the item `Bar` was already imported here +... +LL | use crate::foo::Bar; | ^^^^^^^^^^^^^^^ | = note: #[warn(redundant_import)] on by default -warning: another import - --> $DIR/use-redundant.rs:3:5 - | -LL | use crate::foo::Bar; //~ WARNING first import - | ^^^^^^^^^^^^^^^ -