diff options
Diffstat (limited to 'crates/ide')
-rw-r--r-- | crates/ide/src/diagnostics.rs | 59 |
1 files changed, 33 insertions, 26 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index fb2a2fb28..d3d8c86b5 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs | |||
@@ -201,35 +201,42 @@ fn check_expr_field_shorthand( | |||
201 | acc: &mut Vec<Diagnostic>, | 201 | acc: &mut Vec<Diagnostic>, |
202 | file_id: FileId, | 202 | file_id: FileId, |
203 | record_lit: ast::RecordExpr, | 203 | record_lit: ast::RecordExpr, |
204 | ) -> Option<()> { | 204 | ) { |
205 | let record_field_list = record_lit.record_expr_field_list()?; | 205 | let record_field_list = match record_lit.record_expr_field_list() { |
206 | Some(it) => it, | ||
207 | None => (), | ||
208 | }; | ||
206 | for record_field in record_field_list.fields() { | 209 | for record_field in record_field_list.fields() { |
207 | if let (Some(name_ref), Some(expr)) = (record_field.name_ref(), record_field.expr()) { | 210 | let (name_ref, expr) = match record_field.name_ref().zip(record_field.expr()) { |
208 | let field_name = name_ref.syntax().text().to_string(); | 211 | Some(it) => it, |
209 | let field_expr = expr.syntax().text().to_string(); | 212 | None => continue, |
210 | let field_name_is_tup_index = name_ref.as_tuple_field().is_some(); | 213 | }; |
211 | if field_name == field_expr && !field_name_is_tup_index { | 214 | |
212 | let mut edit_builder = TextEdit::builder(); | 215 | let field_name = name_ref.syntax().text().to_string(); |
213 | edit_builder.delete(record_field.syntax().text_range()); | 216 | let field_expr = expr.syntax().text().to_string(); |
214 | edit_builder.insert(record_field.syntax().text_range().start(), field_name); | 217 | let field_name_is_tup_index = name_ref.as_tuple_field().is_some(); |
215 | let edit = edit_builder.finish(); | 218 | if field_name != field_expr || field_name_is_tup_index { |
216 | 219 | continue; | |
217 | let field_range = record_field.syntax().text_range(); | ||
218 | acc.push(Diagnostic { | ||
219 | // name: None, | ||
220 | range: field_range, | ||
221 | message: "Shorthand struct initialization".to_string(), | ||
222 | severity: Severity::WeakWarning, | ||
223 | fix: Some(Fix::new( | ||
224 | "Use struct shorthand initialization", | ||
225 | SourceFileEdit { file_id, edit }.into(), | ||
226 | field_range, | ||
227 | )), | ||
228 | }); | ||
229 | } | ||
230 | } | 220 | } |
221 | |||
222 | let mut edit_builder = TextEdit::builder(); | ||
223 | edit_builder.delete(record_field.syntax().text_range()); | ||
224 | edit_builder.insert(record_field.syntax().text_range().start(), field_name); | ||
225 | let edit = edit_builder.finish(); | ||
226 | |||
227 | let field_range = record_field.syntax().text_range(); | ||
228 | acc.push(Diagnostic { | ||
229 | // name: None, | ||
230 | range: field_range, | ||
231 | message: "Shorthand struct initialization".to_string(), | ||
232 | severity: Severity::WeakWarning, | ||
233 | fix: Some(Fix::new( | ||
234 | "Use struct shorthand initialization", | ||
235 | SourceFileEdit { file_id, edit }.into(), | ||
236 | field_range, | ||
237 | )), | ||
238 | }); | ||
231 | } | 239 | } |
232 | Some(()) | ||
233 | } | 240 | } |
234 | 241 | ||
235 | #[cfg(test)] | 242 | #[cfg(test)] |