]> git.lizzy.rs Git - rust.git/blob - tests/ui/cmp_owned.rs
Merge pull request #3291 from JoshMcguigan/cmp_owned-3289
[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
11
12
13
14 #[warn(clippy::cmp_owned)]
15 #[allow(clippy::unnecessary_operation)]
16 fn main() {
17     fn with_to_string(x : &str) {
18         x != "foo".to_string();
19
20         "foo".to_string() != x;
21     }
22
23     let x = "oh";
24
25     with_to_string(x);
26
27     x != "foo".to_owned();
28
29     x != String::from("foo");
30
31     42.to_string() == "42";
32
33     Foo.to_owned() == Foo;
34
35     "abc".chars().filter(|c| c.to_owned() != 'X');
36
37     "abc".chars().filter(|c| *c != 'X');
38
39     let x = &Baz;
40     let y = &Baz;
41
42     y.to_owned() == *x;
43
44     let x = &&Baz;
45     let y = &Baz;
46
47     y.to_owned() == **x;
48 }
49
50 struct Foo;
51
52 impl PartialEq for Foo {
53     fn eq(&self, other: &Self) -> bool {
54         self.to_owned() == *other
55     }
56 }
57
58 impl ToOwned for Foo {
59     type Owned = Bar;
60     fn to_owned(&self) -> Bar {
61         Bar
62     }
63 }
64
65 #[derive(PartialEq)]
66 struct Bar;
67
68 impl PartialEq<Foo> for Bar {
69     fn eq(&self, _: &Foo) -> bool {
70         true
71     }
72 }
73
74 impl std::borrow::Borrow<Foo> for Bar {
75     fn borrow(&self) -> &Foo {
76         static FOO: Foo = Foo;
77         &FOO
78     }
79 }
80
81 #[derive(PartialEq)]
82 struct Baz;
83
84 impl ToOwned for Baz {
85     type Owned = Baz;
86     fn to_owned(&self) -> Baz {
87         Baz
88     }
89 }