Skip to content
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

fix incorrect suggestions related to parentheses in needless_return #14094

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lapla-cogito
Copy link
Contributor

@lapla-cogito lapla-cogito commented Jan 29, 2025

close #12157

needless_return sometimes makes incorrect suggestions by omitting necessary enclosing parentheses. This PR resolves the issue by using clippy_utils::sugg::Sugg.

changelog: [needless_return]: now makes correct suggestions which require enclosing parentheses

@rustbot
Copy link
Collaborator

rustbot commented Jan 29, 2025

r? @Alexendoo

rustbot has assigned @Alexendoo.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 29, 2025
help: remove `return`
|
LL - return unsafe { todo() } as *const i32;
LL + (unsafe { todo() } as *const i32)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are parentheses required here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's because without this enclosing parentheses, the suggested code won't compile (see the original issue).
However, I know this change adds some essentially unneeded parentheses like this but this is ok as far as compiling is concerned, so I wonder if there isn't a better way...

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok for the present case, but I really don't like the extra parentheses added as soon as there is a binary expression. What are the case we need to detect? It looks like this is any binary expression (in the sense of Sugg, where a cast is also a binary expression) whose first argument is a block (unsafe or not):

pub fn f() -> u32 {
    { 10 } + 1
}

also requires parentheses either around { 10 } or around { 10 } + 1.

Maybe we should enhance Sugg with a maybe_stmt() which would add parentheses in this case, as it looks like this is needed only when the expression happens where a statement could also be expected. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Autofix for needless_return with unsafe block produces invalid code
4 participants