diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index ca189e71800..132690dcd7d 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -673,6 +673,10 @@ impl<'a, 'b> ImportResolver<'a, 'b> { self.throw_unresolved_import_error(errors.clone(), None); } + for import in &self.r.indeterminate_imports { + // Consider erroneous imports used to avoid duplicate diagnostics. + self.r.used_imports.insert((import.id, TypeNS)); + } // Report unresolved imports only if no hard error was already reported // to avoid generating multiple errors on the same import. if !has_errors { @@ -839,6 +843,10 @@ impl<'a, 'b> ImportResolver<'a, 'b> { true, directive.span, directive.crate_lint()); let no_ambiguity = self.r.ambiguity_errors.len() == prev_ambiguity_errors_len; directive.vis.set(orig_vis); + if let PathResult::Failed { .. } | PathResult::NonModule(..) = path_res { + // Consider erroneous imports used to avoid duplicate diagnostics. + self.r.used_imports.insert((directive.id, TypeNS)); + } let module = match path_res { PathResult::Module(module) => { // Consistency checks, analogous to `finalize_macro_resolutions`. diff --git a/src/test/ui/imports/unresolved-imports-used.rs b/src/test/ui/imports/unresolved-imports-used.rs index d1461e7b041..5398dd63c89 100644 --- a/src/test/ui/imports/unresolved-imports-used.rs +++ b/src/test/ui/imports/unresolved-imports-used.rs @@ -1,12 +1,18 @@ -// There should be *no* unused import errors. +// There should be *one* unused import error. #![deny(unused_imports)] mod qux { fn quz() {} + pub fn quy() {} } -use qux::quz; //~ ERROR function `quz` is private -use qux::bar; //~ ERROR unresolved import `qux::bar` -use foo::bar; //~ ERROR unresolved import `foo` +use qux::quz; //~ ERROR function `quz` is private +use qux::bar; //~ ERROR unresolved import `qux::bar` +use foo::bar; +use baz::*; +use qux::bar2; //~ ERROR unresolved import `qux::bar2` +use foo2::bar2; +use baz2::*; +use qux::quy; //~ ERROR unused import fn main() {} diff --git a/src/test/ui/imports/unresolved-imports-used.stderr b/src/test/ui/imports/unresolved-imports-used.stderr index e8c827a6179..c9342d17a49 100644 --- a/src/test/ui/imports/unresolved-imports-used.stderr +++ b/src/test/ui/imports/unresolved-imports-used.stderr @@ -1,22 +1,34 @@ error[E0432]: unresolved import `qux::bar` - --> $DIR/unresolved-imports-used.rs:9:5 + --> $DIR/unresolved-imports-used.rs:10:5 | LL | use qux::bar; | ^^^^^^^^ no `bar` in `qux` -error[E0432]: unresolved import `foo` - --> $DIR/unresolved-imports-used.rs:10:5 +error[E0432]: unresolved import `qux::bar2` + --> $DIR/unresolved-imports-used.rs:13:5 | -LL | use foo::bar; - | ^^^ maybe a missing crate `foo`? +LL | use qux::bar2; + | ^^^^^^^^^ no `bar2` in `qux` error[E0603]: function `quz` is private - --> $DIR/unresolved-imports-used.rs:8:10 + --> $DIR/unresolved-imports-used.rs:9:10 | LL | use qux::quz; | ^^^ -error: aborting due to 3 previous errors +error: unused import: `qux::quy` + --> $DIR/unresolved-imports-used.rs:16:5 + | +LL | use qux::quy; + | ^^^^^^^^ + | +note: lint level defined here + --> $DIR/unresolved-imports-used.rs:2:9 + | +LL | #![deny(unused_imports)] + | ^^^^^^^^^^^^^^ + +error: aborting due to 4 previous errors Some errors have detailed explanations: E0432, E0603. For more information about an error, try `rustc --explain E0432`.