Open tvolk131 opened 1 month ago
while similar to option_map_or_none
, this lint would need to modify lambda code, which I would think is slightly trickier:
|v| { let i = foo(); Some(i + v) }
|v| if v > 10 { Some(v+1) } else { Some(v-1) }
|v| match v { 1 => Some(1), _ => Some(v+1) }
In other words, there would need to be code evaluating every code path inside the lambda to check that every one either returns Some(...)
or !
(i.e. panics)
this lint would need to modify lambda code
Good point! I was thinking solely of lambdas that immediately return None
or Some
, but the examples you listed would be nice to tackle too. I imagine the simple case could be done first, and the cases you mentioned could be added later.
What it does
Very similar to the
option_map_or_none
rule.When calling
map_or_else()
on anOption<T>
and the following two conditions are met:default
parameter is a lambda that always returnsNone
f
parameter is a lambda that always returnsSome
This is effectively a more verbose way of doing what
Option::map()
does.Advantage
Code is shorter, more expressive, and more concise.
Drawbacks
Given that this rule is only applicable within such narrow constraints, I don't think there is any downside.
Example
Could both be written as: