rust-lang / rust-lang/rust-clippy

redundant_clone: false positive using ToOwned

Open
#6,729 9 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-enhancement E-medium L-suggestion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

The following code :

pub fn own<T, U>(v: T) -> U
where
    T: ToOwned<Owned = U>,
{
    v.to_owned()
}

generates the following lint:

warning: redundant clone
 --> src/main.rs:5:6
  |
5 |     v.to_owned()
  |      ^^^^^^^^^^^ help: remove this
  |
  = note: `#[warn(clippy::redundant_clone)]` on by default
note: this value is dropped without further use
 --> src/main.rs:5:5
  |
5 |     v.to_owned()
  |     ^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone

However, removing the .to_owned leads to a compilation error since the return types are different.

May be related to #5700

Meta
  • cargo clippy -V: clippy 0.1.52 (e9920ef 2021-02-11)
  • rustc -Vv:
    rustc 1.52.0-nightly (e9920ef77 2021-02-11)
    binary: rustc
    commit-hash: e9920ef7749d11fc71cc32ca4ba055bcfeaab945
    commit-date: 2021-02-11
    host: x86_64-unknown-linux-gnu
    release: 1.52.0-nightly
    LLVM version: 11.0.1
    

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report from the src/main.rs example with cargo clippy, focusing on the redundant_clone lint and the ToOwned<Owned = U> bound. Done means the valid v.to_owned() call is not reported as a redundant clone, while the example continues to compile.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.