Skip to content

Commit c243129

Browse files
committed
fix: wrap parentheses on guard for replace_if_let_with_match
Example --- ```rust fn main() { match$0 Some(0) { Some(n) if n % 2 == 0 || n == 7 => (), _ => (), } } ``` **Before this PR** ```rust fn main() { if let Some(n) = Some(0) && n % 2 == 0 || n == 7 { () } } ``` **After this PR** ```rust fn main() { if let Some(n) = Some(0) && (n % 2 == 0 || n == 7) { () } } ```
1 parent 671ba41 commit c243129

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

crates/ide-assists/src/handlers/replace_if_let_with_match.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use syntax::{
1313

1414
use crate::{
1515
AssistContext, AssistId, Assists,
16-
utils::{does_pat_match_variant, does_pat_variant_nested_or_literal, unwrap_trivial_block},
16+
utils::{
17+
does_pat_match_variant, does_pat_variant_nested_or_literal, unwrap_trivial_block,
18+
wrap_paren,
19+
},
1720
};
1821

1922
// Assist: replace_if_let_with_match
@@ -289,6 +292,7 @@ pub(crate) fn replace_match_with_if_let(acc: &mut Assists, ctx: &AssistContext<'
289292
_ => make.expr_let(if_let_pat, scrutinee).into(),
290293
};
291294
let condition = if let Some(guard) = guard {
295+
let guard = wrap_paren(guard, &make, ast::prec::ExprPrecedence::LAnd);
292296
make.expr_bin(condition, ast::BinaryOp::LogicOp(ast::LogicOp::And), guard).into()
293297
} else {
294298
condition
@@ -2268,14 +2272,35 @@ fn main() {
22682272
"#,
22692273
r#"
22702274
fn main() {
2271-
if let Some(n) = Some(0) && n % 2 == 0 && n != 6 {
2275+
if let Some(n) = Some(0) && (n % 2 == 0 && n != 6) {
22722276
()
22732277
} else {
22742278
code()
22752279
}
22762280
}
22772281
"#,
2278-
)
2282+
);
2283+
2284+
check_assist(
2285+
replace_match_with_if_let,
2286+
r#"
2287+
fn main() {
2288+
match$0 Some(0) {
2289+
Some(n) if n % 2 == 0 || n == 7 => (),
2290+
_ => code(),
2291+
}
2292+
}
2293+
"#,
2294+
r#"
2295+
fn main() {
2296+
if let Some(n) = Some(0) && (n % 2 == 0 || n == 7) {
2297+
()
2298+
} else {
2299+
code()
2300+
}
2301+
}
2302+
"#,
2303+
);
22792304
}
22802305

22812306
#[test]

0 commit comments

Comments
 (0)