From 1316422a7c2ef26e9da78fa23f170407b1cb39bb Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Mon, 28 Dec 2020 13:41:15 +0000 Subject: Add diagnostic for filter_map followed by next --- crates/ide/src/diagnostics/fixes.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'crates/ide/src/diagnostics') diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index 579d5a308..eafbac43a 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs @@ -4,7 +4,7 @@ use hir::{ db::AstDatabase, diagnostics::{ Diagnostic, IncorrectCase, MissingFields, MissingOkOrSomeInTailExpr, NoSuchField, - RemoveThisSemicolon, UnresolvedModule, + RemoveThisSemicolon, ReplaceFilterMapNextWithFindMap, UnresolvedModule, }, HasSource, HirDisplay, InFile, Semantics, VariantDef, }; @@ -144,6 +144,37 @@ impl DiagnosticWithFix for IncorrectCase { } } +// Bugs: +// * Action is applicable for both iter() and filter_map() rows +// * Action deletes the entire method chain +impl DiagnosticWithFix for ReplaceFilterMapNextWithFindMap { + fn fix(&self, sema: &Semantics) -> Option { + let root = sema.db.parse_or_expand(self.file)?; + + let next_expr = self.next_expr.to_node(&root); + let next_expr_range = next_expr.syntax().text_range(); + + let filter_map_expr = self.filter_map_expr.to_node(&root); + let filter_map_expr_range = filter_map_expr.syntax().text_range(); + + let edit = TextEdit::delete(next_expr_range); + + // This is the entire method chain, including the array literal + eprintln!("NEXT EXPR: {:#?}", next_expr); + // This is the entire method chain except for the final next() + eprintln!("FILTER MAP EXPR: {:#?}", filter_map_expr); + + let source_change = + SourceFileEdit { file_id: self.file.original_file(sema.db), edit }.into(); + + Some(Fix::new( + "Replace filter_map(..).next() with find_map()", + source_change, + filter_map_expr_range, + )) + } +} + fn missing_record_expr_field_fix( sema: &Semantics, usage_file_id: FileId, -- cgit v1.2.3