From f85e383b94376d55bb5ee6be375ef3dc0006590f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 17:29:25 +0300 Subject: internal: refactor inactive code diagnostics --- crates/hir/src/diagnostics.rs | 32 +++----------------------------- 1 file changed, 3 insertions(+), 29 deletions(-) (limited to 'crates/hir/src/diagnostics.rs') diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 718c86b3a..03e7f5e84 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -5,11 +5,10 @@ //! be expressed in terms of hir types themselves. use std::any::Any; -use cfg::{CfgExpr, CfgOptions, DnfExpr}; +use cfg::{CfgExpr, CfgOptions}; use either::Either; use hir_def::path::ModPath; use hir_expand::{name::Name, HirFileId, InFile}; -use stdx::format_to; use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange}; pub use crate::diagnostics_sink::{ @@ -38,6 +37,7 @@ diagnostics![ UnresolvedImport, UnresolvedMacroCall, MissingFields, + InactiveCode, ]; #[derive(Debug)] @@ -62,39 +62,13 @@ pub struct UnresolvedMacroCall { pub path: ModPath, } -// Diagnostic: inactive-code -// -// This diagnostic is shown for code with inactive `#[cfg]` attributes. #[derive(Debug, Clone, Eq, PartialEq)] pub struct InactiveCode { - pub file: HirFileId, - pub node: SyntaxNodePtr, + pub node: InFile, pub cfg: CfgExpr, pub opts: CfgOptions, } -impl Diagnostic for InactiveCode { - fn code(&self) -> DiagnosticCode { - DiagnosticCode("inactive-code") - } - fn message(&self) -> String { - let inactive = DnfExpr::new(self.cfg.clone()).why_inactive(&self.opts); - let mut buf = "code is inactive due to #[cfg] directives".to_string(); - - if let Some(inactive) = inactive { - format_to!(buf, ": {}", inactive); - } - - buf - } - fn display_source(&self) -> InFile { - InFile::new(self.file, self.node.clone()) - } - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } -} - // Diagnostic: unresolved-proc-macro // // This diagnostic is shown when a procedural macro can not be found. This usually means that -- cgit v1.2.3 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/hir/src/diagnostics.rs | 34 ++-------------------------------- 1 file changed, 2 insertions(+), 32 deletions(-) (limited to 'crates/hir/src/diagnostics.rs') diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 03e7f5e84..2039d2b43 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -36,6 +36,7 @@ diagnostics![ UnresolvedExternCrate, UnresolvedImport, UnresolvedMacroCall, + UnresolvedProcMacro, MissingFields, InactiveCode, ]; @@ -69,46 +70,15 @@ pub struct InactiveCode { pub opts: CfgOptions, } -// 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`). #[derive(Debug, Clone, Eq, PartialEq)] pub struct UnresolvedProcMacro { - pub file: HirFileId, - pub node: SyntaxNodePtr, + pub node: InFile, /// If the diagnostic can be pinpointed more accurately than via `node`, this is the `TextRange` /// to use instead. pub precise_location: Option, pub macro_name: Option, } -impl Diagnostic for UnresolvedProcMacro { - fn code(&self) -> DiagnosticCode { - DiagnosticCode("unresolved-proc-macro") - } - - fn message(&self) -> String { - match &self.macro_name { - Some(name) => format!("proc macro `{}` not expanded", name), - None => "proc macro not expanded".to_string(), - } - } - - fn display_source(&self) -> InFile { - InFile::new(self.file, self.node.clone()) - } - - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } -} - // Diagnostic: macro-error // // This diagnostic is shown for macro expansion errors. -- cgit v1.2.3 From 00303284b5cc3a82e32dc3ecbbcfeb2f99de6818 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 18:41:04 +0300 Subject: internal: refactor macro error --- crates/hir/src/diagnostics.rs | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) (limited to 'crates/hir/src/diagnostics.rs') diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 2039d2b43..28580eeb4 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -37,6 +37,7 @@ diagnostics![ UnresolvedImport, UnresolvedMacroCall, UnresolvedProcMacro, + MacroError, MissingFields, InactiveCode, ]; @@ -79,35 +80,12 @@ pub struct UnresolvedProcMacro { pub macro_name: Option, } -// Diagnostic: macro-error -// -// This diagnostic is shown for macro expansion errors. #[derive(Debug, Clone, Eq, PartialEq)] pub struct MacroError { - pub file: HirFileId, - pub node: SyntaxNodePtr, + pub node: InFile, pub message: String, } -impl Diagnostic for MacroError { - fn code(&self) -> DiagnosticCode { - DiagnosticCode("macro-error") - } - fn message(&self) -> String { - self.message.clone() - } - fn display_source(&self) -> InFile { - InFile::new(self.file, self.node.clone()) - } - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } - fn is_experimental(&self) -> bool { - // Newly added and not very well-tested, might contain false positives. - true - } -} - #[derive(Debug)] pub struct UnimplementedBuiltinMacro { pub file: HirFileId, -- cgit v1.2.3