Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

or_fun_call misses nested calls #12973

Closed
TomFryersMidsummer opened this issue Jun 21, 2024 · 1 comment · Fixed by #13085
Closed

or_fun_call misses nested calls #12973

TomFryersMidsummer opened this issue Jun 21, 2024 · 1 comment · Fixed by #13085
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't L-nursery Lint: Currently in the nursery group

Comments

@TomFryersMidsummer
Copy link

TomFryersMidsummer commented Jun 21, 2024

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 TomFryersMidsummer added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Jun 21, 2024
@J-ZhengLi J-ZhengLi added the L-nursery Lint: Currently in the nursery group label Jul 10, 2024
@J-ZhengLi
Copy link
Member

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't L-nursery Lint: Currently in the nursery group
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants