Skip to content

New lint: "unnecessary return foo() because foo returns ()" #8055

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

Closed
alco opened this issue Jul 26, 2013 · 5 comments
Closed

New lint: "unnecessary return foo() because foo returns ()" #8055

alco opened this issue Jul 26, 2013 · 5 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@alco
Copy link
Contributor

alco commented Jul 26, 2013

As suggested by @huonw in IRC.

From sha1 implementation here:

fn mk_result(st: &mut Sha1, rs: &mut [u8]) { ... }
fn result(&mut self, out: &mut [u8]) { return mk_result(self, out); }

This last line should emit the warning.

@msullivan
Copy link
Contributor

I don't really buy it. In this particular case, the return is silly, but there are plenty of places where it isn't.

if something {
   return foo();
}
// more stuff

We could have something more specific, but I don't really see the point.

@nikomatsakis
Copy link
Contributor

I sometimes intentionally write return foo() where foo() returns (), because I want there to be a compile error should foo() not return unit at some point in the future (i.e., if it should change to return a Result)

@alexcrichton
Copy link
Member

Closing, this is an aspect of the language which I think is quite useful, and it makes sense in a typesystem sort-of-way because if a function returns unit you can then continue to return the unit.

@nikomatsakis
Copy link
Contributor

Just to agree with @alexcrichton -- I often purposefully do return foo() when I know foo() has unit return type, because I want to get a compilation error if the return type of foo() should change.

@nikomatsakis
Copy link
Contributor

Oh, I see I wrote that already. Old age.

flip1995 pushed a commit to flip1995/rust that referenced this issue Apr 22, 2025
)

Closes rust-lang#9191
Closes rust-lang#14444
Closes rust-lang#8055

Adds a new helper to partly check for side effects by recursively
checking if the iterator type contains closures with mutable captures.

changelog: [`double_ended_iterator_last`] fix FP when iter has side
effects
changelog: [`needless_collect`] fix lint not consider side effects
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. C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

4 participants