Skip to content

redundant_clone false-positive with arrays #5405

Closed

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)

Activity

  1. added
    C-bugCategory: Clippy is not doing the correct thing
    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
    on Apr 2, 2020
  2. flip1995 commented on Apr 2, 2020

    @flip1995
    Member
  3. added 2 commits that reference this issue on Apr 12, 2020
    a0defdd
    ab3946d
  4. added a commit that references this issue on Apr 14, 2020
    74e9256
  5. added a commit that references this issue on May 5, 2020
    d918cd9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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