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
{{ message }}
Repository navigation
option_if_let_else suggestion inapplicable when mutable borrow or owned value is used in both branches #5822
#[deny(clippy::option_if_let_else)]pubfnfoo(o:Option<i32>,mutf:implFnMut(i32)){ifletSome(i) = o {f(i)}else{f(5)}}
Apart from having the same problem as #5821, in this particular case even the suggestion of o.map_or_else(|| f(5), |i| f(i)) would be wrong because f is being used as a &mut, so it cannot be used with two closures at the same time. This also happens for f: impl FnOnce(i32).
I don't think there's any better way to write this code using any of the Option combinators, so it seems to be the lint should not fire at all.
I've found a real world example inside Clippy where presumably this bug led to an incorrect use of crate::util::snippet_with_applicability, which slipped into the code base along with the lint itself. I'm not sure whether we should immediately revert the change with #[allow(clippy::option_if_let_else)] for the time being or can leave it until this issue gets fixed though.
OK, I made a PR for it (and it's just quickly merged while I'm writing this). Thanks for the reply!
Regarding an new lint, it makes sense for me; considering a function taking a mutable reference probably means the reference is some kind of an "out" parameter.
Activity
rail-rain commentedon Aug 26, 2020
I've found a real world example inside Clippy where presumably this bug led to an incorrect use of
crate::util::snippet_with_applicability, which slipped into the code base along with the lint itself. I'm not sure whether we should immediately revert the change with#[allow(clippy::option_if_let_else)]for the time being or can leave it until this issue gets fixed though.rust-clippy/clippy_lints/src/write.rs
Lines 299 to 306 in 23db542
ebroto commentedon Aug 26, 2020
Nice catch! I would say let's allow the lint for now and fix that.
BTW wouldn't this be a nice candidate for a lint? I can't think now of a case where you actually want to pass a
&mutto a temporary to a function.Fix the wrong use of `snippet_with_applicability`
Auto merge of #5968 - rail-rain:fix_use_snippet_with_applicability, r…
rail-rain commentedon Aug 26, 2020
OK, I made a PR for it (and it's just quickly merged while I'm writing this). Thanks for the reply!
Regarding an new lint, it makes sense for me; considering a function taking a mutable reference probably means the reference is some kind of an "out" parameter.
brightly-salty commentedon Jan 24, 2021
I still have this issue on nightly, I don't know if it's been fixed or not.
edward-shen commentedon May 11, 2021
I can confirm this issue still exists as I'm running into this on cargo 1.54.0-nightly (e51522ab3 2021-05-07).
Auto merge of #7568 - dtolnay-contrib:ifletelse, r=llogiq
Auto merge of #7573 - Jarcho:option_if_let_else, r=giraffate