rust-lang / rust-lang/rust-clippy

unnecessary_lazy_evaluations help produces code which formatted differently than rustfmt

Open
#8,394 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

A clippy lint produced a hint which is different than how rustfmt formats the code, which, seems weird?

Reproducer

I tried this code:

    // Get the name. This is non-optional.
    let name = parts
        .next()
        .ok_or(HeaderError::ServerTimingInvalid("must include a name"))?
        .trim_end();

(Note that it is already one level indented)

I expected to see this happen:

warning: unnecessary closure used to substitute value for `Option::None`
  --> src/trace/server_timing/parse.rs:33:16
   |
33 |       let name = parts
   |  ________________^
34 | |         .next()
35 | |         .ok_or_else(|| HeaderError::ServerTimingInvalid("must include a name"))?
   | |_______________________________________________________________________________^
   |
   = 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
help: use `ok_or` instead
   |
33 ~     let name = parts
34 ~         .next()
35 ~         .ok_or(HeaderError::ServerTimingInvalid("must include a name"))?
   |

Instead, this happened:

warning: unnecessary closure used to substitute value for `Option::None`
  --> src/trace/server_timing/parse.rs:33:16
   |
33 |       let name = parts
   |  ________________^
34 | |         .next()
35 | |         .ok_or_else(|| HeaderError::ServerTimingInvalid("must include a name"))?
   | |_______________________________________________________________________________^
   |
   = 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
help: use `ok_or` instead
   |
33 ~     let name = parts
34 ~         .next().ok_or(HeaderError::ServerTimingInvalid("must include a name"))?
   |
Version
rustc 1.58.1 (db9d1b20b 2022-01-20)
binary: rustc
commit-hash: db9d1b20bba1968c1ec1fc49616d4742c1725b4b
commit-date: 2022-01-20
host: x86_64-unknown-linux-gnu
release: 1.58.1
LLVM version: 13.0.0
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 reproducing the unnecessary_lazy_evaluations warning with the Rust 1.58.1 example and compare Clippy's suggested replacement with rustfmt's formatting. Trace the lint's help suggestion generation and verify that the resulting code is formatted consistently with the expected multiline layout.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.