Skip to content

False positive for or_fun_call #1609

Closed
Closed
@tbu-

Description

@tbu-

I got the following message:

warning: use of `ok_or` followed by a function call
   --> src/file.rs:220:5
    |
220 |     o.ok_or(io::Error::new(io::ErrorKind::InvalidData, SeekOverflow(())))
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: #[warn(or_fun_call)] on by default
help: try this
    |     o.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, SeekOverflow(())))
    = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#or_fun_call

AIUI, this does not invoke allocations (SeekOverflow is a zero-sized struct (ZST) and ZSTs aren't actually allocated), and can be const-folded by the compiler. Thus, this ok_or_else shouldn't provide benefit over ok_or.

To make it a bit more obvious, here's an example that definitely doesn't invoke any allocations:

warning: use of `ok_or` followed by a function call
 --> src/lib.rs:4:38
  |
4 |     let x: Result<(), AtomicUsize> = Some(()).ok_or(AtomicUsize::new(0));
  |                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(or_fun_call)] on by default
help: try this
  |     let x: Result<(), AtomicUsize> = Some(()).ok_or_else(|| AtomicUsize::new(0));
  = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#or_fun_call

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.I-false-positiveIssue: The lint was triggered on code it shouldn't haveT-middleType: Probably requires verifiying types

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions