diff options
Diffstat (limited to 'crates/ide/src/diagnostics/unresolved_import.rs')
-rw-r--r-- | crates/ide/src/diagnostics/unresolved_import.rs | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/crates/ide/src/diagnostics/unresolved_import.rs b/crates/ide/src/diagnostics/unresolved_import.rs new file mode 100644 index 000000000..1cbf96ba1 --- /dev/null +++ b/crates/ide/src/diagnostics/unresolved_import.rs | |||
@@ -0,0 +1,90 @@ | |||
1 | use crate::diagnostics::{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(super) 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::diagnostics::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 | //^^^^^^^^^^^^^^ 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 | //^^^^^^^^^^^ unresolved import | ||
47 | |||
48 | use {does_not_exist::*, does_exist}; | ||
49 | //^^^^^^^^^^^^^^^^^ unresolved import | ||
50 | |||
51 | use does_not_exist::{ | ||
52 | a, | ||
53 | //^ unresolved import | ||
54 | b, | ||
55 | //^ unresolved import | ||
56 | c, | ||
57 | //^ 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 | //^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 | //^^^^^^^^^^^^^^^^^^^ unresolved import | ||
81 | } | ||
82 | |||
83 | mod m { | ||
84 | use super::doesnotexist; | ||
85 | //^^^^^^^^^^^^^^^^^^^ unresolved import | ||
86 | } | ||
87 | "#, | ||
88 | ); | ||
89 | } | ||
90 | } | ||