Improve warning

This commit is contained in:
Fabian Drinck 2019-03-16 17:38:12 +01:00
parent 2cebbe2599
commit 2245d10fac
3 changed files with 18 additions and 17 deletions

View file

@ -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<Span>, 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)
);
}
}
}
}
}

View file

@ -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"
);
}
}
}

View file

@ -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
| ^^^^^^^^^^^^^^^