Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #22369 from A4-Tacks/iflet-with-match-happy-with-guard
fix: no use sad pattern on happy arm with guard
Chayim Refael Friedman 2 weeks ago
parent 2ba9e2e · parent 3b9c775 · commit b94da5d
-rw-r--r--crates/ide-assists/src/handlers/replace_if_let_with_match.rs52
1 files changed, 51 insertions, 1 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 aa3f917f12..ff2d0544b2 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
@@ -171,7 +171,7 @@ fn make_else_arm(
let (pattern, expr) = if let Some(else_expr) = else_expr {
let pattern = match conditionals {
[(None, Some(_), _)] => make.literal_pat("false").into(),
- [(Some(pat), _, _)] => match ctx
+ [(Some(pat), None, _)] => match ctx
.sema
.type_of_pat(pat)
.and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty.adjusted()))
@@ -956,6 +956,31 @@ fn foo(x: Option<i32>) {
}
#[test]
+ fn special_case_option_with_guard() {
+ check_assist(
+ replace_if_let_with_match,
+ r#"
+//- minicore: option
+fn foo(x: Option<i32>) {
+ $0if let Some(x) = x && x != 4 {
+ println!("{}", x)
+ } else {
+ println!("none")
+ }
+}
+"#,
+ r#"
+fn foo(x: Option<i32>) {
+ match x {
+ Some(x) if x != 4 => println!("{}", x),
+ _ => println!("none"),
+ }
+}
+"#,
+ );
+ }
+
+ #[test]
fn special_case_option_ref() {
check_assist(
replace_if_let_with_match,
@@ -1006,6 +1031,31 @@ fn foo(x: Option<i32>) {
}
#[test]
+ fn special_case_inverted_option_with_guard() {
+ check_assist(
+ replace_if_let_with_match,
+ r#"
+//- minicore: option
+fn foo(x: Option<i32>) {
+ $0if let None = x && other_cond {
+ println!("none")
+ } else {
+ println!("some")
+ }
+}
+"#,
+ r#"
+fn foo(x: Option<i32>) {
+ match x {
+ None if other_cond => println!("none"),
+ _ => println!("some"),
+ }
+}
+"#,
+ );
+ }
+
+ #[test]
fn special_case_result() {
check_assist(
replace_if_let_with_match,