Skip to content

Beta/1.57: Wrong suggestion with needless_option_as_deref lint #8047

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

Closed
sdroege opened this issue Nov 30, 2021 · 1 comment · Fixed by #8646
Closed

Beta/1.57: Wrong suggestion with needless_option_as_deref lint #8047

sdroege opened this issue Nov 30, 2021 · 1 comment · 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

@sdroege
Copy link

sdroege commented Nov 30, 2021

Lint name: needless_option_as_deref

I tried this code:

fn foo(_x: Option<&mut i32>) {}

fn main() {
    let mut y = 0;
    let mut x = Some(&mut y);
    
    foo(x.as_deref_mut());
    println!("{:?}", x);
}

I expected to see this happen: no clippy warnings

Instead, this happened:

warning: derefed type is same as origin
 --> src/main.rs:7:9
  |
7 |     foo(x.as_deref_mut());
  |         ^^^^^^^^^^^^^^^^ help: try this: `x`
  |
  = note: `#[warn(clippy::needless_option_as_deref)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_option_as_deref

warning: `playground` (bin "playground") generated 1 warning

See playground link: https://play.rust-lang.org/?version=beta&mode=debug&edition=2021&gist=2a51686bfc1dd26539145888b18e8b09

Applying that suggestion causes compilation to fail:

error[E0382]: borrow of moved value: `x`
 --> src/main.rs:8:22
  |
5 |     let mut x = Some(&mut y);
  |         ----- move occurs because `x` has type `Option<&mut i32>`, which does not implement the `Copy` trait
6 |     
7 |     foo(x);
  |         - value moved here
8 |     println!("{:?}", x);
  |                      ^ value borrowed here after move

Meta

Rust version (rustc -Vv):

rustc 1.57.0 (f1edd0429 2021-11-29)
binary: rustc
commit-hash: f1edd0429582dd29cccacaf50fd134b05593bd9c
commit-date: 2021-11-29
host: x86_64-unknown-linux-gnu
release: 1.57.0
LLVM version: 13.0.0

@rustbot label +I-suggestion-causes-error

@sdroege sdroege 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 Nov 30, 2021
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Nov 30, 2021
gstreamer-github pushed a commit to sdroege/gstreamer-rs that referenced this issue Dec 2, 2021
@Thomasdezeeuw
Copy link
Contributor

Hit this as well, maybe it should suggest as_mut/as_ref instead?

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
3 participants