]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/cmp_owned/with_suggestion.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / cmp_owned / with_suggestion.rs
CommitLineData
49aad941 1//@run-rustfix
f20569fa
XL
2
3#[warn(clippy::cmp_owned)]
4#[allow(clippy::unnecessary_operation, clippy::no_effect, unused_must_use, clippy::eq_op)]
5fn main() {
6 fn with_to_string(x: &str) {
7 x != "foo".to_string();
8
9 "foo".to_string() != x;
10 }
11
12 let x = "oh";
13
14 with_to_string(x);
15
16 x != "foo".to_owned();
17
18 x != String::from("foo");
19
20 42.to_string() == "42";
21
22 Foo.to_owned() == Foo;
23
24 "abc".chars().filter(|c| c.to_owned() != 'X');
25
26 "abc".chars().filter(|c| *c != 'X');
27}
28
29struct Foo;
30
31impl PartialEq for Foo {
32 // Allow this here, because it emits the lint
33 // without a suggestion. This is tested in
34 // `tests/ui/cmp_owned/without_suggestion.rs`
35 #[allow(clippy::cmp_owned)]
36 fn eq(&self, other: &Self) -> bool {
37 self.to_owned() == *other
38 }
39}
40
41impl ToOwned for Foo {
42 type Owned = Bar;
43 fn to_owned(&self) -> Bar {
44 Bar
45 }
46}
47
923072b8 48#[derive(PartialEq, Eq)]
f20569fa
XL
49struct Bar;
50
51impl PartialEq<Foo> for Bar {
52 fn eq(&self, _: &Foo) -> bool {
53 true
54 }
55}
56
57impl std::borrow::Borrow<Foo> for Bar {
58 fn borrow(&self) -> &Foo {
59 static FOO: Foo = Foo;
60 &FOO
61 }
62}
63
923072b8 64#[derive(PartialEq, Eq)]
f20569fa
XL
65struct Baz;
66
67impl ToOwned for Baz {
68 type Owned = Baz;
69 fn to_owned(&self) -> Baz {
70 Baz
71 }
72}