rust-lang / rust-lang/rust-clippy

`unnecessary_lazy_evaluations`: lint `{Option,Result}::map_or_else`

Open
#15,829 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-enhancement good first issue
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Description

None of the following currently get linted:
(EDIT: this used to include map_or, but as the comments below rightfully say, its closure can't actually be made eager)

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

fn main() {
    let o = Some(42);
    o.map_or_else(|| 42, |_| 42);
    
    let r: Result<i32, i32> = Ok(42);
    r.map_or_else(|_| 42, |_| 42);
}

It would be cool if they did:

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

fn main() {
    let o = Some(42);
    o.map_or(42, |_| 42);
    
    let r: Result<i32, i32> = Ok(42);
    r.map_or(42, |_| 42);
}
Version
rustc 1.92.0-nightly (839222065 2025-10-05)
binary: rustc
commit-hash: 839222065a44ac21c15df68ed2f2f3c0127b0b8e
commit-date: 2025-10-05
host: x86_64-unknown-linux-gnu
release: 1.92.0-nightly
LLVM version: 21.1.2
Additional Labels

@rustbot label C-enhancement "good first issue"

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 unnecessary_lazy_evaluations lint implementation and its existing tests in rust-clippy. Reproduce the Option::map_or_else and Result::map_or_else examples from the issue, while keeping the stated map_or limitation in mind. Done means both examples receive the intended eager alternative without incorrectly linting map_or.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.