Skip to content

redundant_clone false-positive with arrays #5405

Closed
@arnauorriols

Description

@arnauorriols

When you need to own a value out of an array, you need to clone it. However clippy complains that the clone is redundant:

fn main() {
    let a: [String; 1] = [String::from("foo")];
    let b: String = a[0].clone();
}
// warning: redundant clone
//  --> src/main.rs:3:25
//   |
// 3 |     let b: String = a[0].clone();
//   |                         ^^^^^^^^ help: remove this
//   |
//   = note: `#[warn(clippy::redundant_clone)]` on by default
// note: this value is dropped without further use
//  --> src/main.rs:3:21
//   |
// 3 |     let b: String = a[0].clone();
//   |                     ^^^^
//   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone

If I remove the clone, I get this compiler error:

fn main() {
    let a: [String; 1] = [String::from("foo")];
    let b: String = a[0];
}
// error[E0508]: cannot move out of type `[std::string::String; 1]`, a non-copy array
//  --> src/lib.rs:3:21
//   |
// 3 |     let b: String = a[0];
//   |                     ^^^^
//   |                     |
//   |                     cannot move out of here
//   |                     move occurs because `a[_]` has type `std::string::String`, which does not implement the `Copy` trait
//   |                     help: consider borrowing here: `&a[0]`
//
// error: aborting due to previous error
//
// For more information about this error, try `rustc --explain E0508`.
$ cargo +nightly clippy -V
clippy 0.0.212 (23549a8 2020-03-16)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-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