Skip to content

Commit

Permalink
Auto merge of rust-lang#16405 - dfireBird:guarded-return-for-loop, r=…
Browse files Browse the repository at this point in the history
…lnicola

fix: Include `for` construct in convert to guarded return conditions
  • Loading branch information
bors committed Jan 20, 2024
2 parents 0c76421 + 721e790 commit d4926c1
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions crates/ide-assists/src/handlers/convert_to_guarded_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use syntax::{
make,
},
ted, AstNode,
SyntaxKind::{FN, LOOP_EXPR, WHILE_EXPR, WHITESPACE},
SyntaxKind::{FN, FOR_EXPR, LOOP_EXPR, WHILE_EXPR, WHITESPACE},
T,
};

Expand Down Expand Up @@ -82,7 +82,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext<'
let parent_container = parent_block.syntax().parent()?;

let early_expression: ast::Expr = match parent_container.kind() {
WHILE_EXPR | LOOP_EXPR => make::expr_continue(None),
WHILE_EXPR | LOOP_EXPR | FOR_EXPR => make::expr_continue(None),
FN => make::expr_return(None),
_ => return None,
};
Expand Down Expand Up @@ -424,6 +424,32 @@ fn main() {
);
}

#[test]
fn convert_let_inside_for() {
check_assist(
convert_to_guarded_return,
r#"
fn main() {
for n in ns {
if$0 let Some(n) = n {
foo(n);
bar();
}
}
}
"#,
r#"
fn main() {
for n in ns {
let Some(n) = n else { continue };
foo(n);
bar();
}
}
"#,
);
}

#[test]
fn convert_arbitrary_if_let_patterns() {
check_assist(
Expand Down

0 comments on commit d4926c1

Please sign in to comment.