ide_diagnostics/handlers/
unresolved_ident.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
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};

// Diagnostic: unresolved-ident
//
// This diagnostic is triggered if an expr-position ident is invalid.
pub(crate) fn unresolved_ident(
    ctx: &DiagnosticsContext<'_>,
    d: &hir::UnresolvedIdent,
) -> Diagnostic {
    let mut range =
        ctx.sema.diagnostics_display_range(d.node.map(|(node, _)| node.syntax_node_ptr()));
    if let Some(in_node_range) = d.node.value.1 {
        range.range = in_node_range + range.range.start();
    }
    Diagnostic::new(DiagnosticCode::RustcHardError("E0425"), "no such value in this scope", range)
        .experimental()
}

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

    #[test]
    fn feature() {
        check_diagnostics(
            r#"
//- minicore: fmt
fn main() {
    format_args!("{unresolved}");
                // ^^^^^^^^^^ error: no such value in this scope
}
"#,
        )
    }

    #[test]
    fn missing() {
        check_diagnostics(
            r#"
fn main() {
    let _ = x;
          //^ error: no such value in this scope
}
"#,
        );
    }

    #[test]
    fn present() {
        check_diagnostics(
            r#"
fn main() {
    let x = 5;
    let _ = x;
}
"#,
        );
    }

    #[test]
    fn unresolved_self_val() {
        check_diagnostics(
            r#"
fn main() {
    self.a;
  //^^^^ error: no such value in this scope
    let self:
         self =
            self;
          //^^^^ error: no such value in this scope
}
"#,
        );
    }
}