rust-lang / rust-lang/rust-clippy

`unnecessary_lazy_evaluations` fires when the closure has a `match`

Open
#15,832 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

I'd argue that match is not completely free, so it shouldn't be made eager? Though I guess it's a bit less expensive in my case, because the enum variants don't actually contain values that are then used in the match arms.

Lint Name

unnecessary_lazy_evaluations

Reproducer

I tried this code:

#![allow(unused)]
#![warn(clippy::unnecessary_lazy_evaluations)]

enum MyEnum {
    Foo,
    Bar,
    Baz,
}

fn foo(opt: Option<u32>, e: MyEnum) -> Option<u32> {
    opt.or_else(|| match e {
        MyEnum::Foo => Some(7),
        MyEnum::Bar => Some(13),
        MyEnum::Baz => None,
    })
}

I saw this happen:

warning: unnecessary closure used to substitute value for `Option::None`
  --> src/main.rs:11:5
   |
11 | /     opt.or_else(|| match e {
12 | |         MyEnum::Foo => Some(7),
13 | |         MyEnum::Bar => Some(13),
14 | |         MyEnum::Baz => None,
15 | |     })
   | |______^
   |

I expected to see this happen:
No lint

Version
rustc 1.92.0-nightly (3d8c1c1fc 2025-10-06)
binary: rustc
commit-hash: 3d8c1c1fc077d04658de63261d8ce2903546db13
commit-date: 2025-10-06
host: x86_64-unknown-linux-gnu
release: 1.92.0-nightly
LLVM version: 21.1.2
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the implementation of the unnecessary_lazy_evaluations lint and read how it handles closures containing match expressions. Run the supplied Rust reproducer, then verify that the lint no longer fires for this case while its other intended diagnostics remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.