Skip to content

Commit 8b45966

Browse files
authored
Merge pull request #21092 from Aditya-PS-05/fix/remove-parens-prefix-operators
fix: never remove parens from prefix ops with valueless return/break/continue
2 parents 35c0661 + b80ca3f commit 8b45966

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,30 @@ mod tests {
162162
check_assist_not_applicable(remove_parentheses, r#"fn f() { if $0(return) {} }"#);
163163
}
164164

165+
#[test]
166+
fn remove_parens_prefix_with_return_no_value() {
167+
// removing `()` from !(return) would make `!return` which is invalid syntax
168+
check_assist_not_applicable(
169+
remove_parentheses,
170+
r#"fn main() { let _x = true && !$0(return) || true; }"#,
171+
);
172+
check_assist_not_applicable(remove_parentheses, r#"fn f() { !$0(return) }"#);
173+
check_assist_not_applicable(remove_parentheses, r#"fn f() { !$0(break) }"#);
174+
check_assist_not_applicable(remove_parentheses, r#"fn f() { !$0(continue) }"#);
175+
176+
// Binary operators should still allow removal
177+
check_assist(
178+
remove_parentheses,
179+
r#"fn f() { true || $0(return) }"#,
180+
r#"fn f() { true || return }"#,
181+
);
182+
check_assist(
183+
remove_parentheses,
184+
r#"fn f() { cond && $0(return) }"#,
185+
r#"fn f() { cond && return }"#,
186+
);
187+
}
188+
165189
#[test]
166190
fn remove_parens_return_with_value_followed_by_block() {
167191
check_assist(

crates/syntax/src/ast/prec.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ impl Expr {
226226
return false;
227227
}
228228

229+
// Special-case prefix operators with return/break/etc without value
230+
// e.g., `!(return)` - parentheses are necessary
231+
if self.is_ret_like_with_no_value() && parent.is_prefix() {
232+
return true;
233+
}
234+
229235
if self.is_paren_like()
230236
|| parent.is_paren_like()
231237
|| self.is_prefix()

0 commit comments

Comments
 (0)