Skip to content

Commit 95e537b

Browse files
committed
Auto merge of #3987 - phansch:rustfix_option_map_or_none, r=flip1995
Add run-rustfix for option_map_or_none lint * Extracts `option_map_or_none` tests into separate file * Adds `// run-rustfix` to `tests/ui/option_map_or_none.rs` cc #3630
2 parents b834dbb + 0f69aac commit 95e537b

File tree

5 files changed

+73
-54
lines changed

5 files changed

+73
-54
lines changed

tests/ui/methods.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ impl Mul<T> for T {
148148
/// Checks implementation of the following lints:
149149
/// * `OPTION_MAP_UNWRAP_OR`
150150
/// * `OPTION_MAP_UNWRAP_OR_ELSE`
151-
/// * `OPTION_MAP_OR_NONE`
152151
#[rustfmt::skip]
153152
fn option_methods() {
154153
let opt = Some(1);
@@ -204,15 +203,6 @@ fn option_methods() {
204203
// Macro case.
205204
// Should not lint.
206205
let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
207-
208-
// Check `OPTION_MAP_OR_NONE`.
209-
// Single line case.
210-
let _ = opt.map_or(None, |x| Some(x + 1));
211-
// Multi-line case.
212-
let _ = opt.map_or(None, |x| {
213-
Some(x + 1)
214-
}
215-
);
216206
}
217207

218208
/// Checks implementation of `FILTER_NEXT` lint.

tests/ui/methods.stderr

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LL | fn new(self) -> Self {
2929
| ^^^^
3030

3131
error: called `map(f).unwrap_or(a)` on an Option value. This can be done more directly by calling `map_or(a, f)` instead
32-
--> $DIR/methods.rs:158:13
32+
--> $DIR/methods.rs:157:13
3333
|
3434
LL | let _ = opt.map(|x| x + 1)
3535
| _____________^
@@ -41,7 +41,7 @@ LL | | .unwrap_or(0);
4141
= note: replace `map(|x| x + 1).unwrap_or(0)` with `map_or(0, |x| x + 1)`
4242

4343
error: called `map(f).unwrap_or(a)` on an Option value. This can be done more directly by calling `map_or(a, f)` instead
44-
--> $DIR/methods.rs:162:13
44+
--> $DIR/methods.rs:161:13
4545
|
4646
LL | let _ = opt.map(|x| {
4747
| _____________^
@@ -51,7 +51,7 @@ LL | | ).unwrap_or(0);
5151
| |____________________________^
5252

5353
error: called `map(f).unwrap_or(a)` on an Option value. This can be done more directly by calling `map_or(a, f)` instead
54-
--> $DIR/methods.rs:166:13
54+
--> $DIR/methods.rs:165:13
5555
|
5656
LL | let _ = opt.map(|x| x + 1)
5757
| _____________^
@@ -61,15 +61,15 @@ LL | | });
6161
| |__________________^
6262

6363
error: called `map(f).unwrap_or(None)` on an Option value. This can be done more directly by calling `and_then(f)` instead
64-
--> $DIR/methods.rs:171:13
64+
--> $DIR/methods.rs:170:13
6565
|
6666
LL | let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6868
|
6969
= note: replace `map(|x| Some(x + 1)).unwrap_or(None)` with `and_then(|x| Some(x + 1))`
7070

7171
error: called `map(f).unwrap_or(None)` on an Option value. This can be done more directly by calling `and_then(f)` instead
72-
--> $DIR/methods.rs:173:13
72+
--> $DIR/methods.rs:172:13
7373
|
7474
LL | let _ = opt.map(|x| {
7575
| _____________^
@@ -79,7 +79,7 @@ LL | | ).unwrap_or(None);
7979
| |_____________________^
8080

8181
error: called `map(f).unwrap_or(None)` on an Option value. This can be done more directly by calling `and_then(f)` instead
82-
--> $DIR/methods.rs:177:13
82+
--> $DIR/methods.rs:176:13
8383
|
8484
LL | let _ = opt
8585
| _____________^
@@ -90,15 +90,15 @@ LL | | .unwrap_or(None);
9090
= note: replace `map(|x| Some(x + 1)).unwrap_or(None)` with `and_then(|x| Some(x + 1))`
9191

9292
error: called `map(f).unwrap_or(a)` on an Option value. This can be done more directly by calling `map_or(a, f)` instead
93-
--> $DIR/methods.rs:188:13
93+
--> $DIR/methods.rs:187:13
9494
|
9595
LL | let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
9696
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9797
|
9898
= note: replace `map(|p| format!("{}.", p)).unwrap_or(id)` with `map_or(id, |p| format!("{}.", p))`
9999

100100
error: called `map(f).unwrap_or_else(g)` on an Option value. This can be done more directly by calling `map_or_else(g, f)` instead
101-
--> $DIR/methods.rs:192:13
101+
--> $DIR/methods.rs:191:13
102102
|
103103
LL | let _ = opt.map(|x| x + 1)
104104
| _____________^
@@ -110,7 +110,7 @@ LL | | .unwrap_or_else(|| 0);
110110
= note: replace `map(|x| x + 1).unwrap_or_else(|| 0)` with `map_or_else(|| 0, |x| x + 1)`
111111

112112
error: called `map(f).unwrap_or_else(g)` on an Option value. This can be done more directly by calling `map_or_else(g, f)` instead
113-
--> $DIR/methods.rs:196:13
113+
--> $DIR/methods.rs:195:13
114114
|
115115
LL | let _ = opt.map(|x| {
116116
| _____________^
@@ -120,7 +120,7 @@ LL | | ).unwrap_or_else(|| 0);
120120
| |____________________________________^
121121

122122
error: called `map(f).unwrap_or_else(g)` on an Option value. This can be done more directly by calling `map_or_else(g, f)` instead
123-
--> $DIR/methods.rs:200:13
123+
--> $DIR/methods.rs:199:13
124124
|
125125
LL | let _ = opt.map(|x| x + 1)
126126
| _____________^
@@ -129,32 +129,8 @@ LL | | 0
129129
LL | | );
130130
| |_________________^
131131

132-
error: called `map_or(None, f)` on an Option value. This can be done more directly by calling `and_then(f)` instead
133-
--> $DIR/methods.rs:210:13
134-
|
135-
LL | let _ = opt.map_or(None, |x| Some(x + 1));
136-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using and_then instead: `opt.and_then(|x| Some(x + 1))`
137-
|
138-
= note: `-D clippy::option-map-or-none` implied by `-D warnings`
139-
140-
error: called `map_or(None, f)` on an Option value. This can be done more directly by calling `and_then(f)` instead
141-
--> $DIR/methods.rs:212:13
142-
|
143-
LL | let _ = opt.map_or(None, |x| {
144-
| _____________^
145-
LL | | Some(x + 1)
146-
LL | | }
147-
LL | | );
148-
| |_________________^
149-
help: try using and_then instead
150-
|
151-
LL | let _ = opt.and_then(|x| {
152-
LL | Some(x + 1)
153-
LL | });
154-
|
155-
156132
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
157-
--> $DIR/methods.rs:224:13
133+
--> $DIR/methods.rs:214:13
158134
|
159135
LL | let _ = v.iter().filter(|&x| *x < 0).next();
160136
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -163,7 +139,7 @@ LL | let _ = v.iter().filter(|&x| *x < 0).next();
163139
= note: replace `filter(|&x| *x < 0).next()` with `find(|&x| *x < 0)`
164140

165141
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
166-
--> $DIR/methods.rs:227:13
142+
--> $DIR/methods.rs:217:13
167143
|
168144
LL | let _ = v.iter().filter(|&x| {
169145
| _____________^
@@ -173,7 +149,7 @@ LL | | ).next();
173149
| |___________________________^
174150

175151
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
176-
--> $DIR/methods.rs:243:13
152+
--> $DIR/methods.rs:233:13
177153
|
178154
LL | let _ = v.iter().find(|&x| *x < 0).is_some();
179155
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -182,7 +158,7 @@ LL | let _ = v.iter().find(|&x| *x < 0).is_some();
182158
= note: replace `find(|&x| *x < 0).is_some()` with `any(|&x| *x < 0)`
183159

184160
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
185-
--> $DIR/methods.rs:246:13
161+
--> $DIR/methods.rs:236:13
186162
|
187163
LL | let _ = v.iter().find(|&x| {
188164
| _____________^
@@ -192,15 +168,15 @@ LL | | ).is_some();
192168
| |______________________________^
193169

194170
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
195-
--> $DIR/methods.rs:252:13
171+
--> $DIR/methods.rs:242:13
196172
|
197173
LL | let _ = v.iter().position(|&x| x < 0).is_some();
198174
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
199175
|
200176
= note: replace `position(|&x| x < 0).is_some()` with `any(|&x| x < 0)`
201177

202178
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
203-
--> $DIR/methods.rs:255:13
179+
--> $DIR/methods.rs:245:13
204180
|
205181
LL | let _ = v.iter().position(|&x| {
206182
| _____________^
@@ -210,15 +186,15 @@ LL | | ).is_some();
210186
| |______________________________^
211187

212188
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
213-
--> $DIR/methods.rs:261:13
189+
--> $DIR/methods.rs:251:13
214190
|
215191
LL | let _ = v.iter().rposition(|&x| x < 0).is_some();
216192
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
217193
|
218194
= note: replace `rposition(|&x| x < 0).is_some()` with `any(|&x| x < 0)`
219195

220196
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
221-
--> $DIR/methods.rs:264:13
197+
--> $DIR/methods.rs:254:13
222198
|
223199
LL | let _ = v.iter().rposition(|&x| {
224200
| _____________^
@@ -228,12 +204,12 @@ LL | | ).is_some();
228204
| |______________________________^
229205

230206
error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message
231-
--> $DIR/methods.rs:279:13
207+
--> $DIR/methods.rs:269:13
232208
|
233209
LL | let _ = opt.unwrap();
234210
| ^^^^^^^^^^^^
235211
|
236212
= note: `-D clippy::option-unwrap-used` implied by `-D warnings`
237213

238-
error: aborting due to 25 previous errors
214+
error: aborting due to 23 previous errors
239215

tests/ui/option_map_or_none.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
let opt = Some(1);
5+
6+
// Check `OPTION_MAP_OR_NONE`.
7+
// Single line case.
8+
let _ = opt.and_then(|x| Some(x + 1));
9+
// Multi-line case.
10+
#[rustfmt::skip]
11+
let _ = opt.and_then(|x| {
12+
Some(x + 1)
13+
});
14+
}

tests/ui/option_map_or_none.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
let opt = Some(1);
5+
6+
// Check `OPTION_MAP_OR_NONE`.
7+
// Single line case.
8+
let _ = opt.map_or(None, |x| Some(x + 1));
9+
// Multi-line case.
10+
#[rustfmt::skip]
11+
let _ = opt.map_or(None, |x| {
12+
Some(x + 1)
13+
});
14+
}

tests/ui/option_map_or_none.stderr

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: called `map_or(None, f)` on an Option value. This can be done more directly by calling `and_then(f)` instead
2+
--> $DIR/option_map_or_none.rs:8:13
3+
|
4+
LL | let _ = opt.map_or(None, |x| Some(x + 1));
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using and_then instead: `opt.and_then(|x| Some(x + 1))`
6+
|
7+
= note: `-D clippy::option-map-or-none` implied by `-D warnings`
8+
9+
error: called `map_or(None, f)` on an Option value. This can be done more directly by calling `and_then(f)` instead
10+
--> $DIR/option_map_or_none.rs:11:13
11+
|
12+
LL | let _ = opt.map_or(None, |x| {
13+
| _____________^
14+
LL | | Some(x + 1)
15+
LL | | });
16+
| |_________________________^
17+
help: try using and_then instead
18+
|
19+
LL | let _ = opt.and_then(|x| {
20+
LL | Some(x + 1)
21+
LL | });
22+
|
23+
24+
error: aborting due to 2 previous errors
25+

0 commit comments

Comments
 (0)