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

unnecessary_lazy_evaluations suggests a fix that panics #11931

Closed
tuxbzh opened this issue Dec 5, 2023 · 2 comments
Closed

unnecessary_lazy_evaluations suggests a fix that panics #11931

tuxbzh opened this issue Dec 5, 2023 · 2 comments
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

Comments

@tuxbzh
Copy link

tuxbzh commented Dec 5, 2023

Summary

In the example below, unnecessary_lazy_evaluations suggests a fix that is not functionally equivalent to the original code and that panics (the original code works normally).

Lint Name

unnecessary_lazy_evaluations

Reproducer

I tried this code:

fn try_sub(a: u32, b: u32) -> Option<u32> {
    (a >= b).then(|| a - b)
    //(a >= b).then_some(a - b) // Suggested by clippy but panic...
}

fn main() {
    assert_eq!(try_sub(10, 4), Some(6));
    assert_eq!(try_sub(4, 10), None);
}

I saw this happen:

warning: unnecessary closure used with `bool::then`
 --> src/main.rs:2:5
  |
2 |     (a >= b).then(|| a - b)
  |     ^^^^^^^^^--------------
  |              |
  |              help: use `then_some(..)` instead: `then_some(a - b)`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations
  = note: `#[warn(clippy::unnecessary_lazy_evaluations)]` on by default

I expected to see this happen: No suggestion.

Version

rustc 1.74.0 (79e9716c9 2023-11-13)
binary: rustc
commit-hash: 79e9716c980570bfd1f666e3b16ac583f0168962
commit-date: 2023-11-13
host: x86_64-unknown-linux-gnu
release: 1.74.0
LLVM version: 17.0.4

Additional Labels

No response

@tuxbzh tuxbzh added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 5, 2023
@tuxbzh tuxbzh changed the title unnecessary_lazy_evaluations suggests a fix that panic unnecessary_lazy_evaluations suggests a fix that panics Dec 5, 2023
@y21
Copy link
Member

y21 commented Dec 5, 2023

This was fixed in #11002 and should no longer fire here in more recent nightly versions (tested 2023-12-04 and this build seems to have the fix)

@y21
Copy link
Member

y21 commented Feb 8, 2024

Closing as fixed. The fix for it also made it into today's stable, so this should no longer emit a warning on any channel. Thanks for the report!

@y21 y21 closed this as completed Feb 8, 2024
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
Projects
None yet
Development

No branches or pull requests

2 participants