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 in equality comparison #5700

Open
ebroto opened this issue Jun 9, 2020 · 4 comments
Open

redundant_clone: false positive in equality comparison #5700

ebroto opened this issue Jun 9, 2020 · 4 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied S-needs-discussion Status: Needs further discussion before merging or work can be started

Comments

@ebroto
Copy link
Member

ebroto commented Jun 9, 2020

#[derive(PartialEq)]
struct Foo;

struct Bar;

impl ToOwned for Bar {
    type Owned = Foo;
    fn to_owned(&self) -> Foo {
        Foo {}
    }
}

impl std::borrow::Borrow<Bar> for Foo {
    fn borrow(&self) -> &Bar {
        static BAR: Bar = Bar {};
        &BAR
    }
}

fn main() {
    let f = Foo {};
    let b = Bar {};

    if f == b.to_owned() {
        println!("they match")
    }
}

(playground)

Note that there is no PartialEq implementation between Foo and Bar. This generates the following warning:

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

warning: 1 warning emitted

But applying the suggestion causes this error

error[E0308]: mismatched types
  --> src/main.rs:24:13
   |
24 |     if f == b {
   |             ^ expected struct `Foo`, found struct `Bar`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

(playground)

Clippy version: 0.0.212

@flip1995
Copy link
Member

I don't see this as a bug in redundant_clone. The to_owned method is redundant, since Bar does not need to be cloned to be used.

Instead the to_owned method is used to convert a type in this example. For this, From<Bar> for Foo should be implemented.

I think this is a constructed example, that is not realistic in real code.

@flip1995 flip1995 added the S-needs-discussion Status: Needs further discussion before merging or work can be started label Jun 12, 2020
@ebroto
Copy link
Member Author

ebroto commented Jun 12, 2020

Yep I agree the code is not ideal, I ran into this when testing edge cases of cmp_owned when working in #4874.

I opened the issue because the suggestion causes a compilation error and because I understood that the temporary is actually being used in the comparison.

But maybe it's not worth the effort because it's unlikely that someone writes this? In fact having this linted by redundant_clone could be a hint that this should be changed, even though the suggestion is not that explicit.

@ebroto
Copy link
Member Author

ebroto commented Jun 12, 2020

To be clear, I assumed that if it results in a compilation error then it's a false positive, but maybe this is unlikely to happen

@flip1995
Copy link
Member

I assumed that if it results in a compilation error then it's a false positive

Normally it would be, but in this case to_owned is misused for type conversion. So I'm not sure if anything has to be changed in the redundant_clone lint. But I would leave this open, if someone else somehow experiences this.

@flip1995 flip1995 added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jun 12, 2020
@camsteffen camsteffen added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jan 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied S-needs-discussion Status: Needs further discussion before merging or work can be started
Projects
None yet
Development

No branches or pull requests

3 participants