From e9903a91cd2135d2102cfd8547576331bbc85857 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 15 Oct 2020 16:51:55 +0200 Subject: flatten --- crates/ide/src/diagnostics.rs | 59 ++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 26 deletions(-) (limited to 'crates') 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( acc: &mut Vec, file_id: FileId, record_lit: ast::RecordExpr, -) -> Option<()> { - let record_field_list = record_lit.record_expr_field_list()?; +) { + let record_field_list = match record_lit.record_expr_field_list() { + Some(it) => it, + None => (), + }; for record_field in record_field_list.fields() { - if let (Some(name_ref), Some(expr)) = (record_field.name_ref(), record_field.expr()) { - let field_name = name_ref.syntax().text().to_string(); - let field_expr = expr.syntax().text().to_string(); - let field_name_is_tup_index = name_ref.as_tuple_field().is_some(); - if field_name == field_expr && !field_name_is_tup_index { - let mut edit_builder = TextEdit::builder(); - edit_builder.delete(record_field.syntax().text_range()); - edit_builder.insert(record_field.syntax().text_range().start(), field_name); - let edit = edit_builder.finish(); - - let field_range = record_field.syntax().text_range(); - acc.push(Diagnostic { - // name: None, - range: field_range, - message: "Shorthand struct initialization".to_string(), - severity: Severity::WeakWarning, - fix: Some(Fix::new( - "Use struct shorthand initialization", - SourceFileEdit { file_id, edit }.into(), - field_range, - )), - }); - } + let (name_ref, expr) = match record_field.name_ref().zip(record_field.expr()) { + Some(it) => it, + None => continue, + }; + + let field_name = name_ref.syntax().text().to_string(); + let field_expr = expr.syntax().text().to_string(); + let field_name_is_tup_index = name_ref.as_tuple_field().is_some(); + if field_name != field_expr || field_name_is_tup_index { + continue; } + + let mut edit_builder = TextEdit::builder(); + edit_builder.delete(record_field.syntax().text_range()); + edit_builder.insert(record_field.syntax().text_range().start(), field_name); + let edit = edit_builder.finish(); + + let field_range = record_field.syntax().text_range(); + acc.push(Diagnostic { + // name: None, + range: field_range, + message: "Shorthand struct initialization".to_string(), + severity: Severity::WeakWarning, + fix: Some(Fix::new( + "Use struct shorthand initialization", + SourceFileEdit { file_id, edit }.into(), + field_range, + )), + }); } - Some(()) } #[cfg(test)] -- cgit v1.2.3