Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/handlers/replace_if_let_with_match.rs')
-rw-r--r--crates/ide-assists/src/handlers/replace_if_let_with_match.rs73
1 files changed, 70 insertions, 3 deletions
diff --git a/crates/ide-assists/src/handlers/replace_if_let_with_match.rs b/crates/ide-assists/src/handlers/replace_if_let_with_match.rs
index 6959988db7..5225202177 100644
--- a/crates/ide-assists/src/handlers/replace_if_let_with_match.rs
+++ b/crates/ide-assists/src/handlers/replace_if_let_with_match.rs
@@ -17,7 +17,7 @@ use crate::{
AssistContext, AssistId, Assists,
utils::{
does_pat_match_variant, does_pat_variant_nested_or_literal, unwrap_trivial_block,
- wrap_paren,
+ wrap_paren_in_guard_chain,
},
};
@@ -303,7 +303,7 @@ pub(crate) fn replace_match_with_if_let(
_ => make.expr_let(if_let_pat, scrutinee).into(),
};
let condition = if let Some(guard) = guard {
- let guard = wrap_paren(guard, make, ast::prec::ExprPrecedence::LAnd);
+ let guard = wrap_paren_in_guard_chain(guard, make);
make.expr_bin(condition, ast::BinaryOp::LogicOp(ast::LogicOp::And), guard).into()
} else {
condition
@@ -2454,7 +2454,7 @@ fn main() {
}
#[test]
- fn test_replace_match_with_if_let_chain() {
+ fn test_replace_match_with_if_let_with_simple_guard() {
check_assist(
replace_match_with_if_let,
r#"
@@ -2499,6 +2499,73 @@ fn main() {
}
#[test]
+ fn test_replace_match_with_if_let_with_if_let_guard() {
+ check_assist(
+ replace_match_with_if_let,
+ r#"
+fn main() {
+ match$0 Some(0) {
+ Some(n) if let Some(m) = n.checked_add(1) => (),
+ _ => code(),
+ }
+}
+"#,
+ r#"
+fn main() {
+ if let Some(n) = Some(0) && let Some(m) = n.checked_add(1) {
+ ()
+ } else {
+ code()
+ }
+}
+"#,
+ );
+
+ check_assist(
+ replace_match_with_if_let,
+ r#"
+fn main() {
+ match$0 Some(0) {
+ Some(n) if let Some(m) = n.checked_add(1) && m > 5 => (),
+ _ => code(),
+ }
+}
+ "#,
+ r#"
+fn main() {
+ if let Some(n) = Some(0) && let Some(m) = n.checked_add(1) && m > 5 {
+ ()
+ } else {
+ code()
+ }
+}
+ "#,
+ );
+
+ // what if the `let` expr is not the first one in the guard?
+ check_assist(
+ replace_match_with_if_let,
+ r#"
+fn main() {
+ match$0 Some(0) {
+ Some(n) if n > 5 && let Some(m) = n.checked_add(1) => (),
+ _ => code(),
+ }
+}
+ "#,
+ r#"
+fn main() {
+ if let Some(n) = Some(0) && n > 5 && let Some(m) = n.checked_add(1) {
+ ()
+ } else {
+ code()
+ }
+}
+ "#,
+ );
+ }
+
+ #[test]
fn test_replace_match_with_if_let_not_applicable_pat2_is_ident_pat() {
check_assist_not_applicable(
replace_match_with_if_let,