diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-06-13 20:17:57 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-06-13 20:17:57 +0100 |
commit | f0618a8f06a464840079f30b3e25bcdcca3922a3 (patch) | |
tree | 5b57ca5243009f6a63cb6f3d37acb524b8cd2c80 /crates/ide | |
parent | 8c5c0ef7b910ffafc9c684cb7076149ab79f4bdd (diff) | |
parent | 500c909c76a5165e3e3c6ff2eacc009c90f08bbe (diff) |
Merge #9257
9257: internal: diagnostic code is mandatory r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ide')
-rw-r--r-- | crates/ide/src/diagnostics.rs | 84 | ||||
-rw-r--r-- | crates/ide/src/diagnostics/field_shorthand.rs | 23 | ||||
-rw-r--r-- | crates/ide/src/diagnostics/unresolved_module.rs | 12 |
3 files changed, 46 insertions, 73 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index c024e3e1e..815a633e5 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs | |||
@@ -51,28 +51,26 @@ impl DiagnosticCode { | |||
51 | 51 | ||
52 | #[derive(Debug)] | 52 | #[derive(Debug)] |
53 | pub struct Diagnostic { | 53 | pub struct Diagnostic { |
54 | // pub name: Option<String>, | 54 | pub code: DiagnosticCode, |
55 | pub message: String, | 55 | pub message: String, |
56 | pub range: TextRange, | 56 | pub range: TextRange, |
57 | pub severity: Severity, | 57 | pub severity: Severity, |
58 | pub fixes: Option<Vec<Assist>>, | ||
59 | pub unused: bool, | 58 | pub unused: bool, |
60 | pub code: Option<DiagnosticCode>, | ||
61 | pub experimental: bool, | 59 | pub experimental: bool, |
60 | pub fixes: Option<Vec<Assist>>, | ||
62 | } | 61 | } |
63 | 62 | ||
64 | impl Diagnostic { | 63 | impl Diagnostic { |
65 | fn new(code: &'static str, message: impl Into<String>, range: TextRange) -> Diagnostic { | 64 | fn new(code: &'static str, message: impl Into<String>, range: TextRange) -> Diagnostic { |
66 | let message = message.into(); | 65 | let message = message.into(); |
67 | let code = Some(DiagnosticCode(code)); | 66 | Diagnostic { |
68 | Self { | 67 | code: DiagnosticCode(code), |
69 | message, | 68 | message, |
70 | range, | 69 | range, |
71 | severity: Severity::Error, | 70 | severity: Severity::Error, |
72 | fixes: None, | ||
73 | unused: false, | 71 | unused: false, |
74 | code, | ||
75 | experimental: false, | 72 | experimental: false, |
73 | fixes: None, | ||
76 | } | 74 | } |
77 | } | 75 | } |
78 | 76 | ||
@@ -86,36 +84,14 @@ impl Diagnostic { | |||
86 | self | 84 | self |
87 | } | 85 | } |
88 | 86 | ||
89 | fn error(range: TextRange, message: String) -> Self { | 87 | fn with_fixes(mut self, fixes: Option<Vec<Assist>>) -> Diagnostic { |
90 | Self { | 88 | self.fixes = fixes; |
91 | message, | 89 | self |
92 | range, | ||
93 | severity: Severity::Error, | ||
94 | fixes: None, | ||
95 | unused: false, | ||
96 | code: None, | ||
97 | experimental: false, | ||
98 | } | ||
99 | } | ||
100 | |||
101 | fn hint(range: TextRange, message: String) -> Self { | ||
102 | Self { | ||
103 | message, | ||
104 | range, | ||
105 | severity: Severity::WeakWarning, | ||
106 | fixes: None, | ||
107 | unused: false, | ||
108 | code: None, | ||
109 | experimental: false, | ||
110 | } | ||
111 | } | ||
112 | |||
113 | fn with_fixes(self, fixes: Option<Vec<Assist>>) -> Self { | ||
114 | Self { fixes, ..self } | ||
115 | } | 90 | } |
116 | 91 | ||
117 | fn with_unused(self, unused: bool) -> Self { | 92 | fn with_unused(mut self, unused: bool) -> Diagnostic { |
118 | Self { unused, ..self } | 93 | self.unused = unused; |
94 | self | ||
119 | } | 95 | } |
120 | } | 96 | } |
121 | 97 | ||
@@ -150,11 +126,9 @@ pub(crate) fn diagnostics( | |||
150 | 126 | ||
151 | // [#34344] Only take first 128 errors to prevent slowing down editor/ide, the number 128 is chosen arbitrarily. | 127 | // [#34344] Only take first 128 errors to prevent slowing down editor/ide, the number 128 is chosen arbitrarily. |
152 | res.extend( | 128 | res.extend( |
153 | parse | 129 | parse.errors().iter().take(128).map(|err| { |
154 | .errors() | 130 | Diagnostic::new("syntax-error", format!("Syntax Error: {}", err), err.range()) |
155 | .iter() | 131 | }), |
156 | .take(128) | ||
157 | .map(|err| Diagnostic::error(err.range(), format!("Syntax Error: {}", err))), | ||
158 | ); | 132 | ); |
159 | 133 | ||
160 | for node in parse.tree().syntax().descendants() { | 134 | for node in parse.tree().syntax().descendants() { |
@@ -205,15 +179,8 @@ pub(crate) fn diagnostics( | |||
205 | } | 179 | } |
206 | 180 | ||
207 | res.retain(|d| { | 181 | res.retain(|d| { |
208 | if let Some(code) = d.code { | 182 | !ctx.config.disabled.contains(d.code.as_str()) |
209 | if ctx.config.disabled.contains(code.as_str()) { | 183 | && !(ctx.config.disable_experimental && d.experimental) |
210 | return false; | ||
211 | } | ||
212 | } | ||
213 | if ctx.config.disable_experimental && d.experimental { | ||
214 | return false; | ||
215 | } | ||
216 | true | ||
217 | }); | 184 | }); |
218 | 185 | ||
219 | res | 186 | res |
@@ -244,13 +211,18 @@ fn check_unnecessary_braces_in_use_statement( | |||
244 | }); | 211 | }); |
245 | 212 | ||
246 | acc.push( | 213 | acc.push( |
247 | Diagnostic::hint(use_range, "Unnecessary braces in use statement".to_string()) | 214 | Diagnostic::new( |
248 | .with_fixes(Some(vec![fix( | 215 | "unnecessary-braces", |
249 | "remove_braces", | 216 | "Unnecessary braces in use statement".to_string(), |
250 | "Remove unnecessary braces", | 217 | use_range, |
251 | SourceChange::from_text_edit(file_id, edit), | 218 | ) |
252 | use_range, | 219 | .severity(Severity::WeakWarning) |
253 | )])), | 220 | .with_fixes(Some(vec![fix( |
221 | "remove_braces", | ||
222 | "Remove unnecessary braces", | ||
223 | SourceChange::from_text_edit(file_id, edit), | ||
224 | use_range, | ||
225 | )])), | ||
254 | ); | 226 | ); |
255 | } | 227 | } |
256 | 228 | ||
diff --git a/crates/ide/src/diagnostics/field_shorthand.rs b/crates/ide/src/diagnostics/field_shorthand.rs index e885a398e..c7f4dab8e 100644 --- a/crates/ide/src/diagnostics/field_shorthand.rs +++ b/crates/ide/src/diagnostics/field_shorthand.rs | |||
@@ -5,7 +5,7 @@ use ide_db::{base_db::FileId, source_change::SourceChange}; | |||
5 | use syntax::{ast, match_ast, AstNode, SyntaxNode}; | 5 | use syntax::{ast, match_ast, AstNode, SyntaxNode}; |
6 | use text_edit::TextEdit; | 6 | use text_edit::TextEdit; |
7 | 7 | ||
8 | use crate::{diagnostics::fix, Diagnostic}; | 8 | use crate::{diagnostics::fix, Diagnostic, Severity}; |
9 | 9 | ||
10 | pub(super) fn check(acc: &mut Vec<Diagnostic>, file_id: FileId, node: &SyntaxNode) { | 10 | pub(super) fn check(acc: &mut Vec<Diagnostic>, file_id: FileId, node: &SyntaxNode) { |
11 | match_ast! { | 11 | match_ast! { |
@@ -46,7 +46,8 @@ fn check_expr_field_shorthand( | |||
46 | 46 | ||
47 | let field_range = record_field.syntax().text_range(); | 47 | let field_range = record_field.syntax().text_range(); |
48 | acc.push( | 48 | acc.push( |
49 | Diagnostic::hint(field_range, "Shorthand struct initialization".to_string()) | 49 | Diagnostic::new("use-field-shorthand", "Shorthand struct initialization", field_range) |
50 | .severity(Severity::WeakWarning) | ||
50 | .with_fixes(Some(vec![fix( | 51 | .with_fixes(Some(vec![fix( |
51 | "use_expr_field_shorthand", | 52 | "use_expr_field_shorthand", |
52 | "Use struct shorthand initialization", | 53 | "Use struct shorthand initialization", |
@@ -85,14 +86,16 @@ fn check_pat_field_shorthand( | |||
85 | let edit = edit_builder.finish(); | 86 | let edit = edit_builder.finish(); |
86 | 87 | ||
87 | let field_range = record_pat_field.syntax().text_range(); | 88 | let field_range = record_pat_field.syntax().text_range(); |
88 | acc.push(Diagnostic::hint(field_range, "Shorthand struct pattern".to_string()).with_fixes( | 89 | acc.push( |
89 | Some(vec![fix( | 90 | Diagnostic::new("use-field-shorthand", "Shorthand struct pattern", field_range) |
90 | "use_pat_field_shorthand", | 91 | .severity(Severity::WeakWarning) |
91 | "Use struct field shorthand", | 92 | .with_fixes(Some(vec![fix( |
92 | SourceChange::from_text_edit(file_id, edit), | 93 | "use_pat_field_shorthand", |
93 | field_range, | 94 | "Use struct field shorthand", |
94 | )]), | 95 | SourceChange::from_text_edit(file_id, edit), |
95 | )); | 96 | field_range, |
97 | )])), | ||
98 | ); | ||
96 | } | 99 | } |
97 | } | 100 | } |
98 | 101 | ||
diff --git a/crates/ide/src/diagnostics/unresolved_module.rs b/crates/ide/src/diagnostics/unresolved_module.rs index b1da8f0e1..977b46414 100644 --- a/crates/ide/src/diagnostics/unresolved_module.rs +++ b/crates/ide/src/diagnostics/unresolved_module.rs | |||
@@ -65,9 +65,14 @@ mod baz {} | |||
65 | expect![[r#" | 65 | expect![[r#" |
66 | [ | 66 | [ |
67 | Diagnostic { | 67 | Diagnostic { |
68 | code: DiagnosticCode( | ||
69 | "unresolved-module", | ||
70 | ), | ||
68 | message: "unresolved module", | 71 | message: "unresolved module", |
69 | range: 0..8, | 72 | range: 0..8, |
70 | severity: Error, | 73 | severity: Error, |
74 | unused: false, | ||
75 | experimental: false, | ||
71 | fixes: Some( | 76 | fixes: Some( |
72 | [ | 77 | [ |
73 | Assist { | 78 | Assist { |
@@ -98,13 +103,6 @@ mod baz {} | |||
98 | }, | 103 | }, |
99 | ], | 104 | ], |
100 | ), | 105 | ), |
101 | unused: false, | ||
102 | code: Some( | ||
103 | DiagnosticCode( | ||
104 | "unresolved-module", | ||
105 | ), | ||
106 | ), | ||
107 | experimental: false, | ||
108 | }, | 106 | }, |
109 | ] | 107 | ] |
110 | "#]], | 108 | "#]], |