Skip to content

Commit fef7e9d

Browse files
Improve warning
1 parent 1129a34 commit fef7e9d

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
@@ -488,6 +488,7 @@ pub enum BuiltinLintDiagnostics {
488488
UnknownCrateTypes(Span, String, String),
489489
UnusedImports(String, Vec<(Span, String)>),
490490
NestedImplTrait { outer_impl_trait_span: Span, inner_impl_trait_span: Span },
491+
RedundantImport(Vec<Span>, ast::Ident),
491492
}
492493

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

src/librustc_resolve/resolve_imports.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,21 +1309,16 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
13091309
if !is_redundant.is_empty() &&
13101310
is_redundant.present_items().all(|is_redundant| is_redundant)
13111311
{
1312-
self.session.buffer_lint(
1312+
self.session.buffer_lint_with_diagnostic(
13131313
REDUNDANT_IMPORT,
13141314
directive.id,
13151315
directive.span,
13161316
&format!("the item `{}` is imported redundantly", ident),
1317+
BuiltinLintDiagnostics::RedundantImport(
1318+
redundant_span.present_items().collect(),
1319+
ident,
1320+
),
13171321
);
1318-
1319-
for span in redundant_span.present_items() {
1320-
self.session.buffer_lint(
1321-
REDUNDANT_IMPORT,
1322-
directive.id,
1323-
span,
1324-
"another import"
1325-
);
1326-
}
13271322
}
13281323
}
13291324

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)