Skip to content

Commit 68ee745

Browse files
committed
refactor: handle ret-like expressions in remove_parentheses and precedence checks
1 parent e728a04 commit 68ee745

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ mod tests {
174174
r#"fn f() { !return false }"#,
175175
);
176176

177-
// Binary operators should still allow removal
177+
// Binary operators should still allow removal unless a ret-like expression is immediately followed by `||` or `&&`.
178178
check_assist(
179179
remove_parentheses,
180180
r#"fn f() { true || $0(return) }"#,
@@ -297,14 +297,27 @@ mod tests {
297297
remove_parentheses,
298298
r#"fn f() { let _x = true && !$0(return false) && true; }"#,
299299
);
300+
check_assist_not_applicable(
301+
remove_parentheses,
302+
r#"fn f() { let _x = $0(return) || true; }"#,
303+
);
304+
check_assist_not_applicable(
305+
remove_parentheses,
306+
r#"fn f() { let _x = $0(return) && true; }"#,
307+
);
300308
}
301309

302310
#[test]
303311
fn remove_parens_return_in_disjunction_is_ok() {
304312
check_assist(
305313
remove_parentheses,
306-
r#"fn f() { let _ = true || $0(return); }"#,
307-
r#"fn f() { let _ = true || return; }"#,
314+
r#"fn f() { let _x = true || $0(return); }"#,
315+
r#"fn f() { let _x = true || return; }"#,
316+
);
317+
check_assist(
318+
remove_parentheses,
319+
r#"fn f() { let _x = true && $0(return); }"#,
320+
r#"fn f() { let _x = true && return; }"#,
308321
);
309322
}
310323

crates/syntax/src/ast/prec.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use stdx::always;
55
use crate::{
66
AstNode, Direction, SyntaxNode, T,
77
algo::skip_trivia_token,
8-
ast::{self, BinExpr, BinaryOp, Expr, HasArgList, RangeItem},
8+
ast::{self, BinaryOp, Expr, HasArgList, RangeItem},
99
match_ast,
1010
};
1111

@@ -225,19 +225,11 @@ impl Expr {
225225
// For `||`, removing parens could reparse as `<ret-like> || <closure>`.
226226
// For `&&`, we avoid introducing `<ret-like> && <expr>` into a binary chain.
227227

228-
if matches!(
229-
self,
230-
Expr::ReturnExpr(_)
231-
| Expr::BreakExpr(_)
232-
| Expr::BecomeExpr(_)
233-
| Expr::YeetExpr(_)
234-
| Expr::YieldExpr(_)
235-
) && let Some(paren_expr) = self.syntax().parent().and_then(ast::ParenExpr::cast)
236-
&& let Some(parent_expr) = paren_expr.syntax().parent().and_then(ast::Expr::cast)
237-
&& BinExpr::can_cast(parent_expr.syntax().kind())
238-
&& let Some(r_paren) = paren_expr.r_paren_token()
228+
if self.precedence() == ExprPrecedence::Jump
229+
&& let Some(node) =
230+
place_of.ancestors().find(|it| it.parent().is_none_or(|p| &p == parent.syntax()))
239231
&& let Some(next) =
240-
r_paren.next_token().and_then(|t| skip_trivia_token(t, Direction::Next))
232+
node.last_token().and_then(|t| skip_trivia_token(t.next_token()?, Direction::Next))
241233
&& matches!(next.kind(), T![||] | T![&&])
242234
{
243235
return true;

0 commit comments

Comments
 (0)