Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 55 additions & 8 deletions crates/ide-assists/src/handlers/extract_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,11 +693,17 @@
parent
.statements()
.filter(|stmt| text_range.contains_range(stmt.syntax().text_range()))
.filter_map(|stmt| match stmt {
ast::Stmt::ExprStmt(expr_stmt) => expr_stmt.expr(),
ast::Stmt::Item(_) => None,
ast::Stmt::LetStmt(stmt) => stmt.initializer(),
.flat_map(|stmt| match stmt {
ast::Stmt::ExprStmt(expr_stmt) => vec![expr_stmt.expr()],
ast::Stmt::Item(_) => vec![None],
ast::Stmt::LetStmt(stmt) => vec![
stmt.initializer(),
stmt.let_else()
.and_then(|le| le.block_expr())
.map(|be| ast::Expr::BlockExpr(be)),

Check failure on line 703 in crates/ide-assists/src/handlers/extract_function.rs

View workflow job for this annotation

GitHub Actions / Rust (macos-latest)

redundant closure
],
})
.filter_map(std::convert::identity)

Check failure on line 706 in crates/ide-assists/src/handlers/extract_function.rs

View workflow job for this annotation

GitHub Actions / Rust (macos-latest)

use of `filter_map` with an identity function
.for_each(|expr| walk_expr(&expr, cb));
if let Some(expr) = parent
.tail_expr()
Expand All @@ -716,11 +722,17 @@
parent
.statements()
.filter(|stmt| text_range.contains_range(stmt.syntax().text_range()))
.filter_map(|stmt| match stmt {
ast::Stmt::ExprStmt(expr_stmt) => expr_stmt.expr(),
ast::Stmt::Item(_) => None,
ast::Stmt::LetStmt(stmt) => stmt.initializer(),
.flat_map(|stmt| match stmt {
ast::Stmt::ExprStmt(expr_stmt) => vec![expr_stmt.expr()],
ast::Stmt::Item(_) => vec![None],
ast::Stmt::LetStmt(stmt) => vec![
stmt.initializer(),
stmt.let_else()
.and_then(|le| le.block_expr())
.map(|be| ast::Expr::BlockExpr(be)),

Check failure on line 732 in crates/ide-assists/src/handlers/extract_function.rs

View workflow job for this annotation

GitHub Actions / Rust (macos-latest)

redundant closure
],
})
.filter_map(std::convert::identity)

Check failure on line 735 in crates/ide-assists/src/handlers/extract_function.rs

View workflow job for this annotation

GitHub Actions / Rust (macos-latest)

use of `filter_map` with an identity function
.for_each(|expr| preorder_expr(&expr, cb));
if let Some(expr) = parent
.tail_expr()
Expand Down Expand Up @@ -6233,4 +6245,39 @@
cov_mark::check!(extract_function_in_braces_is_not_applicable);
check_assist_not_applicable(extract_function, r"fn foo(arr: &mut $0[$0i32]) {}");
}

#[test]
fn no_parameter_for_variable_used_only_let_else() {
check_assist(
extract_function,
r#"
fn foo() -> u32 {
let x = 5;

$0let Some(y) = Some(1) else {
return x * 2;
};$0

y
}"#,
r#"
fn foo() -> u32 {
let x = 5;

let y = match fun_name(x) {
Ok(value) => value,
Err(value) => return value,
};

y
}

fn $0fun_name(x: u32) -> Result<_, u32> {
let Some(y) = Some(1) else {
return Err(x * 2);
};
Ok(y)
}"#,
);
}
}
Loading