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

Wrong needless_return suggestion with blocks #2472

Open
lnicola opened this issue Feb 20, 2018 · 4 comments
Open

Wrong needless_return suggestion with blocks #2472

lnicola opened this issue Feb 20, 2018 · 4 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@lnicola
Copy link
Member

lnicola commented Feb 20, 2018

Clippy suggests changing return { e } + 10; to { x } + 10 instead of ({ x }) + 10 or ({ x } + 10), and that doesn't compile.

I'm not sure what it should do, always parenthesise the inner span? rustfmt doesn't fix that, and it would be a bit ugly. Should it recurse into the expression to see if there are any blocks?

@oli-obk
Copy link
Contributor

oli-obk commented Feb 20, 2018

huh? this feels more like a rustc bug that it doesn't accept { x } + y as a trailing return expression

@lnicola
Copy link
Member Author

lnicola commented Feb 20, 2018

Apparently, the block is a statement, not an expression. I wasn't aware of the difference until running into this.

@birkenfeld
Copy link
Contributor

It's good that this is a compile error, consider - instead of +:

fn x() -> i32 {
    { 1 } - 1
}

fn y() -> i32 {
    { 1 }
    -1
}

Does it return 0 or -1?

@steffahn
Copy link
Member

steffahn commented Jun 1, 2021

So the right thing to suggest here would be to remove the return but add parentheses.

@giraffate giraffate added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Jun 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

5 participants