mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-12-23 08:48:08 +00:00
fix: never remove parens from prefix ops with valueless return/break/continue
This commit is contained in:
parent
636c3aa24e
commit
b80ca3f35e
2 changed files with 30 additions and 0 deletions
|
|
@ -162,6 +162,30 @@ mod tests {
|
|||
check_assist_not_applicable(remove_parentheses, r#"fn f() { if $0(return) {} }"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_parens_prefix_with_return_no_value() {
|
||||
// removing `()` from !(return) would make `!return` which is invalid syntax
|
||||
check_assist_not_applicable(
|
||||
remove_parentheses,
|
||||
r#"fn main() { let _x = true && !$0(return) || true; }"#,
|
||||
);
|
||||
check_assist_not_applicable(remove_parentheses, r#"fn f() { !$0(return) }"#);
|
||||
check_assist_not_applicable(remove_parentheses, r#"fn f() { !$0(break) }"#);
|
||||
check_assist_not_applicable(remove_parentheses, r#"fn f() { !$0(continue) }"#);
|
||||
|
||||
// Binary operators should still allow removal
|
||||
check_assist(
|
||||
remove_parentheses,
|
||||
r#"fn f() { true || $0(return) }"#,
|
||||
r#"fn f() { true || return }"#,
|
||||
);
|
||||
check_assist(
|
||||
remove_parentheses,
|
||||
r#"fn f() { cond && $0(return) }"#,
|
||||
r#"fn f() { cond && return }"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_parens_return_with_value_followed_by_block() {
|
||||
check_assist(
|
||||
|
|
|
|||
|
|
@ -226,6 +226,12 @@ impl Expr {
|
|||
return false;
|
||||
}
|
||||
|
||||
// Special-case prefix operators with return/break/etc without value
|
||||
// e.g., `!(return)` - parentheses are necessary
|
||||
if self.is_ret_like_with_no_value() && parent.is_prefix() {
|
||||
return true;
|
||||
}
|
||||
|
||||
if self.is_paren_like()
|
||||
|| parent.is_paren_like()
|
||||
|| self.is_prefix()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue