Skip to content

[regression in 1.70] [clippy] redundant clone ; cloned value is neither consumed nor mutated #10870

Open
@glandium

Description

@glandium

Code

#[derive(Clone)]
pub struct Foo(&'static str);

pub fn foo(a: Foo, b: Option<Foo>) -> String {
    let b = if let Some(b) = b { b } else { a.clone() };
    let mut result = String::new();
    result.push_str(a.0);
    result.push_str(b.0);
    result
}

Current output

warning: redundant clone
 --> src/lib.rs:5:46
  |
5 |     let b = if let Some(b) = b { b } else { a.clone() };
  |                                              ^^^^^^^^ help: remove this
  |
note: cloned value is neither consumed nor mutated
 --> src/lib.rs:5:45
  |
5 |     let b = if let Some(b) = b { b } else { a.clone() };
  |                                             ^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
  = note: `#[warn(clippy::redundant_clone)]` on by default

Desired output

Nothing

Rationale and extra context

This is what the compiler says without the clone:

error[[E0382]](https://doc.rust-lang.org/stable/error_codes/E0382.html): borrow of moved value: `a`
 --> src/lib.rs:7:21
  |
4 | pub fn foo(a: Foo, b: Option<Foo>) -> String {
  |            - move occurs because `a` has type `Foo`, which does not implement the `Copy` trait
5 |     let b = if let Some(b) = b { b } else { a };
  |                                             - value moved here
6 |     let mut result = String::new();
7 |     result.push_str(a.0);
  |                     ^^^ value borrowed here after move
  |
help: consider cloning the value if the performance cost is acceptable
  |
5 |     let b = if let Some(b) = b { b } else { a.clone() };
  |                                              ++++++++

Other cases

No response

Anything else?

No response

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 have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions