]> git.proxmox.com Git - rustc.git/blob - src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unresolved_import.rs
New upstream version 1.64.0+dfsg1
[rustc.git] / src / tools / rust-analyzer / crates / ide-diagnostics / src / handlers / unresolved_import.rs
1 use crate::{Diagnostic, DiagnosticsContext};
2
3 // Diagnostic: unresolved-import
4 //
5 // This diagnostic is triggered if rust-analyzer is unable to resolve a path in
6 // a `use` declaration.
7 pub(crate) fn unresolved_import(
8 ctx: &DiagnosticsContext<'_>,
9 d: &hir::UnresolvedImport,
10 ) -> Diagnostic {
11 Diagnostic::new(
12 "unresolved-import",
13 "unresolved import",
14 ctx.sema.diagnostics_display_range(d.decl.clone().map(|it| it.into())).range,
15 )
16 // This currently results in false positives in the following cases:
17 // - `cfg_if!`-generated code in libstd (we don't load the sysroot correctly)
18 // - `core::arch` (we don't handle `#[path = "../<path>"]` correctly)
19 // - proc macros and/or proc macro generated code
20 .experimental()
21 }
22
23 #[cfg(test)]
24 mod tests {
25 use crate::tests::check_diagnostics;
26
27 #[test]
28 fn unresolved_import() {
29 check_diagnostics(
30 r#"
31 use does_exist;
32 use does_not_exist;
33 //^^^^^^^^^^^^^^ error: unresolved import
34
35 mod does_exist {}
36 "#,
37 );
38 }
39
40 #[test]
41 fn unresolved_import_in_use_tree() {
42 // Only the relevant part of a nested `use` item should be highlighted.
43 check_diagnostics(
44 r#"
45 use does_exist::{Exists, DoesntExist};
46 //^^^^^^^^^^^ error: unresolved import
47
48 use {does_not_exist::*, does_exist};
49 //^^^^^^^^^^^^^^^^^ error: unresolved import
50
51 use does_not_exist::{
52 a,
53 //^ error: unresolved import
54 b,
55 //^ error: unresolved import
56 c,
57 //^ error: unresolved import
58 };
59
60 mod does_exist {
61 pub struct Exists;
62 }
63 "#,
64 );
65 }
66
67 #[test]
68 fn dedup_unresolved_import_from_unresolved_crate() {
69 check_diagnostics(
70 r#"
71 //- /main.rs crate:main
72 mod a {
73 extern crate doesnotexist;
74 //^^^^^^^^^^^^^^^^^^^^^^^^^^ error: unresolved extern crate
75
76 // Should not error, since we already errored for the missing crate.
77 use doesnotexist::{self, bla, *};
78
79 use crate::doesnotexist;
80 //^^^^^^^^^^^^^^^^^^^ error: unresolved import
81 }
82
83 mod m {
84 use super::doesnotexist;
85 //^^^^^^^^^^^^^^^^^^^ error: unresolved import
86 }
87 "#,
88 );
89 }
90 }