rust-lang / rust-lang/rust-clippy

regression: unnecessary_lazy_evaluations not triggered on thiserror enum variant

Open
#13,249 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

In stable (1.79.0), unnecessary_lazy_evaluations does not trigger when the value of the closure is a constant error variant in an enum that derives thiserror::Error.

  • This was emitting the correct warning on 1.63.0.
Lint Name

unnecessary_lazy_evaluations

Reproducer

I tried this code:

use std::io;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
    #[error("no devices found")]
    NoDevices,
    #[error(transparent)]
    Io(#[from] io::Error),
}

pub struct Master {}

impl Master {
    pub fn get_info(&self) -> Result<bool, Error> {
        let devices = [false];
        let first_device = devices.first().ok_or_else(|| Error::NoDevices)?;
        Ok(*first_device)
    }
}

I expected to see this happen: (from 1.63.0)

warning: unnecessary closure used to substitute value for `Option::None`
  --> src/master.rs:22:28
   |
22 |         let first_device = devices.first().ok_or_else(|| Error::NoDevices)?;
   |                            ^^^^^^^^^^^^^^^^-------------------------------
   |                                            |
   |                                            help: use `ok_or(..)` instead: `ok_or(Error::NoDevices)`
   |
   = 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

Instead, this happened:
no warning

Version
rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7

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 with the unnecessary_lazy_evaluations lint and reproduce the issue using the Rust code in the report, including the thiserror-derived enum. Confirm that the constant Error::NoDevices case emits the expected ok_or suggestion, and add a regression test covering this pattern.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.