Skip to content
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

False positive in needless_option_as_deref if Option cannot be moved #7846

Closed
Blaidd-Drwg opened this issue Oct 19, 2021 · 2 comments · Fixed by #8646
Closed

False positive in needless_option_as_deref if Option cannot be moved #7846

Blaidd-Drwg opened this issue Oct 19, 2021 · 2 comments · Fixed by #8646
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@Blaidd-Drwg
Copy link

Lint name:
needless_option_as_deref

I tried this code:

fn main() {
    let mut i = 0;
    let mut opt_vec = vec![Some(&mut i)];
    opt_vec[0].as_deref_mut().unwrap();
}

Clippy complains:

warning: derefed type is same as origin
 --> src/main.rs:4:5
  |
4 |     opt_vec[0].as_deref_mut().unwrap();
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `opt_vec[0]`
  |
  = note: `#[warn(clippy::needless_option_as_deref)]` on by default

However, when applying the suggestion, the build fails:

error[E0507]: cannot move out of index of `Vec<Option<&mut i32>>`
 --> src/main.rs:4:5
  |
4 |     opt_vec[0].unwrap();
  |     ^^^^^^^^^^ move occurs because value has type `Option<&mut i32>`, which does not implement the `Copy` trait
  |
help: consider borrowing the `Option`'s content
  |
4 |     opt_vec[0].as_ref().unwrap();
  |               +++++++++

Meta

Rust version (rustc -Vv):

rustc 1.58.0-nightly (bd41e09da 2021-10-18)
binary: rustc
commit-hash: bd41e09da334697c0f993b36685cb599061d9faa
commit-date: 2021-10-18
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0

@rustbot label +I-suggestion-causes-error

@Blaidd-Drwg Blaidd-Drwg added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Oct 19, 2021
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Oct 19, 2021
@WaffleLapkin
Copy link
Member

Also note that adding as_ref/as_mut is not correct in this case either, since it changes the output type to a double ref (see playground)

@H2CO3
Copy link

H2CO3 commented Mar 2, 2022

@WaffleLapkin this URLO case hits the "as_mut() is not enough" path as well, the fix is to .map(BorrowMut::borrow_mut).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
4 participants