-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.Call for participation: Medium difficulty level problem and requires some initial experience.I-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't haveT-middleType: Probably requires verifiying typesType: Probably requires verifiying types
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
Ekleog
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.Call for participation: Medium difficulty level problem and requires some initial experience.I-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't haveT-middleType: Probably requires verifiying typesType: Probably requires verifiying types