ide_diagnostics/handlers/
unresolved_import.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};

// Diagnostic: unresolved-import
//
// This diagnostic is triggered if rust-analyzer is unable to resolve a path in
// a `use` declaration.
pub(crate) fn unresolved_import(
    ctx: &DiagnosticsContext<'_>,
    d: &hir::UnresolvedImport,
) -> Diagnostic {
    Diagnostic::new_with_syntax_node_ptr(
        ctx,
        DiagnosticCode::RustcHardError("E0432"),
        "unresolved import",
        d.decl.map(|it| it.into()),
    )
    // This currently results in false positives in the following cases:
    // - `cfg_if!`-generated code in libstd (we don't load the sysroot correctly)
    // - `core::arch` (we don't handle `#[path = "../<path>"]` correctly)
    // - proc macros and/or proc macro generated code
    .experimental()
}

#[cfg(test)]
mod tests {
    use crate::tests::check_diagnostics;

    #[test]
    fn unresolved_import() {
        check_diagnostics(
            r#"
use does_exist;
use does_not_exist;
  //^^^^^^^^^^^^^^ error: unresolved import

mod does_exist {}
"#,
        );
    }

    #[test]
    fn unresolved_import_in_use_tree() {
        // Only the relevant part of a nested `use` item should be highlighted.
        check_diagnostics(
            r#"
use does_exist::{Exists, DoesntExist};
                       //^^^^^^^^^^^ error: unresolved import

use {does_not_exist::*, does_exist};
   //^^^^^^^^^^^^^^^^^ error: unresolved import

use does_not_exist::{
    a,
  //^ error: unresolved import
    b,
  //^ error: unresolved import
    c,
  //^ error: unresolved import
};

mod does_exist {
    pub struct Exists;
}
"#,
        );
    }

    #[test]
    fn dedup_unresolved_import_from_unresolved_crate() {
        check_diagnostics(
            r#"
//- /main.rs crate:main
mod a {
    extern crate doesnotexist;
  //^^^^^^^^^^^^^^^^^^^^^^^^^^ error: unresolved extern crate

    // Should not error, since we already errored for the missing crate.
    use doesnotexist::{self, bla, *};

    use crate::doesnotexist;
      //^^^^^^^^^^^^^^^^^^^ error: unresolved import
}

mod m {
    use super::doesnotexist;
      //^^^^^^^^^^^^^^^^^^^ error: unresolved import
}
"#,
        );
    }
}