Skip to content

Commit 4c9d2c7

Browse files
committed
Auto merge of rust-lang#15698 - rmehri01:15695_make_guarded_return_more_lenient, r=Veykril
fix: allow more kinds of if let patterns in guarded return assist Removes the checks that require the pattern to be a tuple struct with exactly 1 field that is unqualified and has an identifier pattern in it. I'm not sure if there should be more checks in place but they seem unnecessary now? Closes rust-lang#15695
2 parents d7faec8 + 146a7cc commit 4c9d2c7

File tree

1 file changed

+57
-19
lines changed

1 file changed

+57
-19
lines changed

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

+57-19
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext<'
5151
// Check if there is an IfLet that we can handle.
5252
let (if_let_pat, cond_expr) = if is_pattern_cond(cond.clone()) {
5353
let let_ = single_let(cond)?;
54-
match let_.pat() {
55-
Some(ast::Pat::TupleStructPat(pat)) if pat.fields().count() == 1 => {
56-
let path = pat.path()?;
57-
if path.qualifier().is_some() {
58-
return None;
59-
}
60-
61-
let bound_ident = pat.fields().next()?;
62-
if !ast::IdentPat::can_cast(bound_ident.syntax().kind()) {
63-
return None;
64-
}
65-
66-
(Some((path, bound_ident)), let_.expr()?)
67-
}
68-
_ => return None, // Unsupported IfLet.
69-
}
54+
(Some(let_.pat()?), let_.expr()?)
7055
} else {
7156
(None, cond)
7257
};
@@ -136,11 +121,10 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext<'
136121
};
137122
new_expr.syntax().clone_for_update()
138123
}
139-
Some((path, bound_ident)) => {
124+
Some(pat) => {
140125
// If-let.
141-
let pat = make::tuple_struct_pat(path, once(bound_ident));
142126
let let_else_stmt = make::let_else_stmt(
143-
pat.into(),
127+
pat,
144128
None,
145129
cond_expr,
146130
ast::make::tail_only_block_expr(early_expression),
@@ -442,6 +426,60 @@ fn main() {
442426
);
443427
}
444428

429+
#[test]
430+
fn convert_arbitrary_if_let_patterns() {
431+
check_assist(
432+
convert_to_guarded_return,
433+
r#"
434+
fn main() {
435+
$0if let None = Some(92) {
436+
foo();
437+
}
438+
}
439+
"#,
440+
r#"
441+
fn main() {
442+
let None = Some(92) else { return };
443+
foo();
444+
}
445+
"#,
446+
);
447+
448+
check_assist(
449+
convert_to_guarded_return,
450+
r#"
451+
fn main() {
452+
$0if let [1, x] = [1, 92] {
453+
foo(x);
454+
}
455+
}
456+
"#,
457+
r#"
458+
fn main() {
459+
let [1, x] = [1, 92] else { return };
460+
foo(x);
461+
}
462+
"#,
463+
);
464+
465+
check_assist(
466+
convert_to_guarded_return,
467+
r#"
468+
fn main() {
469+
$0if let (Some(x), None) = (Some(92), None) {
470+
foo(x);
471+
}
472+
}
473+
"#,
474+
r#"
475+
fn main() {
476+
let (Some(x), None) = (Some(92), None) else { return };
477+
foo(x);
478+
}
479+
"#,
480+
);
481+
}
482+
445483
#[test]
446484
fn ignore_already_converted_if() {
447485
check_assist_not_applicable(

0 commit comments

Comments
 (0)