Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redundant_clone: false positive using ToOwned #6729

Open
nicomem opened this issue Feb 12, 2021 · 8 comments
Open

redundant_clone: false positive using ToOwned #6729

nicomem opened this issue Feb 12, 2021 · 8 comments
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@nicomem
Copy link

nicomem commented Feb 12, 2021

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
    
@camsteffen
Copy link
Contributor

The problem is that your function takes a value instead of a reference. But it would be better if redundant_clone was more clear about that. So this is an enhancement rather than a false positive.

@camsteffen camsteffen added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy L-suggestion Lint: Improving, adding or fixing lint suggestions labels Feb 12, 2021
@camsteffen
Copy link
Contributor

In fact, it really should be needless_pass_by_value instead.

@humb1t
Copy link

humb1t commented May 25, 2021

@camsteffen - I had similar issue and if I change function to take a reference it would still cause complains from clippy.

@humb1t
Copy link

humb1t commented May 25, 2021

    9         pub fn new<P: AsRef<Path> + ToOwned<Owned = PathBuf>>(root_path: &P) -> Self {
    8             Configuration {
    7                 #[allow(clippy::redundant_clone)]
    6                 root_path: root_path.to_owned(),
    5                 stream_read_ahead: 10,
    4                 stream_buffer_size: CHUNK_BYTES_SIZE,
    3             }
    2         }

@pitaj
Copy link
Contributor

pitaj commented Apr 2, 2022

@humb1t I wasn't able to reproduce your issue (playground)

Perhaps it's been fixed. Can you provide a complete example?

@humb1t
Copy link

humb1t commented Apr 4, 2022

@pitaj looks like it was fixed. Thank you!

@flip1995 flip1995 reopened this Jul 12, 2022
@flip1995
Copy link
Member

Just noticed that the original example still triggers the lint.

@flip1995 flip1995 added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. and removed good-first-issue These issues are a good way to get started with Clippy labels Jul 12, 2022
@Kimundi
Copy link
Member

Kimundi commented Jun 11, 2023

Lint still triggers on current rust playground (1.70), another short example:

fn example<T: ToOwned<Owned = String>>(v: T) -> String {
    v.to_owned()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

No branches or pull requests

6 participants