Skip to content

Commit 949b347

Browse files
committed
Auto merge of #4444 - phansch:split_up_cmp_owned2, r=flip1995
Split up cmp_owned tests, add run-rustfix Some of the cmp_owned tests emitted non-machine-applicable suggestions, so I moved them to `tests/ui/cmp_owned/without_suggestion.rs` and added `// run-rustfix` to the other half. changelog: none cc #3630
2 parents fea888f + 6d425a6 commit 949b347

File tree

5 files changed

+160
-36
lines changed

5 files changed

+160
-36
lines changed
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// run-rustfix
2+
3+
#[warn(clippy::cmp_owned)]
4+
#[allow(clippy::unnecessary_operation, clippy::no_effect, unused_must_use, clippy::eq_op)]
5+
fn main() {
6+
fn with_to_string(x: &str) {
7+
x != "foo";
8+
9+
"foo" != x;
10+
}
11+
12+
let x = "oh";
13+
14+
with_to_string(x);
15+
16+
x != "foo";
17+
18+
x != "foo";
19+
20+
42.to_string() == "42";
21+
22+
Foo == Foo;
23+
24+
"abc".chars().filter(|c| *c != 'X');
25+
26+
"abc".chars().filter(|c| *c != 'X');
27+
}
28+
29+
struct Foo;
30+
31+
impl 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+
41+
impl ToOwned for Foo {
42+
type Owned = Bar;
43+
fn to_owned(&self) -> Bar {
44+
Bar
45+
}
46+
}
47+
48+
#[derive(PartialEq)]
49+
struct Bar;
50+
51+
impl PartialEq<Foo> for Bar {
52+
fn eq(&self, _: &Foo) -> bool {
53+
true
54+
}
55+
}
56+
57+
impl std::borrow::Borrow<Foo> for Bar {
58+
fn borrow(&self) -> &Foo {
59+
static FOO: Foo = Foo;
60+
&FOO
61+
}
62+
}
63+
64+
#[derive(PartialEq)]
65+
struct Baz;
66+
67+
impl ToOwned for Baz {
68+
type Owned = Baz;
69+
fn to_owned(&self) -> Baz {
70+
Baz
71+
}
72+
}

tests/ui/cmp_owned.rs renamed to tests/ui/cmp_owned/with_suggestion.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
// run-rustfix
2+
13
#[warn(clippy::cmp_owned)]
2-
#[allow(clippy::unnecessary_operation)]
4+
#[allow(clippy::unnecessary_operation, clippy::no_effect, unused_must_use, clippy::eq_op)]
35
fn main() {
46
fn with_to_string(x: &str) {
57
x != "foo".to_string();
@@ -22,21 +24,15 @@ fn main() {
2224
"abc".chars().filter(|c| c.to_owned() != 'X');
2325

2426
"abc".chars().filter(|c| *c != 'X');
25-
26-
let x = &Baz;
27-
let y = &Baz;
28-
29-
y.to_owned() == *x;
30-
31-
let x = &&Baz;
32-
let y = &Baz;
33-
34-
y.to_owned() == **x;
3527
}
3628

3729
struct Foo;
3830

3931
impl 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)]
4036
fn eq(&self, other: &Self) -> bool {
4137
self.to_owned() == *other
4238
}
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,40 @@
11
error: this creates an owned instance just for comparison
2-
--> $DIR/cmp_owned.rs:5:14
2+
--> $DIR/with_suggestion.rs:7:14
33
|
44
LL | x != "foo".to_string();
55
| ^^^^^^^^^^^^^^^^^ help: try: `"foo"`
66
|
77
= note: `-D clippy::cmp-owned` implied by `-D warnings`
88

99
error: this creates an owned instance just for comparison
10-
--> $DIR/cmp_owned.rs:7:9
10+
--> $DIR/with_suggestion.rs:9:9
1111
|
1212
LL | "foo".to_string() != x;
1313
| ^^^^^^^^^^^^^^^^^ help: try: `"foo"`
1414

1515
error: this creates an owned instance just for comparison
16-
--> $DIR/cmp_owned.rs:14:10
16+
--> $DIR/with_suggestion.rs:16:10
1717
|
1818
LL | x != "foo".to_owned();
1919
| ^^^^^^^^^^^^^^^^ help: try: `"foo"`
2020

