-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
option_if_let_else suggestion inapplicable when mutable borrow or owned value is used in both branches #5822
Comments
I've found a real world example inside Clippy where presumably this bug led to an incorrect use of rust-clippy/clippy_lints/src/write.rs Lines 299 to 306 in 23db542
|
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 |
This includes a workaround of the issue rust-lang#5822, the cause of this little mistake.
…=flip1995 Fix the wrong use of `snippet_with_applicability` For the context, please see #5822 (comment) and #5822 (comment). --- changelog: none
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. |
I still have this issue on nightly, I don't know if it's been fixed or not. |
I can confirm this issue still exists as I'm running into this on cargo 1.54.0-nightly (e51522ab3 2021-05-07). |
Downgrade option_if_let_else to nursery I believe that this lint's loose understanding of ownership (#5822, #6737) makes it unsuitable to be enabled by default in its current state, even as a pedantic lint. Additionally the lint has known problems with type inference (#6137), though I may be willing to consider this a non-blocker in isolation if it weren't for the ownership false positives. A fourth false positive involving const fn: #7567. But on top of these, for me the biggest issue is I basically fully agree with #6137 (comment). In my experience this lint universally makes code worse even when the resulting code does compile. --- changelog: remove [`option_if_let_else`] from default set of enabled lints
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 becausef
is being used as a&mut
, so it cannot be used with two closures at the same time. This also happens forf: 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.Meta
cargo clippy -V
:clippy 0.0.212 (39d5a61 2020-07-17)
rustc -Vv
:The text was updated successfully, but these errors were encountered: