about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/cmp_owned/with_suggestion.fixed
blob: 85d0991bef05d91d6db951b6d1cc7a28e000386d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#[warn(clippy::cmp_owned)]
#[allow(clippy::unnecessary_operation, clippy::no_effect, unused_must_use, clippy::eq_op)]
fn main() {
    fn with_to_string(x: &str) {
        x != "foo";
        //~^ cmp_owned

        "foo" != x;
        //~^ cmp_owned
    }

    let x = "oh";

    with_to_string(x);

    x != "foo";
    //~^ cmp_owned

    x != "foo";
    //~^ cmp_owned

    42.to_string() == "42";

    Foo == Foo;
    //~^ cmp_owned

    "abc".chars().filter(|c| *c != 'X');
    //~^ cmp_owned

    "abc".chars().filter(|c| *c != 'X');
}

struct Foo;

impl PartialEq for Foo {
    // Allow this here, because it emits the lint
    // without a suggestion. This is tested in
    // `tests/ui/cmp_owned/without_suggestion.rs`
    #[allow(clippy::cmp_owned)]
    fn eq(&self, other: &Self) -> bool {
        self.to_owned() == *other
    }
}

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

#[derive(PartialEq, Eq)]
struct Bar;

impl PartialEq<Foo> for Bar {
    fn eq(&self, _: &Foo) -> bool {
        true
    }
}

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

#[derive(PartialEq, Eq)]
struct Baz;

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

fn issue_8103() {
    let foo1 = String::from("foo");
    let _ = foo1 == "foo";
    //~^ cmp_owned
    let foo2 = "foo";
    let _ = foo1 == foo2;
    //~^ cmp_owned
}