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.rs34
1 files changed, 29 insertions, 5 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 8ff30fce5b..ada2fd9b21 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
@@ -13,7 +13,10 @@ use syntax::{
use crate::{
AssistContext, AssistId, Assists,
- utils::{does_pat_match_variant, does_pat_variant_nested_or_literal, unwrap_trivial_block},
+ utils::{
+ does_pat_match_variant, does_pat_variant_nested_or_literal, unwrap_trivial_block,
+ wrap_paren,
+ },
};
// Assist: replace_if_let_with_match
@@ -289,6 +292,7 @@ pub(crate) fn replace_match_with_if_let(acc: &mut Assists, ctx: &AssistContext<'
_ => 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);
make.expr_bin(condition, ast::BinaryOp::LogicOp(ast::LogicOp::And), guard).into()
} else {
condition
@@ -398,8 +402,7 @@ fn let_and_guard(cond: &ast::Expr) -> (Option<ast::LetExpr>, Option<ast::Expr>)
} else if let ast::Expr::BinExpr(bin_expr) = cond
&& let Some(ast::Expr::LetExpr(let_expr)) = and_bin_expr_left(bin_expr).lhs()
{
- let new_expr = bin_expr.clone_subtree();
- let mut edit = SyntaxEditor::new(new_expr.syntax().clone());
+ let (mut edit, new_expr) = SyntaxEditor::with_ast_node(bin_expr);
let left_bin = and_bin_expr_left(&new_expr);
if let Some(rhs) = left_bin.rhs() {
@@ -2268,14 +2271,35 @@ fn main() {
"#,
r#"
fn main() {
- if let Some(n) = Some(0) && n % 2 == 0 && n != 6 {
+ if let Some(n) = Some(0) && (n % 2 == 0 && n != 6) {
()
} else {
code()
}
}
"#,
- )
+ );
+
+ check_assist(
+ replace_match_with_if_let,
+ r#"
+fn main() {
+ match$0 Some(0) {
+ Some(n) if n % 2 == 0 || n == 7 => (),
+ _ => code(),
+ }
+}
+"#,
+ r#"
+fn main() {
+ if let Some(n) = Some(0) && (n % 2 == 0 || n == 7) {
+ ()
+ } else {
+ code()
+ }
+}
+"#,
+ );
}
#[test]