Skip to content

unnecessary_lazy_evaluations warns about str::len #8109

Open
@jplatte

Description

Summary

str::len within an unwrap_or_else seems to cause unnecessary_lazy_evaluations to trigger even though it's a method call, not a constant which I assumed to be the only case where unnecessary_lazy_evaluations triggers.

Reproducer

I tried this code (Playground):

fn main() {
    let x = "str:with_separator";
    let first_part_pos = x.find(':').unwrap_or_else(|| x.len());
    dbg!(first_part_pos);
}

I expected to see this happen: No warnings

Instead, this happened:

warning: unnecessary closure used to substitute value for `Option::None`
 --> src/main.rs:3:26
  |
3 |     let first_part_pos = x.find(':').unwrap_or_else(|| x.len());
  |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `unwrap_or` instead: `x.find(':').unwrap_or(x.len())`
  |
  = 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

Version

rustc 1.59.0-nightly (e6b883c74 2021-12-08)
binary: rustc
commit-hash: e6b883c74f49f32cb5d1cbad3457f2b8805a4a38
commit-date: 2021-12-08
host: x86_64-unknown-linux-gnu
release: 1.59.0-nightly
LLVM version: 13.0.0

Additional Labels

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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