Skip to content

Commit f2da0c7

Browse files
committed
manual-unwrap-or / pr remarks
1 parent 915ce36 commit f2da0c7

File tree

3 files changed

+77
-35
lines changed

3 files changed

+77
-35
lines changed

tests/ui/manual_unwrap_or.fixed

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22
#![allow(dead_code)]
3+
#![allow(unused_variables)]
34

45
fn option_unwrap_or() {
56
// int case
@@ -67,44 +68,58 @@ fn option_unwrap_or() {
6768

6869
fn result_unwrap_or() {
6970
// int case
71+
Ok::<i32, &str>(1).unwrap_or(42);
72+
73+
// int case, suggestion must surround with parenthesis
7074
(Ok(1) as Result<i32, &str>).unwrap_or(42);
7175

7276
// int case reversed
73-
(Ok(1) as Result<i32, &str>).unwrap_or(42);
77+
Ok::<i32, &str>(1).unwrap_or(42);
7478

7579
// richer none expr
76-
(Ok(1) as Result<i32, &str>).unwrap_or(1 + 42);
80+
Ok::<i32, &str>(1).unwrap_or(1 + 42);
7781

7882
// multiline case
7983
#[rustfmt::skip]
80-
(Ok(1) as Result<i32, &str>).unwrap_or({
84+
Ok::<i32, &str>(1).unwrap_or({
8185
42 + 42
8286
+ 42 + 42 + 42
8387
+ 42 + 42 + 42
8488
});
8589

8690
// string case
87-
(Ok("Bob") as Result<&str, &str>).unwrap_or("Alice");
91+
Ok::<&str, &str>("Bob").unwrap_or("Alice");
8892

8993
// don't lint
90-
match Ok(1) as Result<i32, &str> {
94+
match Ok::<i32, &str>(1) {
9195
Ok(i) => i + 2,
9296
Err(_) => 42,
9397
};
94-
match Ok(1) as Result<i32, &str> {
98+
match Ok::<i32, &str>(1) {
9599
Ok(i) => i,
96100
Err(_) => return,
97101
};
98102
for j in 0..4 {
99-
match Ok(j) as Result<i32, &str> {
103+
match Ok::<i32, &str>(j) {
100104
Ok(i) => i,
101105
Err(_) => continue,
102106
};
103-
match Ok(j) as Result<i32, &str> {
107+
match Ok::<i32, &str>(j) {
104108
Ok(i) => i,
105109
Err(_) => break,
106110
};
107111
}
112+
113+
// don't lint, Err value is used
114+
match Ok::<&str, &str>("Alice") {
115+
Ok(s) => s,
116+
Err(s) => s,
117+
};
118+
// could lint, but unused_variables takes care of it
119+
match Ok::<&str, &str>("Alice") {
120+
Ok(s) => s,
121+
Err(s) => "Bob",
122+
};
108123
}
109124

110125
fn main() {}

tests/ui/manual_unwrap_or.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22
#![allow(dead_code)]
3+
#![allow(unused_variables)]
34

45
fn option_unwrap_or() {
56
// int case
@@ -82,26 +83,32 @@ fn option_unwrap_or() {
8283

8384
fn result_unwrap_or() {
8485
// int case
86+
match Ok::<i32, &str>(1) {
87+
Ok(i) => i,
88+
Err(_) => 42,
89+
};
90+
91+
// int case, suggestion must surround with parenthesis
8592
match Ok(1) as Result<i32, &str> {
8693
Ok(i) => i,
8794
Err(_) => 42,
8895
};
8996

9097
// int case reversed
91-
match Ok(1) as Result<i32, &str> {
98+
match Ok::<i32, &str>(1) {
9299
Err(_) => 42,
93100
Ok(i) => i,
94101
};
95102

96103
// richer none expr
97-
match Ok(1) as Result<i32, &str> {
104+
match Ok::<i32, &str>(1) {
98105
Ok(i) => i,
99106
Err(_) => 1 + 42,
100107
};
101108

102109
// multiline case
103110
#[rustfmt::skip]
104-
match Ok(1) as Result<i32, &str> {
111+
match Ok::<i32, &str>(1) {
105112
Ok(i) => i,
106113
Err(_) => {
107114
42 + 42
@@ -111,30 +118,41 @@ fn result_unwrap_or() {
111118
};
112119

113120
// string case
114-
match Ok("Bob") as Result<&str, &str> {
121+
match Ok::<&str, &str>("Bob") {
115122
Ok(i) => i,
116123
Err(_) => "Alice",
117124
};
118125

119126
// don't lint
120-
match Ok(1) as Result<i32, &str> {
127+
match Ok::<i32, &str>(1) {
121128
Ok(i) => i + 2,
122129
Err(_) => 42,
123130
};
124-
match Ok(1) as Result<i32, &str> {
131+
match Ok::<i32, &str>(1) {
125132
Ok(i) => i,
126133
Err(_) => return,
127134
};
128135
for j in 0..4 {
129-
match Ok(j) as Result<i32, &str> {
136+
match Ok::<i32, &str>(j) {
130137
Ok(i) => i,
131138
Err(_) => continue,
132139
};
133-
match Ok(j) as Result<i32, &str> {
140+
match Ok::<i32, &str>(j) {
134141
Ok(i) => i,
135142
Err(_) => break,
136143
};
137144
}
145+
146+
// don't lint, Err value is used
147+
match Ok::<&str, &str>("Alice") {
148+
Ok(s) => s,
149+
Err(s) => s,
150+
};
151+
// could lint, but unused_variables takes care of it
152+
match Ok::<&str, &str>("Alice") {
153+
Ok(s) => s,
154+
Err(s) => "Bob",
155+
};
138156
}
139157

140158
fn main() {}

tests/ui/manual_unwrap_or.stderr

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this pattern reimplements `Option::unwrap_or`
2-
--> $DIR/manual_unwrap_or.rs:6:5
2+
--> $DIR/manual_unwrap_or.rs:7:5
33
|
44
LL | / match Some(1) {
55
LL | | Some(i) => i,
@@ -10,7 +10,7 @@ LL | | };
1010
= note: `-D clippy::manual-unwrap-or` implied by `-D warnings`
1111

1212
error: this pattern reimplements `Option::unwrap_or`
13-
--> $DIR/manual_unwrap_or.rs:12:5
13+
--> $DIR/manual_unwrap_or.rs:13:5
1414
|
1515
LL | / match Some(1) {
1616
LL | | None => 42,
@@ -19,7 +19,7 @@ LL | | };
1919
| |_____^ help: replace with: `Some(1).unwrap_or(42)`
2020

2121
error: this pattern reimplements `Option::unwrap_or`
22-
--> $DIR/manual_unwrap_or.rs:18:5
22+
--> $DIR/manual_unwrap_or.rs:19:5
2323
|
2424
LL | / match Some(1) {
2525
LL | | Some(i) => i,
@@ -28,7 +28,7 @@ LL | | };
2828
| |_____^ help: replace with: `Some(1).unwrap_or(1 + 42)`
2929

3030
error: this pattern reimplements `Option::unwrap_or`
31-
--> $DIR/manual_unwrap_or.rs:25:5
31+
--> $DIR/manual_unwrap_or.rs:26:5
3232
|
3333
LL | / match Some(1) {
3434
LL | | Some(i) => i,
@@ -49,7 +49,7 @@ LL | });
4949
|
5050

5151
error: this pattern reimplements `Option::unwrap_or`
52-
--> $DIR/manual_unwrap_or.rs:35:5
52+
--> $DIR/manual_unwrap_or.rs:36:5
5353
|
5454
LL | / match Some("Bob") {
5555
LL | | Some(i) => i,
@@ -58,7 +58,16 @@ LL | | };
5858
| |_____^ help: replace with: `Some("Bob").unwrap_or("Alice")`
5959

6060
error: this pattern reimplements `Result::unwrap_or`
61-
--> $DIR/manual_unwrap_or.rs:85:5
61+
--> $DIR/manual_unwrap_or.rs:86:5
62+
|
63+
LL | / match Ok::<i32, &str>(1) {
64+
LL | | Ok(i) => i,
65+
LL | | Err(_) => 42,
66+
LL | | };
67+
| |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(42)`
68+
69+
error: this pattern reimplements `Result::unwrap_or`
70+
--> $DIR/manual_unwrap_or.rs:92:5
6271
|
6372
LL | / match Ok(1) as Result<i32, &str> {
6473
LL | | Ok(i) => i,
@@ -67,27 +76,27 @@ LL | | };
6776
| |_____^ help: replace with: `(Ok(1) as Result<i32, &str>).unwrap_or(42)`
6877

6978
error: this pattern reimplements `Result::unwrap_or`
70-
--> $DIR/manual_unwrap_or.rs:91:5
79+
--> $DIR/manual_unwrap_or.rs:98:5
7180
|
72-
LL | / match Ok(1) as Result<i32, &str> {
81+
LL | / match Ok::<i32, &str>(1) {
7382
LL | | Err(_) => 42,
7483
LL | | Ok(i) => i,
7584
LL | | };
76-
| |_____^ help: replace with: `(Ok(1) as Result<i32, &str>).unwrap_or(42)`
85+
| |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(42)`
7786

7887
error: this pattern reimplements `Result::unwrap_or`
79-
--> $DIR/manual_unwrap_or.rs:97:5
88+
--> $DIR/manual_unwrap_or.rs:104:5
8089
|
81-
LL | / match Ok(1) as Result<i32, &str> {
90+
LL | / match Ok::<i32, &str>(1) {
8291
LL | | Ok(i) => i,
8392
LL | | Err(_) => 1 + 42,
8493
LL | | };
85-
| |_____^ help: replace with: `(Ok(1) as Result<i32, &str>).unwrap_or(1 + 42)`
94+
| |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(1 + 42)`
8695

8796
error: this pattern reimplements `Result::unwrap_or`
88-
--> $DIR/manual_unwrap_or.rs:104:5
97+
--> $DIR/manual_unwrap_or.rs:111:5
8998
|
90-
LL | / match Ok(1) as Result<i32, &str> {
99+
LL | / match Ok::<i32, &str>(1) {
91100
LL | | Ok(i) => i,
92101
LL | | Err(_) => {
93102
LL | | 42 + 42
@@ -98,21 +107,21 @@ LL | | };
98107
|
99108
help: replace with
100109
|
101-
LL | (Ok(1) as Result<i32, &str>).unwrap_or({
110+
LL | Ok::<i32, &str>(1).unwrap_or({
102111
LL | 42 + 42
103112
LL | + 42 + 42 + 42
104113
LL | + 42 + 42 + 42
105114
LL | });
106115
|
107116

108117
error: this pattern reimplements `Result::unwrap_or`
109-
--> $DIR/manual_unwrap_or.rs:114:5
118+
--> $DIR/manual_unwrap_or.rs:121:5
110119
|
111-
LL | / match Ok("Bob") as Result<&str, &str> {
120+
LL | / match Ok::<&str, &str>("Bob") {
112121
LL | | Ok(i) => i,
113122
LL | | Err(_) => "Alice",
114123
LL | | };
115-
| |_____^ help: replace with: `(Ok("Bob") as Result<&str, &str>).unwrap_or("Alice")`
124+
| |_____^ help: replace with: `Ok::<&str, &str>("Bob").unwrap_or("Alice")`
116125

117-
error: aborting due to 10 previous errors
126+
error: aborting due to 11 previous errors
118127

0 commit comments

Comments
 (0)