diff options
author | Phil Ellison <[email protected]> | 2020-12-30 15:46:05 +0000 |
---|---|---|
committer | Phil Ellison <[email protected]> | 2021-01-23 07:40:25 +0000 |
commit | 1ff860b93c972e0f8d3a8ee582c255fa59e9b284 (patch) | |
tree | 0533465ecb7329b6185c9259fb0f9c2b5ab0f7b0 /crates/hir_ty/src/diagnostics.rs | |
parent | 1316422a7c2ef26e9da78fa23f170407b1cb39bb (diff) |
Implement fix, add tests
Diffstat (limited to 'crates/hir_ty/src/diagnostics.rs')
-rw-r--r-- | crates/hir_ty/src/diagnostics.rs | 56 |
1 files changed, 48 insertions, 8 deletions
diff --git a/crates/hir_ty/src/diagnostics.rs b/crates/hir_ty/src/diagnostics.rs index b4cf81c10..3d7663f6a 100644 --- a/crates/hir_ty/src/diagnostics.rs +++ b/crates/hir_ty/src/diagnostics.rs | |||
@@ -392,7 +392,7 @@ impl Diagnostic for IncorrectCase { | |||
392 | #[derive(Debug)] | 392 | #[derive(Debug)] |
393 | pub struct ReplaceFilterMapNextWithFindMap { | 393 | pub struct ReplaceFilterMapNextWithFindMap { |
394 | pub file: HirFileId, | 394 | pub file: HirFileId, |
395 | pub filter_map_expr: AstPtr<ast::Expr>, | 395 | /// This expression is the whole method chain up to and including `.filter_map(..).next()`. |
396 | pub next_expr: AstPtr<ast::Expr>, | 396 | pub next_expr: AstPtr<ast::Expr>, |
397 | } | 397 | } |
398 | 398 | ||
@@ -404,7 +404,7 @@ impl Diagnostic for ReplaceFilterMapNextWithFindMap { | |||
404 | "replace filter_map(..).next() with find_map(..)".to_string() | 404 | "replace filter_map(..).next() with find_map(..)".to_string() |
405 | } | 405 | } |
406 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | 406 | fn display_source(&self) -> InFile<SyntaxNodePtr> { |
407 | InFile { file_id: self.file, value: self.filter_map_expr.clone().into() } | 407 | InFile { file_id: self.file, value: self.next_expr.clone().into() } |
408 | } | 408 | } |
409 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | 409 | fn as_any(&self) -> &(dyn Any + Send + 'static) { |
410 | self | 410 | self |
@@ -671,15 +671,55 @@ fn foo() { break; } | |||
671 | } | 671 | } |
672 | 672 | ||
673 | #[test] | 673 | #[test] |
674 | fn replace_missing_filter_next_with_find_map() { | 674 | fn replace_filter_next_with_find_map() { |
675 | check_diagnostics( | 675 | check_diagnostics( |
676 | r#" | 676 | r#" |
677 | fn foo() { | 677 | fn foo() { |
678 | let m = [1, 2, 3] | 678 | let m = [1, 2, 3].iter().filter_map(|x| if *x == 2 { Some (4) } else { None }).next(); |
679 | .iter() | 679 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ replace filter_map(..).next() with find_map(..) |
680 | .filter_map(|x| if *x == 2 { Some (4) } else { None }) | 680 | } |
681 | .next(); | 681 | "#, |
682 | //^^^ Replace .filter_map(..).next() with .find_map(..) | 682 | ); |
683 | } | ||
684 | |||
685 | #[test] | ||
686 | fn replace_filter_next_with_find_map_no_diagnostic_without_next() { | ||
687 | check_diagnostics( | ||
688 | r#" | ||
689 | fn foo() { | ||
690 | let m = [1, 2, 3] | ||
691 | .iter() | ||
692 | .filter_map(|x| if *x == 2 { Some (4) } else { None }) | ||
693 | .len(); | ||
694 | } | ||
695 | "#, | ||
696 | ); | ||
697 | } | ||
698 | |||
699 | #[test] | ||
700 | fn replace_filter_next_with_find_map_no_diagnostic_with_intervening_methods() { | ||
701 | check_diagnostics( | ||
702 | r#" | ||
703 | fn foo() { | ||
704 | let m = [1, 2, 3] | ||
705 | .iter() | ||
706 | .filter_map(|x| if *x == 2 { Some (4) } else { None }) | ||
707 | .map(|x| x + 2) | ||
708 | .len(); | ||
709 | } | ||
710 | "#, | ||
711 | ); | ||
712 | } | ||
713 | |||
714 | #[test] | ||
715 | fn replace_filter_next_with_find_map_no_diagnostic_if_not_in_chain() { | ||
716 | check_diagnostics( | ||
717 | r#" | ||
718 | fn foo() { | ||
719 | let m = [1, 2, 3] | ||
720 | .iter() | ||
721 | .filter_map(|x| if *x == 2 { Some (4) } else { None }); | ||
722 | let n = m.next(); | ||
683 | } | 723 | } |
684 | "#, | 724 | "#, |
685 | ); | 725 | ); |