diff options
author | Phil Ellison <[email protected]> | 2020-12-28 13:41:15 +0000 |
---|---|---|
committer | Phil Ellison <[email protected]> | 2021-01-23 07:40:24 +0000 |
commit | 1316422a7c2ef26e9da78fa23f170407b1cb39bb (patch) | |
tree | 1eea156207bcc920f0573ef51d7f6ad4fe03299d /crates/ide | |
parent | eab5db20edd9604ba5d489fa8c6430eb7bac6610 (diff) |
Add diagnostic for filter_map followed by next
Diffstat (limited to 'crates/ide')
-rw-r--r-- | crates/ide/src/diagnostics.rs | 3 | ||||
-rw-r--r-- | crates/ide/src/diagnostics/fixes.rs | 33 |
2 files changed, 35 insertions, 1 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index b35bc2bae..8607139ba 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs | |||
@@ -136,6 +136,9 @@ pub(crate) fn diagnostics( | |||
136 | .on::<hir::diagnostics::IncorrectCase, _>(|d| { | 136 | .on::<hir::diagnostics::IncorrectCase, _>(|d| { |
137 | res.borrow_mut().push(warning_with_fix(d, &sema)); | 137 | res.borrow_mut().push(warning_with_fix(d, &sema)); |
138 | }) | 138 | }) |
139 | .on::<hir::diagnostics::ReplaceFilterMapNextWithFindMap, _>(|d| { | ||
140 | res.borrow_mut().push(warning_with_fix(d, &sema)); | ||
141 | }) | ||
139 | .on::<hir::diagnostics::InactiveCode, _>(|d| { | 142 | .on::<hir::diagnostics::InactiveCode, _>(|d| { |
140 | // If there's inactive code somewhere in a macro, don't propagate to the call-site. | 143 | // If there's inactive code somewhere in a macro, don't propagate to the call-site. |
141 | if d.display_source().file_id.expansion_info(db).is_some() { | 144 | if d.display_source().file_id.expansion_info(db).is_some() { |
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::{ | |||
4 | db::AstDatabase, | 4 | db::AstDatabase, |
5 | diagnostics::{ | 5 | diagnostics::{ |
6 | Diagnostic, IncorrectCase, MissingFields, MissingOkOrSomeInTailExpr, NoSuchField, | 6 | Diagnostic, IncorrectCase, MissingFields, MissingOkOrSomeInTailExpr, NoSuchField, |
7 | RemoveThisSemicolon, UnresolvedModule, | 7 | RemoveThisSemicolon, ReplaceFilterMapNextWithFindMap, UnresolvedModule, |
8 | }, | 8 | }, |
9 | HasSource, HirDisplay, InFile, Semantics, VariantDef, | 9 | HasSource, HirDisplay, InFile, Semantics, VariantDef, |
10 | }; | 10 | }; |
@@ -144,6 +144,37 @@ impl DiagnosticWithFix for IncorrectCase { | |||
144 | } | 144 | } |
145 | } | 145 | } |
146 | 146 | ||
147 | // Bugs: | ||
148 | // * Action is applicable for both iter() and filter_map() rows | ||
149 | // * Action deletes the entire method chain | ||
150 | impl DiagnosticWithFix for ReplaceFilterMapNextWithFindMap { | ||
151 | fn fix(&self, sema: &Semantics<RootDatabase>) -> Option<Fix> { | ||
152 | let root = sema.db.parse_or_expand(self.file)?; | ||
153 | |||
154 | let next_expr = self.next_expr.to_node(&root); | ||
155 | let next_expr_range = next_expr.syntax().text_range(); | ||
156 | |||
157 | let filter_map_expr = self.filter_map_expr.to_node(&root); | ||
158 | let filter_map_expr_range = filter_map_expr.syntax().text_range(); | ||
159 | |||
160 | let edit = TextEdit::delete(next_expr_range); | ||
161 | |||
162 | // This is the entire method chain, including the array literal | ||
163 | eprintln!("NEXT EXPR: {:#?}", next_expr); | ||
164 | // This is the entire method chain except for the final next() | ||
165 | eprintln!("FILTER MAP EXPR: {:#?}", filter_map_expr); | ||
166 | |||
167 | let source_change = | ||
168 | SourceFileEdit { file_id: self.file.original_file(sema.db), edit }.into(); | ||
169 | |||
170 | Some(Fix::new( | ||
171 | "Replace filter_map(..).next() with find_map()", | ||
172 | source_change, | ||
173 | filter_map_expr_range, | ||
174 | )) | ||
175 | } | ||
176 | } | ||
177 | |||
147 | fn missing_record_expr_field_fix( | 178 | fn missing_record_expr_field_fix( |
148 | sema: &Semantics<RootDatabase>, | 179 | sema: &Semantics<RootDatabase>, |
149 | usage_file_id: FileId, | 180 | usage_file_id: FileId, |