Skip to content

Duration's constructors should not trigger or_fun_call #7483

@asomers-ax

Description

@asomers-ax

Lint name: or_fun_call

I tried this code:

let mut tick_rate = cli.time.unwrap_or(Duration::from_secs(1));

I expected to see this happen: nothing, since Duration::from_secs is a const fn. it's cheaper to use unwrap_or than unwrap_or_else, which requires creating a closure. I understand the rationale for closing #6943: Clippy can't be certain that the function will be evaluated at compile time. However, I would argue that since Clippy can neither be certain that the function will be evaluated at runtime, it should not make a recommendation one way or the other in this case. This is a case where the programmer probably knows more than Clippy does.

Instead, this happened:

warning: use of `unwrap_or` followed by a function call
   --> src/main.rs:358:34
    |
358 |     let mut tick_rate = cli.time.unwrap_or(Duration::from_secs(1));
    |                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| Duration::from_secs(1))`

Meta

  • cargo clippy -V: e.g. clippy 0.0.212 (f455e46 2020-06-20)
  • rustc -Vv:
rustc 1.55.0-nightly (952fdf2a1 2021-07-05)
binary: rustc
commit-hash: 952fdf2a1119affa1b37bcacb0c49cf9f0168ac8
commit-date: 2021-07-05
host: x86_64-unknown-freebsd
release: 1.55.0-nightly
LLVM version: 12.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions