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

Needless Return at the end of function inside a closure suggests invalid code #6501

Closed
fee1-dead opened this issue Dec 23, 2020 · 3 comments · Fixed by #6549
Closed

Needless Return at the end of function inside a closure suggests invalid code #6501

fee1-dead opened this issue Dec 23, 2020 · 3 comments · Fixed by #6549
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@fee1-dead
Copy link
Member

This code:

fn foo(bar: Result<(), ()>) {
    bar.unwrap_or_else(|_| return)
}

is suggested to remove the return, like this:

fn foo(bar: Result<(), ()>) {
    bar.unwrap_or_else(|_| )
}

Which is invalid rust code.

Meta

  • cargo clippy -V: clippy 0.0.212 (bb1fbbf 2020-12-22)
  • rustc -Vv:
rustc 1.50.0-nightly (bb1fbbf84 2020-12-22)
binary: rustc
commit-hash: bb1fbbf84455fbad9afd26c17e0f725019322655
commit-date: 2020-12-22
host: x86_64-unknown-linux-gnu
release: 1.50.0-nightly
@fee1-dead fee1-dead added the C-bug Category: Clippy is not doing the correct thing label Dec 23, 2020
@giraffate giraffate added I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Dec 23, 2020
@bors bors closed this as completed in 40ce9f8 Jan 17, 2021
@ThibsG
Copy link
Contributor

ThibsG commented Jan 17, 2021

@fee1-dead commented (by email):

Hey, I am the issue reporter, and I think it is still incorrect behavior in some cases, because when the result is Result<(), ()>, it is fine to use an empty closure, but when I use unwrap_or_else on an actual result with type (Result<Foo, u8>), I can't use |_| {} because that would return () instead of Foo, but return works.

@ThibsG
Copy link
Contributor

ThibsG commented Jan 17, 2021

@fee1-dead If I understand correctly, you have something like:

struct Foo;
fn bar(foo: Result<Foo, u8>) -> Foo {
    foo.unwrap_or_else(|_| return Foo)
}

the suggestion would be to remove the return statement like this:

    foo.unwrap_or_else(|_| Foo)

Playground

Is that the correct situation you are referring to?

@fee1-dead
Copy link
Member Author

@ThibsG, I first commented on the PR, but after some experimenting I realized I can’t return from closure so I just removed the comment.

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-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants