]> git.lizzy.rs Git - rust.git/blob - crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs
37350a7aaf17038e0a807dc4bc46d66096d5c964
[rust.git] / crates / ide-diagnostics / src / handlers / unresolved_proc_macro.rs
1 use crate::{Diagnostic, DiagnosticsContext, Severity};
2
3 // Diagnostic: unresolved-proc-macro
4 //
5 // This diagnostic is shown when a procedural macro can not be found. This usually means that
6 // procedural macro support is simply disabled (and hence is only a weak hint instead of an error),
7 // but can also indicate project setup problems.
8 //
9 // If you are seeing a lot of "proc macro not expanded" warnings, you can add this option to the
10 // `rust-analyzer.diagnostics.disabled` list to prevent them from showing. Alternatively you can
11 // enable support for procedural macros (see `rust-analyzer.procMacro.attributes.enable`).
12 pub(crate) fn unresolved_proc_macro(
13     ctx: &DiagnosticsContext<'_>,
14     d: &hir::UnresolvedProcMacro,
15     proc_macros_enabled: bool,
16     proc_attr_macros_enabled: bool,
17 ) -> Diagnostic {
18     // Use more accurate position if available.
19     let display_range = d
20         .precise_location
21         .unwrap_or_else(|| ctx.sema.diagnostics_display_range(d.node.clone()).range);
22
23     let config_enabled = match d.kind {
24         hir::MacroKind::Attr => proc_macros_enabled && proc_attr_macros_enabled,
25         _ => proc_macros_enabled,
26     };
27
28     let message = match &d.macro_name {
29         Some(name) => format!("proc macro `{}` not expanded", name),
30         None => "proc macro not expanded".to_string(),
31     };
32     let (message, severity) = if config_enabled {
33         (message, Severity::Error)
34     } else {
35         let message = match d.kind {
36             hir::MacroKind::Attr if proc_macros_enabled => {
37                 format!("{message}{}", " (attribute macro expansion is disabled)")
38             }
39             _ => {
40                 format!("{message}{}", " (proc-macro expansion is disabled)")
41             }
42         };
43         (message, Severity::WeakWarning)
44     };
45
46     Diagnostic::new("unresolved-proc-macro", message, display_range).severity(severity)
47 }