Skip to content

Commit 2245d10

Browse files
Improve warning
1 parent 2cebbe2 commit 2245d10

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

src/librustc/lint/builtin.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ pub enum BuiltinLintDiagnostics {
489489
UnknownCrateTypes(Span, String, String),
490490
UnusedImports(String, Vec<(Span, String)>),
491491
NestedImplTrait { outer_impl_trait_span: Span, inner_impl_trait_span: Span },
492+
RedundantImport(Vec<Span>, ast::Ident),
492493
}
493494

494495
impl BuiltinLintDiagnostics {
@@ -585,6 +586,14 @@ impl BuiltinLintDiagnostics {
585586
db.span_label(outer_impl_trait_span, "outer `impl Trait`");
586587
db.span_label(inner_impl_trait_span, "nested `impl Trait` here");
587588
}
589+
BuiltinLintDiagnostics::RedundantImport(spans, ident) => {
590+
for span in spans {
591+
db.span_label(
592+
span,
593+
format!("the item `{}` was already imported here", ident)
594+
);
595+
}
596+
}
588597
}
589598
}
590599
}

src/librustc_resolve/resolve_imports.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,21 +1306,16 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
13061306
if !is_redundant.is_empty() &&
13071307
is_redundant.present_items().all(|is_redundant| is_redundant)
13081308
{
1309-
self.session.buffer_lint(
1309+
self.session.buffer_lint_with_diagnostic(
13101310
REDUNDANT_IMPORT,
13111311
directive.id,
13121312
directive.span,
13131313
&format!("the item `{}` is imported redundantly", ident),
1314+
BuiltinLintDiagnostics::RedundantImport(
1315+
redundant_span.present_items().collect(),
1316+
ident,
1317+
),
13141318
);
1315-
1316-
for span in redundant_span.present_items() {
1317-
self.session.buffer_lint(
1318-
REDUNDANT_IMPORT,
1319-
directive.id,
1320-
span,
1321-
"another import"
1322-
);
1323-
}
13241319
}
13251320
}
13261321

src/test/ui/lint/use-redundant.stderr

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
warning: the item `Bar` is imported redundantly
22
--> $DIR/use-redundant.rs:14:9
33
|
4-
LL | use crate::foo::Bar; //~ WARNING redundant import
4+
LL | use crate::foo::Bar;
5+
| --------------- the item `Bar` was already imported here
6+
...
7+
LL | use crate::foo::Bar;
58
| ^^^^^^^^^^^^^^^
69
|
710
= note: #[warn(redundant_import)] on by default
811

9-
warning: another import
10-
--> $DIR/use-redundant.rs:3:5
11-
|
12-
LL | use crate::foo::Bar; //~ WARNING first import
13-
| ^^^^^^^^^^^^^^^
14-

0 commit comments

Comments
 (0)