Skip to content

unnecessary_lazy_evaluations suggestion causes panic on bool::then #9422

@barryfam

Description

@barryfam

Summary

Clippy does not consider the possibility of arithmetic underflow in the non-lazy suggestion

Lint Name

unnecessary_lazy_evaluations

Reproducer

fn f(x: usize) -> Option<usize>
{
    (x >= 5).then(|| x - 5)
    // (x >= 5).then_some(x - 5)  // clippy suggestion panics
}

fn main() {
    let x = f(3);
    println!("{:?}", x);
}

clippy output:

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

warning: `playground` (bin "playground") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.68s

Version

rustc 1.65.0-nightly (8c6ce6b91 2022-09-02)
binary: rustc
commit-hash: 8c6ce6b91b172f77c795a74bfeaf74b865146b3f
commit-date: 2022-09-02
host: x86_64-unknown-linux-gnu
release: 1.65.0-nightly
LLVM version: 15.0.0

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions