Skip to content

Conversation

@Aditya-PS-05
Copy link
Contributor

@Aditya-PS-05 Aditya-PS-05 commented Nov 21, 2025

fixes #21063

The Remove redundant parentheses was suggesting removal of parentheses in expressions like !(return), !(break), and !(continue), which would result in invalid syntax.

So, added a case in Expr::needs_parens_in() which checks for two things:
1.) Prefix operator
2.) is_ret_like_with_no_value() which handles return, yield, break, continue, become

If both are present, the parenthesis are not removed.

Added test also.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 21, 2025
@asuto15
Copy link
Contributor

asuto15 commented Nov 22, 2025

I opened PR #21090 for the same issue, but your approach looks smarter and the test coverage is broader than mine.

To the maintainers: if this solution is preferred, I can close my PR (#21090). Please let me know your preference.

@Aditya-PS-05
Copy link
Contributor Author

@asuto15 actually I started working on the issue and completed earlier than you but could not open a pr due to time constraint. So, I opened it anyway.

Copy link
Member

@ShoyuVanilla ShoyuVanilla left a comment

Choose a reason for hiding this comment

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

Yeah, this one is a bit cleaner than #21090 (though I think both are great), so I'm going to merge this one as @asuto15 suggested.
But in general, I think the person assigned to the issue should have priority.

@ShoyuVanilla ShoyuVanilla added this pull request to the merge queue Nov 22, 2025
Merged via the queue into rust-lang:master with commit 8b45966 Nov 22, 2025
15 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 22, 2025
@A4-Tacks
Copy link
Member

A4-Tacks commented Nov 22, 2025

This does not solve issue, such as this test:

check_assist_not_applicable(
    remove_parentheses,
    r#"fn main() { let _x = true && $0(return) || true; }"#,
);
check_assist(
    remove_parentheses,
    r#"fn f() { cond && !$0(return) }"#,
    r#"fn f() { cond && !return }"#,
);

@asuto15

This comment was marked as resolved.

@A4-Tacks
Copy link
Member

If that’s the intended behavior

Yes, this is intended behavior. I missed typing a character

@asuto15
Copy link
Contributor

asuto15 commented Nov 22, 2025

Thank you!

One more question for the maintainers @ShoyuVanilla and @A4-Tacks:

Would you prefer that we treat all valueless control-flow expressions (return, break, continue) uniformly?

For example, allowing:

fn f() { !($0(return)) }   // -> !return
fn f() { !($0(break)) }    // -> !break
fn f() { !($0(continue)) } // -> !continue

(except the return || case, which must remain blocked)

This would give a simple, consistent rule.
If this direction is acceptable, I can adjust the PR(#21090) accordingly.

@A4-Tacks
Copy link
Member

valueless control-flow expressions

Consider this test:

check_assist_not_applicable(
    remove_parentheses,
    r#"fn main() { let _x = true && $0(return false) || true; }"#,
);
check_assist(
    remove_parentheses,
    r#"fn f() { cond && !$0(return false) }"#,
    r#"fn f() { cond && !return false }"#,
);

@ShoyuVanilla
Copy link
Member

This does not solve issue, such as this test:

check_assist_not_applicable(
    remove_parentheses,
    r#"fn main() { let _x = true && $0(return) || true; }"#,
);
check_assist(
    remove_parentheses,
    r#"fn f() { cond && !$0(return) }"#,
    r#"fn f() { cond && !return }"#,
);

Oh, good catch

@Aditya-PS-05
Copy link
Contributor Author

Sorry I missed this discussion. Does this pr not fixes the issue or any edge case?

@A4-Tacks
Copy link
Member

Does this pr not fixes the issue or any edge case?

The main issue is BinExpr instead of PrefixExpr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive on return for remove_parentheses

5 participants