rust-lang / rust-lang/rust-clippy

`expect_fun_call` recommends code which is not equivalent

Open
#2,928 2 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-documentation C-enhancement L-suggestion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Clippy will suggest replacing:

Err::<(), _>("foo").expect(&format!("bar"))

with

Err:<(), _>("foo").unwrap_or_else(|_| panic!("bar"))

However, these are not equivalent. The first outputs foo: "bar", while the second will only output bar. Clippy could recommend doing the same formatting as expect, now we're literally recommending replacing expect with the exact body of that function. While it does of course avoid a single string allocation, it seems like a very aggressive lint to have on by default to avoid a case that is insignificant performance-wise for the vast majority of programs. format! should be white listed here IMO

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 expect_fun_call lint implementation and its existing tests. Reproduce the shown expect and unwrap_or_else outputs, then define the desired handling for format! and add a regression test demonstrating equivalent output before updating the recommendation.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.