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

or_fun_call misses nested calls

Open TomFryersMidsummer opened this issue 2 months ago • 0 comments

Summary

If f() is too expensive to compute eagerly, then of course f() + 1 must be too. However x.unwrap_or(f()) triggers or_fun_call, but x.unwrap_or(f() + 1) does not.

Lint Name

or_fun_call

Reproducer

I tried this code:

pub fn foo(f: fn() -> i32, x: Option<i32>) -> (i32, i32) {
    let b = x.unwrap_or(f());
    let c = x.unwrap_or(f() + 1);
    (b, c)
}
clippy-driver --crate-type lib foo.rs -W clippy::or_fun_call`

I expected to see this happen:

Two similar errors.

Instead, this happened:

warning: use of `unwrap_or` followed by a function call
 --> foo.rs:2:15
  |
2 |     let b = x.unwrap_or(f());
  |               ^^^^^^^^^^^^^^ help: try: `unwrap_or_else(f)`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#or_fun_call
  = note: requested on the command line with `-W clippy::or-fun-call`

warning: 1 warning emitted

Version

rustc 1.81.0-nightly (684b3553f 2024-06-20)
binary: rustc
commit-hash: 684b3553f70148ded97a80371c2405984d4f6aa7
commit-date: 2024-06-20
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7

TomFryersMidsummer avatar Jun 21 '24 09:06 TomFryersMidsummer