Skip to content

needless_option_as_deref false positive in trait #14148

@bobroberts177

Description

@bobroberts177

Summary

Clippy emits a warning but the suggested fix does not compile. This looks like another case of #8646 and #13077.

Lint Name

clippy::needless_option_as_deref

Reproducer

I tried this code:

pub trait SomeTrait {
  fn something(&self, mut maybe_side_effect: Option<&mut String>) {
    other(maybe_side_effect.as_deref_mut());
    other(maybe_side_effect);
  }
}

fn other(_maybe_side_effect: Option<&mut String>) {
  unimplemented!()
}

I saw this happen:

    Checking playground v0.0.1 (/playground)
warning: derefed type is same as origin
 --> src/lib.rs:3:11
  |
3 |     other(maybe_side_effect.as_deref_mut());
  |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `maybe_side_effect`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_option_as_deref
  = note: `#[warn(clippy::needless_option_as_deref)]` on by default

warning: `playground` (lib) generated 1 warning (run `cargo clippy --fix --lib -p playground` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.50s

The suggested fix does not compile:

   Compiling playground v0.0.1 (/playground)
warning: variable does not need to be mutable
 --> src/lib.rs:2:23
  |
2 |   fn something(&self, mut maybe_side_effect: Option<&mut String>) {
  |                       ----^^^^^^^^^^^^^^^^^
  |                       |
  |                       help: remove this `mut`
  |
  = note: `#[warn(unused_mut)]` on by default

error[E0382]: use of moved value: `maybe_side_effect`
 --> src/lib.rs:4:11
  |
2 |   fn something(&self, mut maybe_side_effect: Option<&mut String>) {
  |                       --------------------- move occurs because `maybe_side_effect` has type `Option<&mut String>`, which does not implement the `Copy` trait
3 |     other(maybe_side_effect);
  |           ----------------- value moved here
4 |     other(maybe_side_effect);
  |           ^^^^^^^^^^^^^^^^^ value used here after move
  |
note: consider changing this parameter type in function `other` to borrow instead if owning the value isn't necessary
 --> src/lib.rs:8:30
  |
8 | fn other(_maybe_side_effect: Option<&mut String>) {
  |    ----- in this function    ^^^^^^^^^^^^^^^^^^^ this parameter takes ownership of the value

For more information about this error, try `rustc --explain E0382`.
warning: `playground` (lib) generated 1 warning
error: could not compile `playground` (lib) due to 1 previous error; 1 warning emitted

I believe the warning should not have been emitted in the first place.

Version

rustc 1.84.0 (9fc6b4312 2025-01-07)
binary: rustc
commit-hash: 9fc6b43126469e3858e2fe86cafb4f0fd5068869
commit-date: 2025-01-07
host: x86_64-pc-windows-msvc
release: 1.84.0
LLVM version: 19.1.5

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

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