]> git.lizzy.rs Git - rust.git/blob - tests/ui/cmp_owned.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / cmp_owned.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #[warn(clippy::cmp_owned)]
11 #[allow(clippy::unnecessary_operation)]
12 fn main() {
13     fn with_to_string(x: &str) {
14         x != "foo".to_string();
15
16         "foo".to_string() != x;
17     }
18
19     let x = "oh";
20
21     with_to_string(x);
22
23     x != "foo".to_owned();
24
25     x != String::from("foo");
26
27     42.to_string() == "42";
28
29     Foo.to_owned() == Foo;
30
31     "abc".chars().filter(|c| c.to_owned() != 'X');
32
33     "abc".chars().filter(|c| *c != 'X');
34
35     let x = &Baz;
36     let y = &Baz;
37
38     y.to_owned() == *x;
39
40     let x = &&Baz;
41     let y = &Baz;
42
43     y.to_owned() == **x;
44 }
45
46 struct Foo;
47
48 impl PartialEq for Foo {
49     fn eq(&self, other: &Self) -> bool {
50         self.to_owned() == *other
51     }
52 }
53
54 impl ToOwned for Foo {
55     type Owned = Bar;
56     fn to_owned(&self) -> Bar {
57         Bar
58     }
59 }
60
61 #[derive(PartialEq)]
62 struct Bar;
63
64 impl PartialEq<Foo> for Bar {
65     fn eq(&self, _: &Foo) -> bool {
66         true
67     }
68 }
69
70 impl std::borrow::Borrow<Foo> for Bar {
71     fn borrow(&self) -> &Foo {
72         static FOO: Foo = Foo;
73         &FOO
74     }
75 }
76
77 #[derive(PartialEq)]
78 struct Baz;
79
80 impl ToOwned for Baz {
81     type Owned = Baz;
82     fn to_owned(&self) -> Baz {
83         Baz
84     }
85 }