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 gives wrong suggestions for then #11793

Closed
danieledapo opened this issue Nov 12, 2023 · 1 comment · Fixed by #11002
Closed

unnecessary_lazy_evaluations gives wrong suggestions for then #11793

danieledapo opened this issue Nov 12, 2023 · 1 comment · Fixed by #11002
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

@danieledapo
Copy link
Contributor

Summary

In the attached scenario unnecessary_lazy_evaluations is triggered, but it gives a wrong suggestion because the lazy expression relies on the fact that's called only when the number is greater than 0 because otherwise always subtracting 1 would result in an underflow.

Lint Name

unnecessary_lazy_evaluations

Reproducer

I tried this code:

pub fn sub(n: u8) -> Option<u8> {
    (n > 0).then(|| n - 1)
}

I saw this happen:

    Checking playground v0.0.1 (/playground)
warning: unnecessary closure used with `bool::then`
 --> src/lib.rs:4:5
  |
4 |     (n > 0).then(|| n - 1)
  |     ^^^^^^^^--------------
  |             |
  |             help: use `then_some(..)` instead: `then_some(n - 1)`
  |
  = 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

warning: `playground` (lib) generated 1 warning (run `cargo clippy --fix --lib -p playground` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.61s

I expected to see this happen:

No lint should be emitted

Version

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2

Additional Labels

No response

@danieledapo danieledapo 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 Nov 12, 2023
@y21
Copy link
Member

y21 commented Nov 12, 2023

Duplicate of #9814, will be fixed by #11002

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

Successfully merging a pull request may close this issue.

2 participants