rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

regression: unnecessary_lazy_evaluations not triggered on thiserror enum variant

Open birkenfeld opened this issue 1 year ago • 0 comments

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

birkenfeld avatar Aug 10 '24 15:08 birkenfeld