Skip to content
Open
Show file tree
Hide file tree
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
112 changes: 71 additions & 41 deletions crates/ide-assists/src/handlers/convert_to_guarded_return.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::iter::once;

use either::Either;
use hir::{Semantics, TypeInfo};
use ide_db::{RootDatabase, ty_filter::TryEnum};
Expand All @@ -17,7 +15,7 @@ use syntax::{
use crate::{
AssistId,
assist_context::{AssistContext, Assists},
utils::invert_boolean_expression_legacy,
utils::{invert_boolean_expression_legacy, is_never_block},
};

// Assist: convert_to_guarded_return
Expand All @@ -35,9 +33,7 @@ use crate::{
// ->
// ```
// fn main() {
// if !cond {
// return;
// }
// if !cond { return }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall but I think we'd prefer the former return; stmt over { return } as rustfmt does so

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will make it more compact in let-chain

// foo();
// bar();
// }
Expand All @@ -54,9 +50,13 @@ fn if_expr_to_guarded_return(
acc: &mut Assists,
ctx: &AssistContext<'_>,
) -> Option<()> {
if if_expr.else_branch().is_some() {
return None;
}
let else_block = match if_expr.else_branch() {
Some(ast::ElseBranch::Block(block_expr)) if is_never_block(&ctx.sema, &block_expr) => {
Some(block_expr)
}
Some(_) => return None,
_ => None,
};

let cond = if_expr.condition()?;

Expand Down Expand Up @@ -96,7 +96,11 @@ fn if_expr_to_guarded_return(

let parent_container = parent_block.syntax().parent()?;

let early_expression: ast::Expr = early_expression(parent_container, &ctx.sema)?;
let early_expression = else_block
.or_else(|| {
early_expression(parent_container, &ctx.sema).map(ast::make::tail_only_block_expr)
})?
.reset_indent();

then_block.syntax().first_child_or_token().map(|t| t.kind() == T!['{'])?;

Expand All @@ -123,21 +127,14 @@ fn if_expr_to_guarded_return(
&& let (Some(pat), Some(expr)) = (let_expr.pat(), let_expr.expr())
{
// If-let.
let let_else_stmt = make::let_else_stmt(
pat,
None,
expr,
ast::make::tail_only_block_expr(early_expression.clone()),
);
let let_else_stmt =
make::let_else_stmt(pat, None, expr, early_expression.clone());
let let_else_stmt = let_else_stmt.indent(if_indent_level);
let_else_stmt.syntax().clone()
} else {
// If.
let new_expr = {
let then_branch = make::block_expr(
once(make::expr_stmt(early_expression.clone()).into()),
None,
);
let then_branch = early_expression.clone();
let cond = invert_boolean_expression_legacy(expr);
make::expr_if(cond, then_branch, None).indent(if_indent_level)
};
Expand Down Expand Up @@ -296,9 +293,7 @@ fn main() {
r#"
fn main() {
bar();
if false {
return;
}
if false { return }
foo();

// comment
Expand Down Expand Up @@ -327,9 +322,7 @@ fn ret_option() -> Option<()> {
r#"
fn ret_option() -> Option<()> {
bar();
if false {
return None;
}
if false { return None }
foo();

// comment
Expand Down Expand Up @@ -360,9 +353,7 @@ fn main() {
fn main() {
let _f = || {
bar();
if false {
return;
}
if false { return }
foo();

// comment
Expand Down Expand Up @@ -421,6 +412,53 @@ fn main() {
);
}

#[test]
fn convert_if_let_has_never_type_else_block() {
check_assist(
convert_to_guarded_return,
r#"
fn main() {
if$0 let Ok(x) = Err(92) {
foo(x);
} else {
// needless comment
return;
}
}
"#,
r#"
fn main() {
let Ok(x) = Err(92) else {
// needless comment
return;
};
foo(x);
}
"#,
);

check_assist(
convert_to_guarded_return,
r#"
fn main() {
if$0 let Ok(x) = Err(92) {
foo(x);
} else {
return
}
}
"#,
r#"
fn main() {
let Ok(x) = Err(92) else {
return
};
foo(x);
}
"#,
);
}

#[test]
fn convert_if_let_result_inside_let() {
check_assist(
Expand Down Expand Up @@ -462,9 +500,7 @@ fn main() {
r#"
fn main() {
let Ok(x) = Err(92) else { return };
if x >= 30 {
return;
}
if x >= 30 { return }
let Some(y) = Some(8) else { return };
foo(x, y);
}
Expand All @@ -487,9 +523,7 @@ fn main() {
r#"
fn main() {
let Ok(x) = Err(92) else { return };
if !(x < 30 && y < 20) {
return;
}
if !(x < 30 && y < 20) { return }
let Some(y) = Some(8) else { return };
foo(x, y);
}
Expand Down Expand Up @@ -598,9 +632,7 @@ fn main() {
r#"
fn main() {
while true {
if false {
continue;
}
if false { continue }
foo();
bar();
}
Expand Down Expand Up @@ -652,9 +684,7 @@ fn main() {
r#"
fn main() {
loop {
if false {
continue;
}
if false { continue }
foo();
bar();
}
Expand Down
4 changes: 1 addition & 3 deletions crates/ide-assists/src/tests/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,9 +726,7 @@ fn main() {
"#####,
r#####"
fn main() {
if !cond {
return;
}
if !cond { return }
foo();
bar();
}
Expand Down
16 changes: 16 additions & 0 deletions crates/ide-assists/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,3 +1150,19 @@ pub fn is_body_const(sema: &Semantics<'_, RootDatabase>, expr: &ast::Expr) -> bo
});
is_const
}

// FIXME: #20460 When hir-ty can analyze the `never` statement at the end of block, remove it
pub(crate) fn is_never_block(
sema: &Semantics<'_, RootDatabase>,
block_expr: &ast::BlockExpr,
) -> bool {
if let Some(tail_expr) = block_expr.tail_expr() {
sema.type_of_expr(&tail_expr).is_some_and(|ty| ty.original.is_never())
} else if let Some(ast::Stmt::ExprStmt(expr_stmt)) = block_expr.statements().last()
&& let Some(expr) = expr_stmt.expr()
{
sema.type_of_expr(&expr).is_some_and(|ty| ty.original.is_never())
} else {
false
}
}
Loading