You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Clippy's suggested fix for returning 1 or 0 from an if statement is unintuitive
Reproducer
writing code like
if opt.is_some(){1}else{0}
leads clippy to suggest usize::from(opt.is_some()), i.e. coercing a boolean into an integer. It is not obvious to me that a boolean coerced into an integer will yield 0 for false and 1 for true. I'm not even sure why rust implements this From.
I've settled on opt.map_or(0, |_| 1) in order to avoid the lint while also still being clear about the output values.
Summary
Clippy's suggested fix for returning 1 or 0 from an if statement is unintuitive
Reproducer
writing code like
leads clippy to suggest
usize::from(opt.is_some())
, i.e. coercing a boolean into an integer. It is not obvious to me that a boolean coerced into an integer will yield 0 for false and 1 for true. I'm not even sure why rust implements this From.I've settled on
opt.map_or(0, |_| 1)
in order to avoid the lint while also still being clear about the output values.Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: