aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVladyslav Katasonov <[email protected]>2021-02-10 17:05:03 +0000
committerVladyslav Katasonov <[email protected]>2021-02-13 19:04:52 +0000
commit4be260d693babea7d32fead0ca10762a64557aaf (patch)
tree180dced93a90abd43aa213fbe419601368f14dfc
parent9eb19d92dd8d3200f3530faefa7a4048f58d280d (diff)
allow try expr? with return None in extracted function
-rw-r--r--crates/assists/src/handlers/extract_function.rs46
1 files changed, 42 insertions, 4 deletions
diff --git a/crates/assists/src/handlers/extract_function.rs b/crates/assists/src/handlers/extract_function.rs
index 225a50d2d..74fa2013c 100644
--- a/crates/assists/src/handlers/extract_function.rs
+++ b/crates/assists/src/handlers/extract_function.rs
@@ -229,12 +229,12 @@ fn external_control_flow(ctx: &AssistContext, body: &FunctionBody) -> Option<Con
229 229
230/// Checks is expr is `Err(_)` or `None` 230/// Checks is expr is `Err(_)` or `None`
231fn expr_err_kind(expr: &ast::Expr, ctx: &AssistContext) -> Option<TryKind> { 231fn expr_err_kind(expr: &ast::Expr, ctx: &AssistContext) -> Option<TryKind> {
232 let call_expr = match expr { 232 let func_name = match expr {
233 ast::Expr::CallExpr(call_expr) => call_expr, 233 ast::Expr::CallExpr(call_expr) => call_expr.expr()?,
234 ast::Expr::PathExpr(_) => expr.clone(),
234 _ => return None, 235 _ => return None,
235 }; 236 };
236 let func = call_expr.expr()?; 237 let text = func_name.syntax().text();
237 let text = func.syntax().text();
238 238
239 if text == "Err" { 239 if text == "Err" {
240 Some(TryKind::Result { ty: ctx.sema.type_of_expr(expr)? }) 240 Some(TryKind::Result { ty: ctx.sema.type_of_expr(expr)? })
@@ -3277,6 +3277,44 @@ fn $0fun_name() -> Result<i32, i64> {
3277 } 3277 }
3278 3278
3279 #[test] 3279 #[test]
3280 fn try_option_with_return() {
3281 check_assist(
3282 extract_function,
3283 r##"
3284enum Option<T> { None, Some(T) }
3285use Option::*;
3286fn foo() -> Option<()> {
3287 let n = 1;
3288 $0let k = foo()?;
3289 if k == 42 {
3290 return None;
3291 }
3292 let m = k + 1;$0
3293 let h = 1 + m;
3294 Some(())
3295}"##,
3296 r##"
3297enum Option<T> { None, Some(T) }
3298use Option::*;
3299fn foo() -> Option<()> {
3300 let n = 1;
3301 let m = fun_name()?;
3302 let h = 1 + m;
3303 Some(())
3304}
3305
3306fn $0fun_name() -> Option<i32> {
3307 let k = foo()?;
3308 if k == 42 {
3309 return None;
3310 }
3311 let m = k + 1;
3312 Some(m)
3313}"##,
3314 );
3315 }
3316
3317 #[test]
3280 fn try_result_with_return() { 3318 fn try_result_with_return() {
3281 check_assist( 3319 check_assist(
3282 extract_function, 3320 extract_function,