Closed
Description
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