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

clippy::unnecessary_lazy_evaluations false positive when laziness prevents underflow #10071

Closed
krtab opened this issue Dec 13, 2022 · 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

@krtab
Copy link
Contributor

krtab commented Dec 13, 2022

Summary

I have the following code:

(pos % n != 0).then(|| pos - 1),

Because pos may be equal to 0, changing to (pos % n != 0).then_some(pos - 1) can trigger underflow and hence panic in debug mode. Nonetheless clippy warns:

warning: unnecessary closure used with `bool::then`
  --> src/bin/12.rs:31:13
   |
31 |             (pos % n != 0).then(|| pos - 1),
   |             ^^^^^^^^^^^^^^^----------------
   |                            |
   |                            help: use `then_some(..)` instead: `then_some(pos - 1)`
   |
   = note: `#[warn(clippy::unnecessary_lazy_evaluations)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations

Lint Name

clippy::unnecessary_lazy_evaluations

Reproducer

Cf summary

Version

rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: x86_64-unknown-linux-gnu
release: 1.65.0
LLVM version: 15.0.0

Additional Labels

No response

@krtab krtab 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 13, 2022
@kraktus
Copy link
Contributor

kraktus commented Dec 18, 2022

Hey, thanks for the report! This seem a duplicate of #9422

@krtab
Copy link
Contributor Author

krtab commented Dec 18, 2022

Thanks! It is indeed. I'm closing it as such.

@krtab krtab closed this as not planned Won't fix, can't repro, duplicate, stale Dec 18, 2022
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