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 +++----------------------------- crates/hir/src/lib.rs | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 41 deletions(-) (limited to 'crates/hir') 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 diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 0a9414013..d59b52b25 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -506,12 +506,14 @@ impl Module { DefDiagnosticKind::UnconfiguredCode { ast, cfg, opts } => { let item = ast.to_node(db.upcast()); - sink.push(InactiveCode { - file: ast.file_id, - node: AstPtr::new(&item).into(), - cfg: cfg.clone(), - opts: opts.clone(), - }); + acc.push( + InactiveCode { + node: ast.with_value(AstPtr::new(&item).into()), + cfg: cfg.clone(), + opts: opts.clone(), + } + .into(), + ); } DefDiagnosticKind::UnresolvedProcMacro { ast } => { @@ -1045,12 +1047,10 @@ impl Function { let source_map = db.body_with_source_map(self.id.into()).1; for diag in source_map.diagnostics() { match diag { - BodyDiagnostic::InactiveCode { node, cfg, opts } => sink.push(InactiveCode { - file: node.file_id, - node: node.value.clone(), - cfg: cfg.clone(), - opts: opts.clone(), - }), + BodyDiagnostic::InactiveCode { node, cfg, opts } => acc.push( + InactiveCode { node: node.clone(), cfg: cfg.clone(), opts: opts.clone() } + .into(), + ), BodyDiagnostic::MacroError { node, message } => sink.push(MacroError { file: node.file_id, node: node.value.clone().into(), -- cgit v1.2.3