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

Added FORCED_RETURN lint. #3494

Merged
merged 5 commits into from
Dec 6, 2018
Merged

Added FORCED_RETURN lint. #3494

merged 5 commits into from
Dec 6, 2018

Conversation

daxpedda
Copy link
Contributor

@daxpedda daxpedda commented Dec 5, 2018

This adds a new lint that warns if return is not used, which is the opposite of clippy::needless_return.

Questions:

  • Should I add a check for ; at the end? I thought that would be more a thing for rustfmt.
  • Should I add more tests?
  • I made the functions that I needed into associated functions instead of methods, is that fine? Could also make them into closures?
  • I needed to add #[allow(clippy::needless_bool)] and #[allow(clippy::match_bool)] to the test to make it work properly. Other tests had it and others didn't, how should this be handled?

Thanks.
Fixes #2685.

@phansch phansch added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Dec 5, 2018
@phansch
Copy link
Member

phansch commented Dec 5, 2018

Should I add a check for ; at the end? I thought that would be more a thing for rustfmt.

Yup, that's more a thing for rustfmt

Should I add more tests?

Looks like you re-used the existing needless_return tests, which seems good enough to me 👍

I made the functions that I needed into associated functions instead of methods, is that fine? Could also make them into closures?

It looks fine as it is. I don't really mind either way, apart from Closures.

I needed to add #[allow(clippy::needless_bool)] and #[allow(clippy::match_bool)] to the test to make it work properly. Other tests had it and others didn't, how should this be handled?

Generally we want to make the tests as simple as possible so that they are shorter and more readable. If allowing a lint helps with that, then it's fine. (as in this case)

Copy link
Member

@phansch phansch left a comment

Choose a reason for hiding this comment

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

just a nit regarding the documentation text

clippy_lints/src/forced_return.rs Outdated Show resolved Hide resolved
Better clarification in the docs.
Ran `update_lints`.
Seems I'm not smart enough to run the tests locally before committing.
@oli-obk
Copy link
Contributor

oli-obk commented Dec 5, 2018

So. I read up on the RFC. Specifically relevant is this section

"restriction" lints follow all the rules for semantic changes,

This lint is no semantic change, OK

but do not bother with the rules for the lint being useful to most rust programmers.

Not useful to most rust programmers, OK

A restriction lint must still be such that you have a good reason to enable it — "I dislike such code" is insufficient — but will likely be a lint most programmers wish to keep off by default for most of their code.

The motivation in the docs is lacking imo.

The goal of restriction lints is to provide tools with which you can supplement the language checks in very specific cases where you need it, e.g. forbidding panics from a certain area of code.

This is not making the language stricter, as any expression is still a value, only the return position is affected. It might fit under the hypothetical defensive lint category: https://internals.rust-lang.org/t/defensive-programming-clippy-lint-category/8210 But that first needs an RFC

@daxpedda
Copy link
Contributor Author

daxpedda commented Dec 5, 2018

I could write something up if it helps?:
Why is this bad? Actually omitting the return keyword is idiomatic Rust code. Programmers coming from other languages might prefer the expressiveness of return. It's possible to miss the last returning statement because the only difference is a missing ;. Especially in bigger code with multiple return paths having a return keyword makes it easier to find the corresponding statements.

I'm not sure if I should write it all up under Why is this bad?, not sure where else though.
Otherwise we could also wait until the defensive lint category goes through?

@Manishearth
Copy link
Member

Lints should be named after the thing they lint for, so this should be implicit-return or something, I think.

Covered all other kinds besides `ExprKind::Lit`.
Added check for replacing `break` with `return`.
@daxpedda
Copy link
Contributor Author

daxpedda commented Dec 5, 2018

I added a check and test for replacing break with return as seen here:

fn test_loop() -> bool {
    loop {
        break true;
    }
}

resolves to:

fn test_loop() -> bool {
    loop {
        return true;
    }
}

Is that desirable or should I remove it?

@oli-obk
Copy link
Contributor

oli-obk commented Dec 5, 2018

I'm not sure if I should write it all up under Why is this bad?, not sure where else though.

The additional text you suggested seems good.

Added comment to explain usage of MIR.
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.

None yet

4 participants