aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Aleksanov <[email protected]>2020-08-07 11:18:47 +0100
committerIgor Aleksanov <[email protected]>2020-08-07 11:18:47 +0100
commitc463d217a1e001abe6a812f309d93527e28a70c6 (patch)
treee72e74fe56971568b4b92ee4c5a38ea0bba3c844
parentf1d507270c7d915ef0177feca7b6745d95169ac8 (diff)
Add names for diagnostics and add a possibility to disable them
-rw-r--r--crates/ra_hir_def/src/diagnostics.rs3
-rw-r--r--crates/ra_hir_expand/src/diagnostics.rs18
-rw-r--r--crates/ra_hir_ty/src/diagnostics.rs25
3 files changed, 44 insertions, 2 deletions
diff --git a/crates/ra_hir_def/src/diagnostics.rs b/crates/ra_hir_def/src/diagnostics.rs
index 30db48f86..1d28c24e8 100644
--- a/crates/ra_hir_def/src/diagnostics.rs
+++ b/crates/ra_hir_def/src/diagnostics.rs
@@ -15,6 +15,9 @@ pub struct UnresolvedModule {
15} 15}
16 16
17impl Diagnostic for UnresolvedModule { 17impl Diagnostic for UnresolvedModule {
18 fn name(&self) -> String {
19 "unresolved-module".to_string()
20 }
18 fn message(&self) -> String { 21 fn message(&self) -> String {
19 "unresolved module".to_string() 22 "unresolved module".to_string()
20 } 23 }
diff --git a/crates/ra_hir_expand/src/diagnostics.rs b/crates/ra_hir_expand/src/diagnostics.rs
index 84ba97b14..bf9fb081a 100644
--- a/crates/ra_hir_expand/src/diagnostics.rs
+++ b/crates/ra_hir_expand/src/diagnostics.rs
@@ -14,13 +14,14 @@
14//! subsystem provides a separate, non-query-based API which can walk all stored 14//! subsystem provides a separate, non-query-based API which can walk all stored
15//! values and transform them into instances of `Diagnostic`. 15//! values and transform them into instances of `Diagnostic`.
16 16
17use std::{any::Any, fmt}; 17use std::{any::Any, collections::HashSet, fmt};
18 18
19use ra_syntax::{SyntaxNode, SyntaxNodePtr}; 19use ra_syntax::{SyntaxNode, SyntaxNodePtr};
20 20
21use crate::{db::AstDatabase, InFile}; 21use crate::{db::AstDatabase, InFile};
22 22
23pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { 23pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
24 fn name(&self) -> String;
24 fn message(&self) -> String; 25 fn message(&self) -> String;
25 fn source(&self) -> InFile<SyntaxNodePtr>; 26 fn source(&self) -> InFile<SyntaxNodePtr>;
26 fn as_any(&self) -> &(dyn Any + Send + 'static); 27 fn as_any(&self) -> &(dyn Any + Send + 'static);
@@ -49,10 +50,16 @@ pub struct DiagnosticSink<'a> {
49 callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>, 50 callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>,
50 filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>, 51 filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>,
51 default_callback: Box<dyn FnMut(&dyn Diagnostic) + 'a>, 52 default_callback: Box<dyn FnMut(&dyn Diagnostic) + 'a>,
53 disabled_diagnostics: HashSet<String>,
52} 54}
53 55
54impl<'a> DiagnosticSink<'a> { 56impl<'a> DiagnosticSink<'a> {
55 pub fn push(&mut self, d: impl Diagnostic) { 57 pub fn push(&mut self, d: impl Diagnostic) {
58 if self.disabled_diagnostics.contains(&d.name()) {
59 // This diagnostic is disabled, ignore it completely.
60 return;
61 }
62
56 let d: &dyn Diagnostic = &d; 63 let d: &dyn Diagnostic = &d;
57 self._push(d); 64 self._push(d);
58 } 65 }
@@ -76,11 +83,12 @@ impl<'a> DiagnosticSink<'a> {
76pub struct DiagnosticSinkBuilder<'a> { 83pub struct DiagnosticSinkBuilder<'a> {
77 callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>, 84 callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>,
78 filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>, 85 filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>,
86 disabled_diagnostics: HashSet<String>,
79} 87}
80 88
81impl<'a> DiagnosticSinkBuilder<'a> { 89impl<'a> DiagnosticSinkBuilder<'a> {
82 pub fn new() -> Self { 90 pub fn new() -> Self {
83 Self { callbacks: Vec::new(), filters: Vec::new() } 91 Self { callbacks: Vec::new(), filters: Vec::new(), disabled_diagnostics: HashSet::new() }
84 } 92 }
85 93
86 pub fn filter<F: FnMut(&dyn Diagnostic) -> bool + 'a>(mut self, cb: F) -> Self { 94 pub fn filter<F: FnMut(&dyn Diagnostic) -> bool + 'a>(mut self, cb: F) -> Self {
@@ -100,11 +108,17 @@ impl<'a> DiagnosticSinkBuilder<'a> {
100 self 108 self
101 } 109 }
102 110
111 pub fn disable_diagnostic(mut self, diagnostic: impl Into<String>) -> Self {
112 self.disabled_diagnostics.insert(diagnostic.into());
113 self
114 }
115
103 pub fn build<F: FnMut(&dyn Diagnostic) + 'a>(self, default_callback: F) -> DiagnosticSink<'a> { 116 pub fn build<F: FnMut(&dyn Diagnostic) + 'a>(self, default_callback: F) -> DiagnosticSink<'a> {
104 DiagnosticSink { 117 DiagnosticSink {
105 callbacks: self.callbacks, 118 callbacks: self.callbacks,
106 filters: self.filters, 119 filters: self.filters,
107 default_callback: Box::new(default_callback), 120 default_callback: Box::new(default_callback),
121 disabled_diagnostics: self.disabled_diagnostics,
108 } 122 }
109 } 123 }
110} 124}
diff --git a/crates/ra_hir_ty/src/diagnostics.rs b/crates/ra_hir_ty/src/diagnostics.rs
index 977c0525b..0b3e16ae7 100644
--- a/crates/ra_hir_ty/src/diagnostics.rs
+++ b/crates/ra_hir_ty/src/diagnostics.rs
@@ -33,6 +33,10 @@ pub struct NoSuchField {
33} 33}
34 34
35impl Diagnostic for NoSuchField { 35impl Diagnostic for NoSuchField {
36 fn name(&self) -> String {
37 "no-such-field".to_string()
38 }
39
36 fn message(&self) -> String { 40 fn message(&self) -> String {
37 "no such field".to_string() 41 "no such field".to_string()
38 } 42 }
@@ -64,6 +68,9 @@ pub struct MissingFields {
64} 68}
65 69
66impl Diagnostic for MissingFields { 70impl Diagnostic for MissingFields {
71 fn name(&self) -> String {
72 "missing-structure-fields".to_string()
73 }
67 fn message(&self) -> String { 74 fn message(&self) -> String {
68 let mut buf = String::from("Missing structure fields:\n"); 75 let mut buf = String::from("Missing structure fields:\n");
69 for field in &self.missed_fields { 76 for field in &self.missed_fields {
@@ -97,6 +104,9 @@ pub struct MissingPatFields {
97} 104}
98 105
99impl Diagnostic for MissingPatFields { 106impl Diagnostic for MissingPatFields {
107 fn name(&self) -> String {
108 "missing-pat-fields".to_string()
109 }
100 fn message(&self) -> String { 110 fn message(&self) -> String {
101 let mut buf = String::from("Missing structure fields:\n"); 111 let mut buf = String::from("Missing structure fields:\n");
102 for field in &self.missed_fields { 112 for field in &self.missed_fields {
@@ -120,6 +130,9 @@ pub struct MissingMatchArms {
120} 130}
121 131
122impl Diagnostic for MissingMatchArms { 132impl Diagnostic for MissingMatchArms {
133 fn name(&self) -> String {
134 "missing-match-arm".to_string()
135 }
123 fn message(&self) -> String { 136 fn message(&self) -> String {
124 String::from("Missing match arm") 137 String::from("Missing match arm")
125 } 138 }
@@ -138,6 +151,9 @@ pub struct MissingOkInTailExpr {
138} 151}
139 152
140impl Diagnostic for MissingOkInTailExpr { 153impl Diagnostic for MissingOkInTailExpr {
154 fn name(&self) -> String {
155 "missing-ok-in-tail-expr".to_string()
156 }
141 fn message(&self) -> String { 157 fn message(&self) -> String {
142 "wrap return expression in Ok".to_string() 158 "wrap return expression in Ok".to_string()
143 } 159 }
@@ -166,6 +182,9 @@ pub struct BreakOutsideOfLoop {
166} 182}
167 183
168impl Diagnostic for BreakOutsideOfLoop { 184impl Diagnostic for BreakOutsideOfLoop {
185 fn name(&self) -> String {
186 "break-outside-of-loop".to_string()
187 }
169 fn message(&self) -> String { 188 fn message(&self) -> String {
170 "break outside of loop".to_string() 189 "break outside of loop".to_string()
171 } 190 }
@@ -194,6 +213,9 @@ pub struct MissingUnsafe {
194} 213}
195 214
196impl Diagnostic for MissingUnsafe { 215impl Diagnostic for MissingUnsafe {
216 fn name(&self) -> String {
217 "missing-unsafe".to_string()
218 }
197 fn message(&self) -> String { 219 fn message(&self) -> String {
198 format!("This operation is unsafe and requires an unsafe function or block") 220 format!("This operation is unsafe and requires an unsafe function or block")
199 } 221 }
@@ -224,6 +246,9 @@ pub struct MismatchedArgCount {
224} 246}
225 247
226impl Diagnostic for MismatchedArgCount { 248impl Diagnostic for MismatchedArgCount {
249 fn name(&self) -> String {
250 "mismatched-arg-count".to_string()
251 }
227 fn message(&self) -> String { 252 fn message(&self) -> String {
228 let s = if self.expected == 1 { "" } else { "s" }; 253 let s = if self.expected == 1 { "" } else { "s" };
229 format!("Expected {} argument{}, found {}", self.expected, s, self.found) 254 format!("Expected {} argument{}, found {}", self.expected, s, self.found)