Skip to content

Following cmp_owned hint leads to different behaviour #7769

@Schaeff

Description

@Schaeff

I have code which uses different PartialEq implementations, which is arguably not the best design, but clippy complains about unnecessary allocations and its suggestion ends up using another PartialEq implementation.

Lint name: cmp_owned

I tried this code, before the suggestion it prints Strict! which is the derived PartialEq, after it prints Optimistic! which is the more optimistic PartialEq.

#[derive(PartialEq)] enum MaybeU32 { Unknown, Known(u32), } impl From<u32> for MaybeU32 { fn from(a: u32) -> Self { MaybeU32::Known(a) } } impl PartialEq<u32> for MaybeU32 { fn eq(&self, other: &u32) -> bool { use MaybeU32::*; match self { Known(number) => number == other, _ => true, // we optimistically return true now because it may end up being the case } } } fn main() { if MaybeU32::Unknown == MaybeU32::from(32u32) { println!("Optimistic!"); } else { println!("Strict!"); } }

Meta

Rust version (rustc -Vv):

rustc 1.46.0-nightly (f455e46ea 2020-06-20) binary: rustc commit-hash: f455e46eae1a227d735091091144601b467e1565 commit-date: 2020-06-20 host: x86_64-unknown-linux-gnu release: 1.46.0-nightly LLVM version: 10.0 

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions