From 1e4aaee7bbc1d56698e70158aa35f578422623d9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 17:51:44 +0300 Subject: internal: refactor unresolved proc macro diagnostic --- crates/ide/src/diagnostics.rs | 17 +++++------- crates/ide/src/diagnostics/inactive_code.rs | 6 ++++- .../ide/src/diagnostics/unresolved_proc_macro.rs | 30 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 crates/ide/src/diagnostics/unresolved_proc_macro.rs (limited to 'crates/ide/src') diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index 634e6a043..f7965326d 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs @@ -8,6 +8,7 @@ mod unresolved_module; mod unresolved_extern_crate; mod unresolved_import; mod unresolved_macro_call; +mod unresolved_proc_macro; mod inactive_code; mod missing_fields; @@ -68,6 +69,11 @@ impl Diagnostic { self } + fn severity(mut self, severity: Severity) -> Diagnostic { + self.severity = severity; + self + } + fn error(range: TextRange, message: String) -> Self { Self { message, @@ -178,16 +184,6 @@ pub(crate) fn diagnostics( .with_code(Some(d.code())), ); }) - .on::(|d| { - // Use more accurate position if available. - let display_range = d - .precise_location - .unwrap_or_else(|| sema.diagnostics_display_range(d.display_source()).range); - - // FIXME: it would be nice to tell the user whether proc macros are currently disabled - res.borrow_mut() - .push(Diagnostic::hint(display_range, d.message()).with_code(Some(d.code()))); - }) .on::(|d| { let display_range = sema.diagnostics_display_range(d.display_source()).range; res.borrow_mut() @@ -231,6 +227,7 @@ pub(crate) fn diagnostics( AnyDiagnostic::UnresolvedExternCrate(d) => unresolved_extern_crate::unresolved_extern_crate(&ctx, &d), AnyDiagnostic::UnresolvedImport(d) => unresolved_import::unresolved_import(&ctx, &d), AnyDiagnostic::UnresolvedMacroCall(d) => unresolved_macro_call::unresolved_macro_call(&ctx, &d), + AnyDiagnostic::UnresolvedProcMacro(d) => unresolved_proc_macro::unresolved_proc_macro(&ctx, &d), AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d), AnyDiagnostic::InactiveCode(d) => match inactive_code::inactive_code(&ctx, &d) { diff --git a/crates/ide/src/diagnostics/inactive_code.rs b/crates/ide/src/diagnostics/inactive_code.rs index 52f97cb4c..afe333204 100644 --- a/crates/ide/src/diagnostics/inactive_code.rs +++ b/crates/ide/src/diagnostics/inactive_code.rs @@ -1,7 +1,10 @@ use cfg::DnfExpr; use stdx::format_to; -use crate::diagnostics::{Diagnostic, DiagnosticsContext}; +use crate::{ + diagnostics::{Diagnostic, DiagnosticsContext}, + Severity, +}; // Diagnostic: inactive-code // @@ -27,6 +30,7 @@ pub(super) fn inactive_code( message, ctx.sema.diagnostics_display_range(d.node.clone()).range, ) + .severity(Severity::WeakWarning) .with_unused(true); Some(res) } diff --git a/crates/ide/src/diagnostics/unresolved_proc_macro.rs b/crates/ide/src/diagnostics/unresolved_proc_macro.rs new file mode 100644 index 000000000..3dc6ab451 --- /dev/null +++ b/crates/ide/src/diagnostics/unresolved_proc_macro.rs @@ -0,0 +1,30 @@ +use crate::{ + diagnostics::{Diagnostic, DiagnosticsContext}, + Severity, +}; + +// Diagnostic: unresolved-proc-macro +// +// This diagnostic is shown when a procedural macro can not be found. This usually means that +// procedural macro support is simply disabled (and hence is only a weak hint instead of an error), +// but can also indicate project setup problems. +// +// If you are seeing a lot of "proc macro not expanded" warnings, you can add this option to the +// `rust-analyzer.diagnostics.disabled` list to prevent them from showing. Alternatively you can +// enable support for procedural macros (see `rust-analyzer.procMacro.enable`). +pub(super) fn unresolved_proc_macro( + ctx: &DiagnosticsContext<'_>, + d: &hir::UnresolvedProcMacro, +) -> Diagnostic { + // Use more accurate position if available. + let display_range = d + .precise_location + .unwrap_or_else(|| ctx.sema.diagnostics_display_range(d.node.clone()).range); + // FIXME: it would be nice to tell the user whether proc macros are currently disabled + let message = match &d.macro_name { + Some(name) => format!("proc macro `{}` not expanded", name), + None => "proc macro not expanded".to_string(), + }; + + Diagnostic::new("unresolved-proc-macro", message, display_range).severity(Severity::WeakWarning) +} -- cgit v1.2.3