Skip to content

Beta/1.57: Wrong suggestion with needless_option_as_deref lint #8047

Closed
@sdroege

Description

@sdroege

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions