aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_diagnostics/src/unresolved_macro_call.rs
blob: 88133d0f3be66f489a34bad1e51f3e2a7e17393b (plain)
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
use hir::{db::AstDatabase, InFile};
use syntax::{AstNode, SyntaxNodePtr};

use crate::{Diagnostic, DiagnosticsContext};

// Diagnostic: unresolved-macro-call
//
// This diagnostic is triggered if rust-analyzer is unable to resolve the path
// to a macro in a macro invocation.
pub(super) fn unresolved_macro_call(
    ctx: &DiagnosticsContext<'_>,
    d: &hir::UnresolvedMacroCall,
) -> Diagnostic {
    let last_path_segment = ctx.sema.db.parse_or_expand(d.macro_call.file_id).and_then(|root| {
        d.macro_call
            .value
            .to_node(&root)
            .path()
            .and_then(|it| it.segment())
            .and_then(|it| it.name_ref())
            .map(|it| InFile::new(d.macro_call.file_id, SyntaxNodePtr::new(it.syntax())))
    });
    let diagnostics = last_path_segment.unwrap_or_else(|| d.macro_call.clone().map(|it| it.into()));

    Diagnostic::new(
        "unresolved-macro-call",
        format!("unresolved macro `{}!`", d.path),
        ctx.sema.diagnostics_display_range(diagnostics).range,
    )
    .experimental()
}

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

    #[test]
    fn unresolved_macro_diag() {
        check_diagnostics(
            r#"
fn f() {
    m!();
} //^ unresolved macro `m!`

"#,
        );
    }

    #[test]
    fn test_unresolved_macro_range() {
        check_diagnostics(
            r#"
foo::bar!(92);
   //^^^ unresolved macro `foo::bar!`
"#,
        );
    }

    #[test]
    fn unresolved_legacy_scope_macro() {
        check_diagnostics(
            r#"
macro_rules! m { () => {} }

m!(); m2!();
    //^^ unresolved macro `self::m2!`
"#,
        );
    }

    #[test]
    fn unresolved_module_scope_macro() {
        check_diagnostics(
            r#"
mod mac {
#[macro_export]
macro_rules! m { () => {} } }

self::m!(); self::m2!();
                //^^ unresolved macro `self::m2!`
"#,
        );
    }
}