aboutsummaryrefslogtreecommitdiff
path: root/crates/ide
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide')
-rw-r--r--crates/ide/src/diagnostics.rs48
1 files changed, 13 insertions, 35 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs
index fe6236e44..c024e3e1e 100644
--- a/crates/ide/src/diagnostics.rs
+++ b/crates/ide/src/diagnostics.rs
@@ -26,12 +26,7 @@ mod unresolved_proc_macro;
26 26
27mod field_shorthand; 27mod field_shorthand;
28 28
29use std::cell::RefCell; 29use hir::{diagnostics::AnyDiagnostic, Semantics};
30
31use hir::{
32 diagnostics::{AnyDiagnostic, DiagnosticCode, DiagnosticSinkBuilder},
33 Semantics,
34};
35use ide_assists::AssistResolveStrategy; 30use ide_assists::AssistResolveStrategy;
36use ide_db::{base_db::SourceDatabase, RootDatabase}; 31use ide_db::{base_db::SourceDatabase, RootDatabase};
37use itertools::Itertools; 32use itertools::Itertools;
@@ -45,6 +40,15 @@ use unlinked_file::UnlinkedFile;
45 40
46use crate::{Assist, AssistId, AssistKind, FileId, Label, SourceChange}; 41use crate::{Assist, AssistId, AssistKind, FileId, Label, SourceChange};
47 42
43#[derive(Copy, Clone, Debug, PartialEq)]
44pub struct DiagnosticCode(pub &'static str);
45
46impl DiagnosticCode {
47 pub fn as_str(&self) -> &str {
48 self.0
49 }
50}
51
48#[derive(Debug)] 52#[derive(Debug)]
49pub struct Diagnostic { 53pub struct Diagnostic {
50 // pub name: Option<String>, 54 // pub name: Option<String>,
@@ -113,10 +117,6 @@ impl Diagnostic {
113 fn with_unused(self, unused: bool) -> Self { 117 fn with_unused(self, unused: bool) -> Self {
114 Self { unused, ..self } 118 Self { unused, ..self }
115 } 119 }
116
117 fn with_code(self, code: Option<DiagnosticCode>) -> Self {
118 Self { code, ..self }
119 }
120} 120}
121 121
122#[derive(Debug, Copy, Clone)] 122#[derive(Debug, Copy, Clone)]
@@ -161,35 +161,13 @@ pub(crate) fn diagnostics(
161 check_unnecessary_braces_in_use_statement(&mut res, file_id, &node); 161 check_unnecessary_braces_in_use_statement(&mut res, file_id, &node);
162 field_shorthand::check(&mut res, file_id, &node); 162 field_shorthand::check(&mut res, file_id, &node);
163 } 163 }
164 let res = RefCell::new(res);
165 let sink_builder = DiagnosticSinkBuilder::new()
166 // Only collect experimental diagnostics when they're enabled.
167 .filter(|diag| !(diag.is_experimental() && config.disable_experimental))
168 .filter(|diag| !config.disabled.contains(diag.code().as_str()));
169
170 // Finalize the `DiagnosticSink` building process.
171 let mut sink = sink_builder
172 // Diagnostics not handled above get no fix and default treatment.
173 .build(|d| {
174 res.borrow_mut().push(
175 Diagnostic::error(
176 sema.diagnostics_display_range(d.display_source()).range,
177 d.message(),
178 )
179 .with_code(Some(d.code())),
180 );
181 });
182 164
183 let mut diags = Vec::new(); 165 let mut diags = Vec::new();
184 let module = sema.to_module_def(file_id); 166 let module = sema.to_module_def(file_id);
185 if let Some(m) = module { 167 if let Some(m) = module {
186 diags = m.diagnostics(db, &mut sink) 168 m.diagnostics(db, &mut diags)
187 } 169 }
188 170
189 drop(sink);
190
191 let mut res = res.into_inner();
192
193 let ctx = DiagnosticsContext { config, sema, resolve }; 171 let ctx = DiagnosticsContext { config, sema, resolve };
194 if module.is_none() { 172 if module.is_none() {
195 let d = UnlinkedFile { file: file_id }; 173 let d = UnlinkedFile { file: file_id };
@@ -350,8 +328,8 @@ mod tests {
350 ) 328 )
351 .unwrap() 329 .unwrap()
352 .pop() 330 .pop()
353 .unwrap(); 331 .expect("no diagnostics");
354 let fix = &diagnostic.fixes.unwrap()[nth]; 332 let fix = &diagnostic.fixes.expect("diagnostic misses fixes")[nth];
355 let actual = { 333 let actual = {
356 let source_change = fix.source_change.as_ref().unwrap(); 334 let source_change = fix.source_change.as_ref().unwrap();
357 let file_id = *source_change.source_file_edits.keys().next().unwrap(); 335 let file_id = *source_change.source_file_edits.keys().next().unwrap();