Skip to content

Commit c0dd1f9

Browse files
committed
Fix tests for map_unwrap_or*
1 parent e2d86b5 commit c0dd1f9

File tree

6 files changed

+95
-66
lines changed

6 files changed

+95
-66
lines changed

tests/ui/map_unwrap_or.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ fn option_methods() {
1212
let opt = Some(1);
1313

1414
// Check for `option.map(_).unwrap_or(_)` use.
15+
// Single line case.
16+
let _ = opt.map(|x| x + 1)
17+
// Should lint even though this call is on a separate line.
18+
.unwrap_or(0);
1519
// Multi-line cases.
1620
let _ = opt.map(|x| {
1721
x + 1
@@ -53,6 +57,25 @@ fn option_methods() {
5357
);
5458
}
5559

60+
#[rustfmt::skip]
61+
fn result_methods() {
62+
let res: Result<i32, ()> = Ok(1);
63+
64+
// Check for `result.map(_).unwrap_or_else(_)` use.
65+
// multi line cases
66+
let _ = res.map(|x| {
67+
x + 1
68+
}
69+
).unwrap_or_else(|_e| 0);
70+
let _ = res.map(|x| x + 1)
71+
.unwrap_or_else(|_e| {
72+
0
73+
});
74+
// macro case
75+
let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint
76+
}
77+
5678
fn main() {
5779
option_methods();
80+
result_methods();
5881
}

tests/ui/map_unwrap_or.stderr

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
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
22
--> $DIR/map_unwrap_or.rs:16:13
33
|
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:20:13
18+
|
419
LL | let _ = opt.map(|x| {
520
| _____________^
621
LL | | x + 1
722
LL | | }
823
LL | | ).unwrap_or(0);
924
| |__________________^
1025
|
11-
= note: `-D clippy::map-unwrap-or` implied by `-D warnings`
1226
help: use `map_or(<a>, <f>)` instead
1327
|
1428
LL | let _ = opt.map_or(0, |x| {
@@ -18,7 +32,7 @@ LL | );
1832
|
1933

2034
error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
21-
--> $DIR/map_unwrap_or.rs:20:13
35+
--> $DIR/map_unwrap_or.rs:24:13
2236
|
2337
LL | let _ = opt.map(|x| x + 1)
2438
| _____________^
@@ -35,7 +49,7 @@ LL | }, |x| x + 1);
3549
|
3650

3751
error: called `map(<f>).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(<f>)` instead
38-
--> $DIR/map_unwrap_or.rs:25:13
52+
--> $DIR/map_unwrap_or.rs:29:13
3953
|
4054
LL | let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
4155
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -46,7 +60,7 @@ LL | let _ = opt.and_then(|x| Some(x + 1));
4660
| ^^^^^^^^ --
4761

4862
error: called `map(<f>).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(<f>)` instead
49-
--> $DIR/map_unwrap_or.rs:27:13
63+
--> $DIR/map_unwrap_or.rs:31:13
5064
|
5165
LL | let _ = opt.map(|x| {
5266
| _____________^
@@ -64,7 +78,7 @@ LL | );
6478
|
6579

6680
error: called `map(<f>).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(<f>)` instead
67-
--> $DIR/map_unwrap_or.rs:31:13
81+
--> $DIR/map_unwrap_or.rs:35:13
6882
|
6983
LL | let _ = opt
7084
| _____________^
@@ -78,7 +92,7 @@ LL | .and_then(|x| Some(x + 1));
7892
| ^^^^^^^^ --
7993

8094
error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
81-
--> $DIR/map_unwrap_or.rs:42:13
95+
--> $DIR/map_unwrap_or.rs:46:13
8296
|
8397
LL | let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
8498
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -89,7 +103,7 @@ LL | let _ = Some("prefix").map_or(id, |p| format!("{}.", p));
89103
| ^^^^^^ ^^^ --
90104

91105
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
92-
--> $DIR/map_unwrap_or.rs:46:13
106+
--> $DIR/map_unwrap_or.rs:50:13
93107
|
94108
LL | let _ = opt.map(|x| {
95109
| _____________^
@@ -99,7 +113,7 @@ LL | | ).unwrap_or_else(|| 0);
99113
| |__________________________^
100114

101115
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
102-
--> $DIR/map_unwrap_or.rs:50:13
116+
--> $DIR/map_unwrap_or.rs:54:13
103117
|
104118
LL | let _ = opt.map(|x| x + 1)
105119
| _____________^
@@ -108,5 +122,25 @@ LL | | 0
108122
LL | | );
109123
| |_________^
110124

111-
error: aborting due to 8 previous errors
125+
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
126+
--> $DIR/map_unwrap_or.rs:66:13
127+
|
128+
LL | let _ = res.map(|x| {
129+
| _____________^
130+
LL | | x + 1
131+
LL | | }
132+
LL | | ).unwrap_or_else(|_e| 0);
133+
| |____________________________^
134+
135+
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
136+
--> $DIR/map_unwrap_or.rs:70:13
137+
|
138+
LL | let _ = res.map(|x| x + 1)
139+
| _____________^
140+
LL | | .unwrap_or_else(|_e| {
141+
LL | | 0
142+
LL | | });
143+
| |__________^
144+
145+
error: aborting due to 11 previous errors
112146

tests/ui/map_unwrap_or_else_fixable.stderr

Lines changed: 0 additions & 40 deletions
This file was deleted.

tests/ui/map_unwrap_or_else_fixable.fixed renamed to tests/ui/map_unwrap_or_fixable.fixed

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ fn option_methods() {
2020
// Should not lint.
2121
let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
2222

23-
// Check for `option.map(_).unwrap_or_else(_)` use.
24-
// single line case
25-
let _ = opt.map_or_else(|| 0, |x| x + 1);
26-
2723
// Issue #4144
2824
{
2925
let mut frequencies = HashMap::new();
@@ -40,15 +36,14 @@ fn option_methods() {
4036
}
4137
}
4238

39+
#[rustfmt::skip]
4340
fn result_methods() {
4441
let res: Result<i32, ()> = Ok(1);
4542

4643
// Check for `result.map(_).unwrap_or_else(_)` use.
4744
// 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);
5145
let _ = res.map_or_else(|_e| 0, |x| x + 1);
46+
5247
// macro case
5348
let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint
5449
}

tests/ui/map_unwrap_or_else_fixable.rs renamed to tests/ui/map_unwrap_or_fixable.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ fn option_methods() {
2222
// Should not lint.
2323
let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
2424

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-
3125
// Issue #4144
3226
{
3327
let mut frequencies = HashMap::new();
@@ -44,15 +38,16 @@ fn option_methods() {
4438
}
4539
}
4640

41+
#[rustfmt::skip]
4742
fn result_methods() {
4843
let res: Result<i32, ()> = Ok(1);
4944

5045
// Check for `result.map(_).unwrap_or_else(_)` use.
5146
// 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);
47+
let _ = res.map(|x| x + 1)
48+
// should lint even though this call is on a separate line
49+
.unwrap_or_else(|_e| 0);
50+
5651
// macro case
5752
let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint
5853
}

tests/ui/map_unwrap_or_fixable.stderr

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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_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 a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
13+
--> $DIR/map_unwrap_or_fixable.rs:47:13
14+
|
15+
LL | let _ = res.map(|x| x + 1)
16+
| _____________^
17+
LL | | // should lint even though this call is on a separate line
18+
LL | | .unwrap_or_else(|_e| 0);
19+
| |_______________________________^ help: try this: `res.map_or_else(|_e| 0, |x| x + 1)`
20+
21+
error: aborting due to 2 previous errors
22+

0 commit comments

Comments
 (0)