2121
error: this creates an owned instance just for comparison
22-
--> $DIR/cmp_owned.rs:16:10
22+
--> $DIR/with_suggestion.rs:18:10
2323
|
2424
LL | x != String::from("foo");
2525
| ^^^^^^^^^^^^^^^^^^^ help: try: `"foo"`
2626

2727
error: this creates an owned instance just for comparison
28-
--> $DIR/cmp_owned.rs:20:5
28+
--> $DIR/with_suggestion.rs:22:5
2929
|
3030
LL | Foo.to_owned() == Foo;
3131
| ^^^^^^^^^^^^^^ help: try: `Foo`
3232

3333
error: this creates an owned instance just for comparison
34-
--> $DIR/cmp_owned.rs:22:30
34+
--> $DIR/with_suggestion.rs:24:30
3535
|
3636
LL | "abc".chars().filter(|c| c.to_owned() != 'X');
3737
| ^^^^^^^^^^^^ help: try: `*c`
3838

39-
error: this creates an owned instance just for comparison
40-
--> $DIR/cmp_owned.rs:29:5
41-
|
42-
LL | y.to_owned() == *x;
43-
| ^^^^^^^^^^^^^^^^^^ try implementing the comparison without allocating
44-
45-
error: this creates an owned instance just for comparison
46-
--> $DIR/cmp_owned.rs:34:5
47-
|
48-
LL | y.to_owned() == **x;
49-
| ^^^^^^^^^^^^^^^^^^^ try implementing the comparison without allocating
50-
51-
error: this creates an owned instance just for comparison
52-
--> $DIR/cmp_owned.rs:41:9
53-
|
54-
LL | self.to_owned() == *other
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ try implementing the comparison without allocating
56-
57-
error: aborting due to 9 previous errors
39+
error: aborting due to 6 previous errors
5840

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#[allow(clippy::unnecessary_operation)]
2+
3+
fn main() {
4+
let x = &Baz;
5+
let y = &Baz;
6+
y.to_owned() == *x;
7+
8+
let x = &&Baz;
9+
let y = &Baz;
10+
y.to_owned() == **x;
11+
}
12+
13+
struct Foo;
14+
15+
impl PartialEq for Foo {
16+
fn eq(&self, other: &Self) -> bool {
17+
self.to_owned() == *other
18+
}
19+
}
20+
21+
impl ToOwned for Foo {
22+
type Owned = Bar;
23+
fn to_owned(&self) -> Bar {
24+
Bar
25+
}
26+
}
27+
28+
#[derive(PartialEq)]
29+
struct Baz;
30+
31+
impl ToOwned for Baz {
32+
type Owned = Baz;
33+
fn to_owned(&self) -> Baz {
34+
Baz
35+
}
36+
}
37+
38+
#[derive(PartialEq)]
39+
struct Bar;
40+
41+
impl PartialEq<Foo> for Bar {
42+
fn eq(&self, _: &Foo) -> bool {
43+
true
44+
}
45+
}
46+
47+
impl std::borrow::Borrow<Foo> for Bar {
48+
fn borrow(&self) -> &Foo {
49+
static FOO: Foo = Foo;
50+
&FOO
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: this creates an owned instance just for comparison
2+
--> $DIR/without_suggestion.rs:6:5
3+
|
4+
LL | y.to_owned() == *x;
5+
| ^^^^^^^^^^^^^^^^^^ try implementing the comparison without allocating
6+
|
7+
= note: `-D clippy::cmp-owned` implied by `-D warnings`
8+
9+
error: this creates an owned instance just for comparison
10+
--> $DIR/without_suggestion.rs:10:5
11+
|
12+
LL | y.to_owned() == **x;
13+
| ^^^^^^^^^^^^^^^^^^^ try implementing the comparison without allocating
14+
15+
error: this creates an owned instance just for comparison
16+
--> $DIR/without_suggestion.rs:17:9
17+
|
18+
LL | self.to_owned() == *other
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ try implementing the comparison without allocating
20+
21+
error: aborting due to 3 previous errors
22+

0 commit comments

Comments
 (0)