Skip to content

Commit 7dc3839

Browse files
committed
resolve: Mark more erroneous imports as used
1 parent ef54f57 commit 7dc3839

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

Diff for: src/librustc_resolve/resolve_imports.rs

+8
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,10 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
673673
self.throw_unresolved_import_error(errors.clone(), None);
674674
}
675675

676+
for import in &self.r.indeterminate_imports {
677+
// Consider erroneous imports used to avoid duplicate diagnostics.
678+
self.r.used_imports.insert((import.id, TypeNS));
679+
}
676680
// Report unresolved imports only if no hard error was already reported
677681
// to avoid generating multiple errors on the same import.
678682
if !has_errors {
@@ -839,6 +843,10 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
839843
true, directive.span, directive.crate_lint());
840844
let no_ambiguity = self.r.ambiguity_errors.len() == prev_ambiguity_errors_len;
841845
directive.vis.set(orig_vis);
846+
if let PathResult::Failed { .. } | PathResult::NonModule(..) = path_res {
847+
// Consider erroneous imports used to avoid duplicate diagnostics.
848+
self.r.used_imports.insert((directive.id, TypeNS));
849+
}
842850
let module = match path_res {
843851
PathResult::Module(module) => {
844852
// Consistency checks, analogous to `finalize_macro_resolutions`.

Diff for: src/test/ui/imports/unresolved-imports-used.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
// There should be *no* unused import errors.
1+
// There should be *one* unused import error.
22
#![deny(unused_imports)]
33

44
mod qux {
55
fn quz() {}
6+
pub fn quy() {}
67
}
78

8-
use qux::quz; //~ ERROR function `quz` is private
9-
use qux::bar; //~ ERROR unresolved import `qux::bar`
10-
use foo::bar; //~ ERROR unresolved import `foo`
9+
use qux::quz; //~ ERROR function `quz` is private
10+
use qux::bar; //~ ERROR unresolved import `qux::bar`
11+
use foo::bar;
12+
use baz::*;
13+
use qux::bar2; //~ ERROR unresolved import `qux::bar2`
14+
use foo2::bar2;
15+
use baz2::*;
16+
use qux::quy; //~ ERROR unused import
1117

1218
fn main() {}

Diff for: src/test/ui/imports/unresolved-imports-used.stderr

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
error[E0432]: unresolved import `qux::bar`
2-
--> $DIR/unresolved-imports-used.rs:9:5
2+
--> $DIR/unresolved-imports-used.rs:10:5
33
|
44
LL | use qux::bar;
55
| ^^^^^^^^ no `bar` in `qux`
66

7-
error[E0432]: unresolved import `foo`
8-
--> $DIR/unresolved-imports-used.rs:10:5
7+
error[E0432]: unresolved import `qux::bar2`
8+
--> $DIR/unresolved-imports-used.rs:13:5
99
|
10-
LL | use foo::bar;
11-
| ^^^ maybe a missing crate `foo`?
10+
LL | use qux::bar2;
11+
| ^^^^^^^^^ no `bar2` in `qux`
1212

1313
error[E0603]: function `quz` is private
14-
--> $DIR/unresolved-imports-used.rs:8:10
14+
--> $DIR/unresolved-imports-used.rs:9:10
1515
|
1616
LL | use qux::quz;
1717
| ^^^
1818

19-
error: aborting due to 3 previous errors
19+
error: unused import: `qux::quy`
20+
--> $DIR/unresolved-imports-used.rs:16:5
21+
|
22+
LL | use qux::quy;
23+
| ^^^^^^^^
24+
|
25+
note: lint level defined here
26+
--> $DIR/unresolved-imports-used.rs:2:9
27+
|
28+
LL | #![deny(unused_imports)]
29+
| ^^^^^^^^^^^^^^
30+
31+
error: aborting due to 4 previous errors
2032

2133
Some errors have detailed explanations: E0432, E0603.
2234
For more information about an error, try `rustc --explain E0432`.

0 commit comments

Comments
 (0)