aboutsummaryrefslogtreecommitdiff
path: root/crates/hir/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-06-13 15:29:25 +0100
committerAleksey Kladov <[email protected]>2021-06-13 15:29:25 +0100
commitf85e383b94376d55bb5ee6be375ef3dc0006590f (patch)
tree03e3c3685a893891c7b9f144597362a5e57c02ca /crates/hir/src
parentfa9ed4e0ce633e51d1411951bf044719e6837457 (diff)
internal: refactor inactive code diagnostics
Diffstat (limited to 'crates/hir/src')
-rw-r--r--crates/hir/src/diagnostics.rs32
-rw-r--r--crates/hir/src/lib.rs24
2 files changed, 15 insertions, 41 deletions
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 @@
5//! be expressed in terms of hir types themselves. 5//! be expressed in terms of hir types themselves.
6use std::any::Any; 6use std::any::Any;
7 7
8use cfg::{CfgExpr, CfgOptions, DnfExpr}; 8use cfg::{CfgExpr, CfgOptions};
9use either::Either; 9use either::Either;
10use hir_def::path::ModPath; 10use hir_def::path::ModPath;
11use hir_expand::{name::Name, HirFileId, InFile}; 11use hir_expand::{name::Name, HirFileId, InFile};
12use stdx::format_to;
13use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange}; 12use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange};
14 13
15pub use crate::diagnostics_sink::{ 14pub use crate::diagnostics_sink::{
@@ -38,6 +37,7 @@ diagnostics![
38 UnresolvedImport, 37 UnresolvedImport,
39 UnresolvedMacroCall, 38 UnresolvedMacroCall,
40 MissingFields, 39 MissingFields,
40 InactiveCode,
41]; 41];
42 42
43#[derive(Debug)] 43#[derive(Debug)]
@@ -62,39 +62,13 @@ pub struct UnresolvedMacroCall {
62 pub path: ModPath, 62 pub path: ModPath,
63} 63}
64 64
65// Diagnostic: inactive-code
66//
67// This diagnostic is shown for code with inactive `#[cfg]` attributes.
68#[derive(Debug, Clone, Eq, PartialEq)] 65#[derive(Debug, Clone, Eq, PartialEq)]
69pub struct InactiveCode { 66pub struct InactiveCode {
70 pub file: HirFileId, 67 pub node: InFile<SyntaxNodePtr>,
71 pub node: SyntaxNodePtr,
72 pub cfg: CfgExpr, 68 pub cfg: CfgExpr,
73 pub opts: CfgOptions, 69 pub opts: CfgOptions,
74} 70}
75 71
76impl Diagnostic for InactiveCode {
77 fn code(&self) -> DiagnosticCode {
78 DiagnosticCode("inactive-code")
79 }
80 fn message(&self) -> String {
81 let inactive = DnfExpr::new(self.cfg.clone()).why_inactive(&self.opts);
82 let mut buf = "code is inactive due to #[cfg] directives".to_string();
83
84 if let Some(inactive) = inactive {
85 format_to!(buf, ": {}", inactive);
86 }
87
88 buf
89 }
90 fn display_source(&self) -> InFile<SyntaxNodePtr> {
91 InFile::new(self.file, self.node.clone())
92 }
93 fn as_any(&self) -> &(dyn Any + Send + 'static) {
94 self
95 }
96}
97
98// Diagnostic: unresolved-proc-macro 72// Diagnostic: unresolved-proc-macro
99// 73//
100// This diagnostic is shown when a procedural macro can not be found. This usually means that 74// 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 {
506 506
507 DefDiagnosticKind::UnconfiguredCode { ast, cfg, opts } => { 507 DefDiagnosticKind::UnconfiguredCode { ast, cfg, opts } => {
508 let item = ast.to_node(db.upcast()); 508 let item = ast.to_node(db.upcast());
509 sink.push(InactiveCode { 509 acc.push(
510 file: ast.file_id, 510 InactiveCode {
511 node: AstPtr::new(&item).into(), 511 node: ast.with_value(AstPtr::new(&item).into()),
512 cfg: cfg.clone(), 512 cfg: cfg.clone(),
513 opts: opts.clone(), 513 opts: opts.clone(),
514 }); 514 }
515 .into(),
516 );
515 } 517 }
516 518
517 DefDiagnosticKind::UnresolvedProcMacro { ast } => { 519 DefDiagnosticKind::UnresolvedProcMacro { ast } => {
@@ -1045,12 +1047,10 @@ impl Function {
1045 let source_map = db.body_with_source_map(self.id.into()).1; 1047 let source_map = db.body_with_source_map(self.id.into()).1;
1046 for diag in source_map.diagnostics() { 1048 for diag in source_map.diagnostics() {
1047 match diag { 1049 match diag {
1048 BodyDiagnostic::InactiveCode { node, cfg, opts } => sink.push(InactiveCode { 1050 BodyDiagnostic::InactiveCode { node, cfg, opts } => acc.push(
1049 file: node.file_id, 1051 InactiveCode { node: node.clone(), cfg: cfg.clone(), opts: opts.clone() }
1050 node: node.value.clone(), 1052 .into(),
1051 cfg: cfg.clone(), 1053 ),
1052 opts: opts.clone(),
1053 }),
1054 BodyDiagnostic::MacroError { node, message } => sink.push(MacroError { 1054 BodyDiagnostic::MacroError { node, message } => sink.push(MacroError {
1055 file: node.file_id, 1055 file: node.file_id,
1056 node: node.value.clone().into(), 1056 node: node.value.clone().into(),