-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix: Enhance remove_parentheses assist to handle return expressions #21090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
I tried to use |
Yes, this is not very good |
|
Based on your feedback, I will revise the approach as follows:
|
Maybe it can be resolved in the Try checking if self like ReturnExpr, Similar: && let Some(node) = place_of.ancestors().find(|it| it.parent().is_none_or(|it| it == parent))
&& let Some(last_token) = node.last_token()
&& let Some(next) = last_token.next_token().and_then(|t| skip_trivia_token(t, Direction::Next))
// ... |
|
A similar case: fn foo() -> bool {
// Inline variable
let x$0: bool = return false;
!x || true;
} |
This seems to have passed all the tests: if self.precedence() == ExprPrecedence::Jump
&& let Some(node) = place_of.ancestors().find(|it| it.parent().is_none_or(|it| &it == parent.syntax()))
&& let Some(next) = node.last_token().and_then(|t| skip_trivia_token(t.next_token()?, Direction::Next))
&& matches!(next.kind(), T![||] | T![&&])
{
return true;
} |
This code works perfectly |
|
Thank you for reviewing my code so actively! |
close #21063
Summary
This PR refines the
remove_parenthesesassist’s handling ofreturn-like control-flow expressions, based on the discussion in #21092.The assist now applies the following rules:
Prevent producing
return || …returnare not removed when doing so would createreturn ||Allow removal under prefix operators
return,return <expr>,break, andcontinueare removed when used under prefix operators like!(e.g.,
!(return)→!return,!(return false)→!return false).Tests
This PR adds comprehensive tests covering:
return || …(with and without values, with or without a prefix operator).!(return),!(return false),!(break),!(continue)).true || (return)→true || return).