Skip to content

Commit 2a3ae11

Browse files
committed
Move fixable map_unwrap_or cases to rustfixed test
1 parent 3fec6f5 commit 2a3ae11

File tree

5 files changed

+172
-92
lines changed

5 files changed

+172
-92
lines changed

tests/ui/map_unwrap_or.rs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// FIXME: Add "run-rustfix" once it's supported for multipart suggestions
21
// aux-build:option_helpers.rs
32

43
#![warn(clippy::map_unwrap_or)]
@@ -13,10 +12,6 @@ fn option_methods() {
1312
let opt = Some(1);
1413

1514
// Check for `option.map(_).unwrap_or(_)` use.
16-
// Single line case.
17-
let _ = opt.map(|x| x + 1)
18-
// Should lint even though this call is on a separate line.
19-
.unwrap_or(0);
2015
// Multi-line cases.
2116
let _ = opt.map(|x| {
2217
x + 1
@@ -47,10 +42,6 @@ fn option_methods() {
4742
let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
4843

4944
// Check for `option.map(_).unwrap_or_else(_)` use.
50-
// single line case
51-
let _ = opt.map(|x| x + 1)
52-
// Should lint even though this call is on a separate line.
53-
.unwrap_or_else(|| 0);
5445
// Multi-line cases.
5546
let _ = opt.map(|x| {
5647
x + 1
@@ -60,40 +51,8 @@ fn option_methods() {
6051
.unwrap_or_else(||
6152
0
6253
);
63-
// Macro case.
64-
// Should not lint.
65-
let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
66-
67-
// Issue #4144
68-
{
69-
let mut frequencies = HashMap::new();
70-
let word = "foo";
71-
72-
frequencies
73-
.get_mut(word)
74-
.map(|count| {
75-
*count += 1;
76-
})
77-
.unwrap_or_else(|| {
78-
frequencies.insert(word.to_owned(), 1);
79-
});
80-
}
81-
}
82-
83-
fn result_methods() {
84-
let res: Result<i32, ()> = Ok(1);
85-
86-
// Check for `result.map(_).unwrap_or_else(_)` use.
87-
// single line case
88-
let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0); // should lint even though this call is on a separate line
89-
// multi line cases
90-
let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
91-
let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
92-
// macro case
93-
let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|e| 0); // should not lint
9454
}
9555

9656
fn main() {
9757
option_methods();
98-
result_methods();
9958
}

tests/ui/map_unwrap_or.stderr

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
2-
--> $DIR/map_unwrap_or.rs:17:13
3-
|
4-
LL | let _ = opt.map(|x| x + 1)
5-
| _____________^
6-
LL | | // Should lint even though this call is on a separate line.
7-
LL | | .unwrap_or(0);
8-
| |_____________________^
9-
|
10-
= note: `-D clippy::map-unwrap-or` implied by `-D warnings`
11-
help: use `map_or(<a>, <f>)` instead
12-
|
13-
LL | let _ = opt.map_or(0, |x| x + 1);
14-
| ^^^^^^ ^^ --
15-
16-
error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
17-
--> $DIR/map_unwrap_or.rs:21:13
2+
--> $DIR/map_unwrap_or.rs:16:13
183
|
194
LL | let _ = opt.map(|x| {
205
| _____________^
@@ -23,6 +8,7 @@ LL | | }
238
LL | | ).unwrap_or(0);
249
| |__________________^
2510
|
11+
= note: `-D clippy::map-unwrap-or` implied by `-D warnings`
2612
help: use `map_or(<a>, <f>)` instead
2713
|
2814
LL | let _ = opt.map_or(0, |x| {
@@ -32,7 +18,7 @@ LL | );
3218
|
3319

3420
error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
35-
--> $DIR/map_unwrap_or.rs:25:13
21+
--> $DIR/map_unwrap_or.rs:20:13
3622
|
3723
LL | let _ = opt.map(|x| x + 1)
3824
| _____________^
@@ -49,7 +35,7 @@ LL | }, |x| x + 1);
4935
|
5036

5137
error: called `map(<f>).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(<f>)` instead
52-
--> $DIR/map_unwrap_or.rs:30:13
38+
--> $DIR/map_unwrap_or.rs:25:13
5339
|
5440
LL | let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
5541
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -60,7 +46,7 @@ LL | let _ = opt.and_then(|x| Some(x + 1));
6046
| ^^^^^^^^ --
6147

6248
error: called `map(<f>).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(<f>)` instead
63-
--> $DIR/map_unwrap_or.rs:32:13
49+
--> $DIR/map_unwrap_or.rs:27:13
6450
|
6551
LL | let _ = opt.map(|x| {
6652
| _____________^
@@ -78,7 +64,7 @@ LL | );
7864
|
7965

8066
error: called `map(<f>).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(<f>)` instead
81-
--> $DIR/map_unwrap_or.rs:36:13
67+
--> $DIR/map_unwrap_or.rs:31:13
8268
|
8369
LL | let _ = opt
8470
| _____________^
@@ -92,7 +78,7 @@ LL | .and_then(|x| Some(x + 1));
9278
| ^^^^^^^^ --
9379

9480
error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
95-
--> $DIR/map_unwrap_or.rs:47:13
81+
--> $DIR/map_unwrap_or.rs:42:13
9682
|
9783
LL | let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
9884
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -103,16 +89,7 @@ LL | let _ = Some("prefix").map_or(id, |p| format!("{}.", p));
10389
| ^^^^^^ ^^^ --
10490

10591
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
106-
--> $DIR/map_unwrap_or.rs:51:13
107-
|
108-
LL | let _ = opt.map(|x| x + 1)
109-
| _____________^
110-
LL | | // Should lint even though this call is on a separate line.
111-
LL | | .unwrap_or_else(|| 0);
112-
| |_____________________________^ help: try this: `opt.map_or_else(|| 0, |x| x + 1)`
113-
114-
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
115-
--> $DIR/map_unwrap_or.rs:55:13
92+
--> $DIR/map_unwrap_or.rs:46:13
11693
|
11794
LL | let _ = opt.map(|x| {
11895
| _____________^
@@ -122,7 +99,7 @@ LL | | ).unwrap_or_else(|| 0);
12299
| |__________________________^
123100

124101
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
125-
--> $DIR/map_unwrap_or.rs:59:13
102+
--> $DIR/map_unwrap_or.rs:50:13
126103
|
127104
LL | let _ = opt.map(|x| x + 1)
128105
| _____________^
@@ -131,23 +108,5 @@ LL | | 0
131108
LL | | );
132109
| |_________^
133110

134-
error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
135-
--> $DIR/map_unwrap_or.rs:88:13
136-
|
137-
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0); // should lint even though this call is on a separate line
138-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|e| 0, |x| x + 1)`
139-
140-
error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
141-
--> $DIR/map_unwrap_or.rs:90:13
142-
|
143-
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
144-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|e| 0, |x| x + 1)`
145-
146-
error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
147-
--> $DIR/map_unwrap_or.rs:91:13
148-
|
149-
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
150-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|e| 0, |x| x + 1)`
151-
152-
error: aborting due to 13 previous errors
111+
error: aborting due to 8 previous errors
153112

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// run-rustfix
2+
// aux-build:option_helpers.rs
3+
4+
#![warn(clippy::map_unwrap_or)]
5+
6+
#[macro_use]
7+
extern crate option_helpers;
8+
9+
use std::collections::HashMap;
10+
11+
#[rustfmt::skip]
12+
fn option_methods() {
13+
let opt = Some(1);
14+
15+
// Check for `option.map(_).unwrap_or_else(_)` use.
16+
// single line case
17+
let _ = opt.map_or_else(|| 0, |x| x + 1);
18+
19+
// Macro case.
20+
// Should not lint.
21+
let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
22+
23+
// Check for `option.map(_).unwrap_or_else(_)` use.
24+
// single line case
25+
let _ = opt.map_or_else(|| 0, |x| x + 1);
26+
27+
// Issue #4144
28+
{
29+
let mut frequencies = HashMap::new();
30+
let word = "foo";
31+
32+
frequencies
33+
.get_mut(word)
34+
.map(|count| {
35+
*count += 1;
36+
})
37+
.unwrap_or_else(|| {
38+
frequencies.insert(word.to_owned(), 1);
39+
});
40+
}
41+
}
42+
43+
fn result_methods() {
44+
let res: Result<i32, ()> = Ok(1);
45+
46+
// Check for `result.map(_).unwrap_or_else(_)` use.
47+
// single line case
48+
let _ = res.map_or_else(|_e| 0, |x| x + 1); // should lint even though this call is on a separate line
49+
// multi line cases
50+
let _ = res.map_or_else(|_e| 0, |x| x + 1);
51+
let _ = res.map_or_else(|_e| 0, |x| x + 1);
52+
// macro case
53+
let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint
54+
}
55+
56+
fn main() {
57+
option_methods();
58+
result_methods();
59+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// run-rustfix
2+
// aux-build:option_helpers.rs
3+
4+
#![warn(clippy::map_unwrap_or)]
5+
6+
#[macro_use]
7+
extern crate option_helpers;
8+
9+
use std::collections::HashMap;
10+
11+
#[rustfmt::skip]
12+
fn option_methods() {
13+
let opt = Some(1);
14+
15+
// Check for `option.map(_).unwrap_or_else(_)` use.
16+
// single line case
17+
let _ = opt.map(|x| x + 1)
18+
// Should lint even though this call is on a separate line.
19+
.unwrap_or_else(|| 0);
20+
21+
// Macro case.
22+
// Should not lint.
23+
let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
24+
25+
// Check for `option.map(_).unwrap_or_else(_)` use.
26+
// single line case
27+
let _ = opt.map(|x| x + 1)
28+
// Should lint even though this call is on a separate line.
29+
.unwrap_or_else(|| 0);
30+
31+
// Issue #4144
32+
{
33+
let mut frequencies = HashMap::new();
34+
let word = "foo";
35+
36+
frequencies
37+
.get_mut(word)
38+
.map(|count| {
39+
*count += 1;
40+
})
41+
.unwrap_or_else(|| {
42+
frequencies.insert(word.to_owned(), 1);
43+
});
44+
}
45+
}
46+
47+
fn result_methods() {
48+
let res: Result<i32, ()> = Ok(1);
49+
50+
// Check for `result.map(_).unwrap_or_else(_)` use.
51+
// single line case
52+
let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0); // should lint even though this call is on a separate line
53+
// multi line cases
54+
let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0);
55+
let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0);
56+
// macro case
57+
let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint
58+
}
59+
60+
fn main() {
61+
option_methods();
62+
result_methods();
63+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
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
2+
--> $DIR/map_unwrap_or_else_fixable.rs:17:13
3+
|
4+
LL | let _ = opt.map(|x| x + 1)
5+
| _____________^
6+
LL | | // Should lint even though this call is on a separate line.
7+
LL | | .unwrap_or_else(|| 0);
8+
| |_____________________________^ help: try this: `opt.map_or_else(|| 0, |x| x + 1)`
9+
|
10+
= note: `-D clippy::map-unwrap-or` implied by `-D warnings`
11+
12+
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
13+
--> $DIR/map_unwrap_or_else_fixable.rs:27:13
14+
|
15+
LL | let _ = opt.map(|x| x + 1)
16+
| _____________^
17+
LL | | // Should lint even though this call is on a separate line.
18+
LL | | .unwrap_or_else(|| 0);
19+
| |_____________________________^ help: try this: `opt.map_or_else(|| 0, |x| x + 1)`
20+
21+
error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
22+
--> $DIR/map_unwrap_or_else_fixable.rs:52:13
23+
|
24+
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0); // should lint even though this call is on a separate line
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|_e| 0, |x| x + 1)`
26+
27+
error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
28+
--> $DIR/map_unwrap_or_else_fixable.rs:54:13
29+
|
30+
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0);
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|_e| 0, |x| x + 1)`
32+
33+
error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
34+
--> $DIR/map_unwrap_or_else_fixable.rs:55:13
35+
|
36+
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0);
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|_e| 0, |x| x + 1)`
38+
39+
error: aborting due to 5 previous errors
40+

0 commit comments

Comments
 (0)