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

Add a lint for unnecessary trailing return #9426

Closed
huonw opened this issue Sep 23, 2013 · 5 comments
Closed

Add a lint for unnecessary trailing return #9426

huonw opened this issue Sep 23, 2013 · 5 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@huonw
Copy link
Member

huonw commented Sep 23, 2013

fn foo() -> int {
    return 1; // warning: unnecessary `return`.
}

It would be extremely neat if it could handle

fn foo() -> int {
    if bar() {
        return 1;
    } else {
        return 2;
    }
}

(This is purely stylistic: how many style lints do we want/need?)

@alexcrichton
Copy link
Member

If this is added, I think it should definitely be allow by default. It's a pretty cool idea, but I personally prefer to write return foo at the end of long functions instead of just foo (may just be me though).

@jesseray
Copy link

I omit return when working with a function/method definition or match expression that can fit comfortably on a single line, but I use return in all other situations. When I depart from this style, the code looks weird to me. (shrugs)

@Kimundi
Copy link
Member

Kimundi commented Sep 24, 2013

My personal experience is that you very quickly drop writing return everywhere except for early return after you've learned to accept last-express-return-value as natural.
If you look at the Rust source code, the majority of functions are written without return, so even if this lint would be off per default, it certainly makes sense to have it enabled for Rust to catch styling issues.

@steveklabnik
Copy link
Member

triage: no change

personal note: would love to see this.

@sanxiyn
Copy link
Member

sanxiyn commented Nov 9, 2015

This is implemented as needless_return in Clippy. It handles both of @huonw's examples and more. Check the test.

@sanxiyn sanxiyn closed this as completed Nov 9, 2015
flip1995 pushed a commit to flip1995/rust that referenced this issue Aug 24, 2023
Clippy Book Chapter Updates Reborn: Defining Lints

Revival of rust-lang#9659, I've made a few changes (mainly ones from reviews from rust-lang#9426, like removing mid-section storytelling) and some re-writes. The goal of the project would be to slowly re-write the Clippy book (only chapters that need it) to more up-to-date style.

## Notes

- Things like `git status` commands have changed, we need to make sure that they're correct.
- As this is a team-wide effort, I would please ask anyone and everyone to read it and give your opinion.
- To talk about the whole project, please use the tracking issue for the project rust-lang#10597  (It also contains a timeline, discussions and more information)

---

changelog: Add a new "Defining lints" chapter to the book

r? `@flip1995`
flip1995 pushed a commit to flip1995/rust that referenced this issue Sep 7, 2023
Clippy Book Chapter Updates Reborn: Writing tests

This PR adds  a new chapter to the book: "Writing tests". The changes have been mainly done from reviews from rust-lang#9426 and some minor re-writes.

## Notes

- We still need to check that the `git status`es are correct, as `cargo dev new_lint` changed a lot since 2022.
- Requires rust-lang#10598: Link to "Emitting Lints" where I flagged with `FIXME:`.
- To talk about the whole project, please use the tracking issue for the project rust-lang#10597 (It also contains a timeline, discussions and more information)

changelog: Add a new "Writing tests" chapter to the book
r? `@flip1995`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

6 participants