diff options
author | Jonas Schievink <[email protected]> | 2020-10-20 16:48:43 +0100 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2020-10-20 16:53:04 +0100 |
commit | a54e481646edb151075d12ca6903091abe7cfc4e (patch) | |
tree | fbf8db1f82d94c52968a96819027883461c3e885 /crates/ide | |
parent | f925735e64cb6aed85f28cacc0a91c1c1bc06bb4 (diff) |
Simplify diagnostic construction, add unused field
Diffstat (limited to 'crates/ide')
-rw-r--r-- | crates/ide/src/diagnostics.rs | 73 | ||||
-rw-r--r-- | crates/ide/src/diagnostics/field_shorthand.rs | 32 |
2 files changed, 48 insertions, 57 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index 1e5ea4617..c92c1c066 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs | |||
@@ -31,6 +31,21 @@ pub struct Diagnostic { | |||
31 | pub range: TextRange, | 31 | pub range: TextRange, |
32 | pub severity: Severity, | 32 | pub severity: Severity, |
33 | pub fix: Option<Fix>, | 33 | pub fix: Option<Fix>, |
34 | pub unused: bool, | ||
35 | } | ||
36 | |||
37 | impl Diagnostic { | ||
38 | fn error(range: TextRange, message: String) -> Self { | ||
39 | Self { message, range, severity: Severity::Error, fix: None, unused: false } | ||
40 | } | ||
41 | |||
42 | fn hint(range: TextRange, message: String) -> Self { | ||
43 | Self { message, range, severity: Severity::WeakWarning, fix: None, unused: false } | ||
44 | } | ||
45 | |||
46 | fn with_fix(self, fix: Option<Fix>) -> Self { | ||
47 | Self { fix, ..self } | ||
48 | } | ||
34 | } | 49 | } |
35 | 50 | ||
36 | #[derive(Debug)] | 51 | #[derive(Debug)] |
@@ -71,13 +86,13 @@ pub(crate) fn diagnostics( | |||
71 | let mut res = Vec::new(); | 86 | let mut res = Vec::new(); |
72 | 87 | ||
73 | // [#34344] Only take first 128 errors to prevent slowing down editor/ide, the number 128 is chosen arbitrarily. | 88 | // [#34344] Only take first 128 errors to prevent slowing down editor/ide, the number 128 is chosen arbitrarily. |
74 | res.extend(parse.errors().iter().take(128).map(|err| Diagnostic { | 89 | res.extend( |
75 | // name: None, | 90 | parse |
76 | range: err.range(), | 91 | .errors() |
77 | message: format!("Syntax Error: {}", err), | 92 | .iter() |
78 | severity: Severity::Error, | 93 | .take(128) |
79 | fix: None, | 94 | .map(|err| Diagnostic::error(err.range(), format!("Syntax Error: {}", err))), |
80 | })); | 95 | ); |
81 | 96 | ||
82 | for node in parse.tree().syntax().descendants() { | 97 | for node in parse.tree().syntax().descendants() { |
83 | check_unnecessary_braces_in_use_statement(&mut res, file_id, &node); | 98 | check_unnecessary_braces_in_use_statement(&mut res, file_id, &node); |
@@ -108,13 +123,8 @@ pub(crate) fn diagnostics( | |||
108 | let mut sink = sink_builder | 123 | let mut sink = sink_builder |
109 | // Diagnostics not handled above get no fix and default treatment. | 124 | // Diagnostics not handled above get no fix and default treatment. |
110 | .build(|d| { | 125 | .build(|d| { |
111 | res.borrow_mut().push(Diagnostic { | 126 | res.borrow_mut() |
112 | // name: Some(d.name().into()), | 127 | .push(Diagnostic::error(sema.diagnostics_display_range(d).range, d.message())); |
113 | message: d.message(), | ||
114 | range: sema.diagnostics_display_range(d).range, | ||
115 | severity: Severity::Error, | ||
116 | fix: None, | ||
117 | }) | ||
118 | }); | 128 | }); |
119 | 129 | ||
120 | if let Some(m) = sema.to_module_def(file_id) { | 130 | if let Some(m) = sema.to_module_def(file_id) { |
@@ -125,22 +135,11 @@ pub(crate) fn diagnostics( | |||
125 | } | 135 | } |
126 | 136 | ||
127 | fn diagnostic_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic { | 137 | fn diagnostic_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic { |
128 | Diagnostic { | 138 | Diagnostic::error(sema.diagnostics_display_range(d).range, d.message()).with_fix(d.fix(&sema)) |
129 | // name: Some(d.name().into()), | ||
130 | range: sema.diagnostics_display_range(d).range, | ||
131 | message: d.message(), | ||
132 | severity: Severity::Error, | ||
133 | fix: d.fix(&sema), | ||
134 | } | ||
135 | } | 139 | } |
136 | 140 | ||
137 | fn warning_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic { | 141 | fn warning_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic { |
138 | Diagnostic { | 142 | Diagnostic::hint(sema.diagnostics_display_range(d).range, d.message()).with_fix(d.fix(&sema)) |
139 | range: sema.diagnostics_display_range(d).range, | ||
140 | message: d.message(), | ||
141 | severity: Severity::WeakWarning, | ||
142 | fix: d.fix(&sema), | ||
143 | } | ||
144 | } | 143 | } |
145 | 144 | ||
146 | fn check_unnecessary_braces_in_use_statement( | 145 | fn check_unnecessary_braces_in_use_statement( |
@@ -161,17 +160,14 @@ fn check_unnecessary_braces_in_use_statement( | |||
161 | edit_builder.finish() | 160 | edit_builder.finish() |
162 | }); | 161 | }); |
163 | 162 | ||
164 | acc.push(Diagnostic { | 163 | acc.push( |
165 | // name: None, | 164 | Diagnostic::hint(use_range, "Unnecessary braces in use statement".to_string()) |
166 | range: use_range, | 165 | .with_fix(Some(Fix::new( |
167 | message: "Unnecessary braces in use statement".to_string(), | 166 | "Remove unnecessary braces", |
168 | severity: Severity::WeakWarning, | 167 | SourceFileEdit { file_id, edit }.into(), |
169 | fix: Some(Fix::new( | 168 | use_range, |
170 | "Remove unnecessary braces", | 169 | ))), |
171 | SourceFileEdit { file_id, edit }.into(), | 170 | ); |
172 | use_range, | ||
173 | )), | ||
174 | }); | ||
175 | } | 171 | } |
176 | 172 | ||
177 | Some(()) | 173 | Some(()) |
@@ -578,6 +574,7 @@ fn test_fn() { | |||
578 | fix_trigger_range: 0..8, | 574 | fix_trigger_range: 0..8, |
579 | }, | 575 | }, |
580 | ), | 576 | ), |
577 | unused: false, | ||
581 | }, | 578 | }, |
582 | ] | 579 | ] |
583 | "#]], | 580 | "#]], |
diff --git a/crates/ide/src/diagnostics/field_shorthand.rs b/crates/ide/src/diagnostics/field_shorthand.rs index 2c4acd783..54e9fce9e 100644 --- a/crates/ide/src/diagnostics/field_shorthand.rs +++ b/crates/ide/src/diagnostics/field_shorthand.rs | |||
@@ -6,7 +6,7 @@ use ide_db::source_change::SourceFileEdit; | |||
6 | use syntax::{ast, match_ast, AstNode, SyntaxNode}; | 6 | use syntax::{ast, match_ast, AstNode, SyntaxNode}; |
7 | use text_edit::TextEdit; | 7 | use text_edit::TextEdit; |
8 | 8 | ||
9 | use crate::{Diagnostic, Fix, Severity}; | 9 | use crate::{Diagnostic, Fix}; |
10 | 10 | ||
11 | pub(super) fn check(acc: &mut Vec<Diagnostic>, file_id: FileId, node: &SyntaxNode) { | 11 | pub(super) fn check(acc: &mut Vec<Diagnostic>, file_id: FileId, node: &SyntaxNode) { |
12 | match_ast! { | 12 | match_ast! { |
@@ -46,17 +46,15 @@ fn check_expr_field_shorthand( | |||
46 | let edit = edit_builder.finish(); | 46 | let edit = edit_builder.finish(); |
47 | 47 | ||
48 | let field_range = record_field.syntax().text_range(); | 48 | let field_range = record_field.syntax().text_range(); |
49 | acc.push(Diagnostic { | 49 | acc.push( |
50 | // name: None, | 50 | Diagnostic::hint(field_range, "Shorthand struct initialization".to_string()).with_fix( |
51 | range: field_range, | 51 | Some(Fix::new( |
52 | message: "Shorthand struct initialization".to_string(), | 52 | "Use struct shorthand initialization", |
53 | severity: Severity::WeakWarning, | 53 | SourceFileEdit { file_id, edit }.into(), |
54 | fix: Some(Fix::new( | 54 | field_range, |
55 | "Use struct shorthand initialization", | 55 | )), |
56 | SourceFileEdit { file_id, edit }.into(), | 56 | ), |
57 | field_range, | 57 | ); |
58 | )), | ||
59 | }); | ||
60 | } | 58 | } |
61 | } | 59 | } |
62 | 60 | ||
@@ -88,17 +86,13 @@ fn check_pat_field_shorthand( | |||
88 | let edit = edit_builder.finish(); | 86 | let edit = edit_builder.finish(); |
89 | 87 | ||
90 | let field_range = record_pat_field.syntax().text_range(); | 88 | let field_range = record_pat_field.syntax().text_range(); |
91 | acc.push(Diagnostic { | 89 | acc.push(Diagnostic::hint(field_range, "Shorthand struct pattern".to_string()).with_fix( |
92 | // name: None, | 90 | Some(Fix::new( |
93 | range: field_range, | ||
94 | message: "Shorthand struct pattern".to_string(), | ||
95 | severity: Severity::WeakWarning, | ||
96 | fix: Some(Fix::new( | ||
97 | "Use struct field shorthand", | 91 | "Use struct field shorthand", |
98 | SourceFileEdit { file_id, edit }.into(), | 92 | SourceFileEdit { file_id, edit }.into(), |
99 | field_range, | 93 | field_range, |
100 | )), | 94 | )), |
101 | }); | 95 | )); |
102 | } | 96 | } |
103 | } | 97 | } |
104 | 98 | ||