-
Notifications
You must be signed in to change notification settings - Fork 1.6k
break; else / return; else #112
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
Comments
That reminds me: Is there a lint for Also, what I've seen in JS code a lot: |
Yeah, but in Rust, the return is optional if it is the last statement, so you'd usually write Actually this could be two lints:
The former would be quite simple and very fitting to clippy's stated mission, while the latter would probably be a bit complex (it could use much of the machinery of eq_op), but be very useful. |
I'm sorry for hijacking this issue. (Feel free to copy my comments into new issues. I'm a work and just wanted to dump my thoughts.)
And we should have a lint to tell beginners just that. :) You are right, @llogiq, these can be two (if not three, see below) separate lints. I agree that detecting superfluous returns should be a lint. Your second point is actually a superset of what I wanted to suggest; since returns in the conditional branches are quite common (as early returns), beginners may also write return in else branches. My suggestion is to detect if there are returns in all branches and suggest to return the if expression. (If this is the last expression, the What do you think of extracting code of else blocks that return? I'm talking about code like:
which could be more readable as this:
This could be another lint, but it's also a concern of code style. |
I just submitted #113 Warn on Superfluous returns. As for the other proposal, I'm unsure if I want to warn on this (at least yet). Some people frown at early returns, readability be damned. 😄 So we would preempt a discussion on the true Rust style that has not yet taken place, if I'm up to date. I happen to own a good set of flame-retardant underwear, but even thusly armed I dare not go there. Oh, and no need to be sorry. |
We may want to match needless else blocks in conditions whose then-block ends with break or return.
Example:
could be
The text was updated successfully, but these errors were encountered: