pub fn something(mut maybe_side_effect: Option<&mut String>) {
for _ in 0..10 {
let _ = S { field: other(maybe_side_effect.as_deref_mut()) };
}
}
fn other(_maybe_side_effect: Option<&mut String>) {
unimplemented!()
}
pub struct S { pub field: () }
Clippy emitted a warning:
Checking playground v0.0.1 (/playground)
warning: derefed type is same as origin
--> src/lib.rs:3:34
|
3 | let _ = S { field: 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:
error[E0382]: use of moved value: `maybe_side_effect`
--> src/lib.rs:3:34
|
1 | pub fn something(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
2 | for _ in 0..10 {
| -------------- inside of this loop
3 | let _ = S { field: other(maybe_side_effect) };
| ^^^^^^^^^^^^^^^^^ value moved here, in previous iteration of loop
|
note: consider changing this parameter type in function `other` to borrow instead if owning the value isn't necessary
--> src/lib.rs:7:30
|
7 | fn other(_maybe_side_effect: Option<&mut String>) {
| ----- in this function ^^^^^^^^^^^^^^^^^^^ this parameter takes ownership of the value
help: consider moving the expression out of the loop so it is only moved once
|
2 ~ let mut value = other(maybe_side_effect);
3 ~ for _ in 0..10 {
4 ~ let _ = S { field: value };
|
I believe the warning should not have been emitted in the first place.
Summary
Clippy emits a warning but the suggested fix does not compile. This looks like another case of https://github.com/rust-lang/rust-clippy/pull/8646.
Lint Name
clippy::needless_option_as_deref
Reproducer
I tried this (somewhat minimized) code:
Clippy emitted a warning:
The suggested fix does not compile:
I believe the warning should not have been emitted in the first place.
Version
Additional Labels
@rustbot label + I-suggestion-causes-error