diff options
Diffstat (limited to 'crates/ide/src/diagnostics.rs')
-rw-r--r-- | crates/ide/src/diagnostics.rs | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index 1c7f02763..3df73ed4f 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs | |||
@@ -10,7 +10,7 @@ mod field_shorthand; | |||
10 | use std::cell::RefCell; | 10 | use std::cell::RefCell; |
11 | 11 | ||
12 | use hir::{ | 12 | use hir::{ |
13 | diagnostics::{Diagnostic as _, DiagnosticSinkBuilder}, | 13 | diagnostics::{Diagnostic as _, DiagnosticCode, DiagnosticSinkBuilder}, |
14 | Semantics, | 14 | Semantics, |
15 | }; | 15 | }; |
16 | use ide_db::base_db::SourceDatabase; | 16 | use ide_db::base_db::SourceDatabase; |
@@ -35,15 +35,23 @@ pub struct Diagnostic { | |||
35 | pub severity: Severity, | 35 | pub severity: Severity, |
36 | pub fix: Option<Fix>, | 36 | pub fix: Option<Fix>, |
37 | pub unused: bool, | 37 | pub unused: bool, |
38 | pub code: Option<DiagnosticCode>, | ||
38 | } | 39 | } |
39 | 40 | ||
40 | impl Diagnostic { | 41 | impl Diagnostic { |
41 | fn error(range: TextRange, message: String) -> Self { | 42 | fn error(range: TextRange, message: String) -> Self { |
42 | Self { message, range, severity: Severity::Error, fix: None, unused: false } | 43 | Self { message, range, severity: Severity::Error, fix: None, unused: false, code: None } |
43 | } | 44 | } |
44 | 45 | ||
45 | fn hint(range: TextRange, message: String) -> Self { | 46 | fn hint(range: TextRange, message: String) -> Self { |
46 | Self { message, range, severity: Severity::WeakWarning, fix: None, unused: false } | 47 | Self { |
48 | message, | ||
49 | range, | ||
50 | severity: Severity::WeakWarning, | ||
51 | fix: None, | ||
52 | unused: false, | ||
53 | code: None, | ||
54 | } | ||
47 | } | 55 | } |
48 | 56 | ||
49 | fn with_fix(self, fix: Option<Fix>) -> Self { | 57 | fn with_fix(self, fix: Option<Fix>) -> Self { |
@@ -53,6 +61,10 @@ impl Diagnostic { | |||
53 | fn with_unused(self, unused: bool) -> Self { | 61 | fn with_unused(self, unused: bool) -> Self { |
54 | Self { unused, ..self } | 62 | Self { unused, ..self } |
55 | } | 63 | } |
64 | |||
65 | fn with_code(self, code: Option<DiagnosticCode>) -> Self { | ||
66 | Self { code, ..self } | ||
67 | } | ||
56 | } | 68 | } |
57 | 69 | ||
58 | #[derive(Debug)] | 70 | #[derive(Debug)] |
@@ -126,7 +138,8 @@ pub(crate) fn diagnostics( | |||
126 | // Override severity and mark as unused. | 138 | // Override severity and mark as unused. |
127 | res.borrow_mut().push( | 139 | res.borrow_mut().push( |
128 | Diagnostic::hint(sema.diagnostics_display_range(d).range, d.message()) | 140 | Diagnostic::hint(sema.diagnostics_display_range(d).range, d.message()) |
129 | .with_unused(true), | 141 | .with_unused(true) |
142 | .with_code(Some(d.code())), | ||
130 | ); | 143 | ); |
131 | }) | 144 | }) |
132 | // Only collect experimental diagnostics when they're enabled. | 145 | // Only collect experimental diagnostics when they're enabled. |
@@ -137,8 +150,10 @@ pub(crate) fn diagnostics( | |||
137 | let mut sink = sink_builder | 150 | let mut sink = sink_builder |
138 | // Diagnostics not handled above get no fix and default treatment. | 151 | // Diagnostics not handled above get no fix and default treatment. |
139 | .build(|d| { | 152 | .build(|d| { |
140 | res.borrow_mut() | 153 | res.borrow_mut().push( |
141 | .push(Diagnostic::error(sema.diagnostics_display_range(d).range, d.message())); | 154 | Diagnostic::error(sema.diagnostics_display_range(d).range, d.message()) |
155 | .with_code(Some(d.code())), | ||
156 | ); | ||
142 | }); | 157 | }); |
143 | 158 | ||
144 | if let Some(m) = sema.to_module_def(file_id) { | 159 | if let Some(m) = sema.to_module_def(file_id) { |
@@ -149,11 +164,15 @@ pub(crate) fn diagnostics( | |||
149 | } | 164 | } |
150 | 165 | ||
151 | fn diagnostic_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic { | 166 | fn diagnostic_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic { |
152 | Diagnostic::error(sema.diagnostics_display_range(d).range, d.message()).with_fix(d.fix(&sema)) | 167 | Diagnostic::error(sema.diagnostics_display_range(d).range, d.message()) |
168 | .with_fix(d.fix(&sema)) | ||
169 | .with_code(Some(d.code())) | ||
153 | } | 170 | } |
154 | 171 | ||
155 | fn warning_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic { | 172 | fn warning_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic { |
156 | Diagnostic::hint(sema.diagnostics_display_range(d).range, d.message()).with_fix(d.fix(&sema)) | 173 | Diagnostic::hint(sema.diagnostics_display_range(d).range, d.message()) |
174 | .with_fix(d.fix(&sema)) | ||
175 | .with_code(Some(d.code())) | ||
157 | } | 176 | } |
158 | 177 | ||
159 | fn check_unnecessary_braces_in_use_statement( | 178 | fn check_unnecessary_braces_in_use_statement( |
@@ -589,6 +608,11 @@ fn test_fn() { | |||
589 | }, | 608 | }, |
590 | ), | 609 | ), |
591 | unused: false, | 610 | unused: false, |
611 | code: Some( | ||
612 | DiagnosticCode( | ||
613 | "unresolved-module", | ||
614 | ), | ||
615 | ), | ||
592 | }, | 616 | }, |
593 | ] | 617 | ] |
594 | "#]], | 618 | "#]], |