Skip to content

Commit e346244

Browse files
committed
test: add more tests
1 parent d91fc14 commit e346244

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

tests/ui/map_unwrap_or_default.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
//@aux-build:option_helpers.rs
12
#![warn(clippy::map_unwrap_or_default)]
23

4+
#[macro_use]
5+
extern crate option_helpers;
6+
37
#[rustfmt::skip]
48
fn option_methods() {
59
let opt = Some(1);
@@ -18,6 +22,10 @@ fn option_methods() {
1822

1923
// won't fix because the return type of the closure is not `bool`
2024
let _ = opt.map(|x| x + 1).unwrap_or_default();
25+
26+
let opt2 = Some('a');
27+
let _ = opt2.is_some_and(char::is_alphanumeric); // should lint
28+
let _ = opt_map!(opt2, |x| x == 'a').unwrap_or_default(); // should not lint
2129
}
2230

2331
#[rustfmt::skip]
@@ -33,6 +41,10 @@ fn result_methods() {
3341

3442
// won't fix because the return type of the closure is not `bool`
3543
let _ = res.map(|x| x + 1).unwrap_or_default();
44+
45+
let res2: Result<char, ()> = Ok('a');
46+
let _ = res2.is_ok_and(char::is_alphanumeric); // should lint
47+
let _ = opt_map!(res2, |x| x == 'a').unwrap_or_default(); // should not lint
3648
}
3749

3850
fn main() {

tests/ui/map_unwrap_or_default.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
//@aux-build:option_helpers.rs
12
#![warn(clippy::map_unwrap_or_default)]
23

4+
#[macro_use]
5+
extern crate option_helpers;
6+
37
#[rustfmt::skip]
48
fn option_methods() {
59
let opt = Some(1);
@@ -21,6 +25,10 @@ fn option_methods() {
2125

2226
// won't fix because the return type of the closure is not `bool`
2327
let _ = opt.map(|x| x + 1).unwrap_or_default();
28+
29+
let opt2 = Some('a');
30+
let _ = opt2.map(char::is_alphanumeric).unwrap_or_default(); // should lint
31+
let _ = opt_map!(opt2, |x| x == 'a').unwrap_or_default(); // should not lint
2432
}
2533

2634
#[rustfmt::skip]
@@ -37,6 +45,10 @@ fn result_methods() {
3745

3846
// won't fix because the return type of the closure is not `bool`
3947
let _ = res.map(|x| x + 1).unwrap_or_default();
48+
49+
let res2: Result<char, ()> = Ok('a');
50+
let _ = res2.map(char::is_alphanumeric).unwrap_or_default(); // should lint
51+
let _ = opt_map!(res2, |x| x == 'a').unwrap_or_default(); // should not lint
4052
}
4153

4254
fn main() {

tests/ui/map_unwrap_or_default.stderr

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: called `map(<f>).unwrap_or_default()` on an `Option` value
2-
--> $DIR/map_unwrap_or_default.rs:9:17
2+
--> $DIR/map_unwrap_or_default.rs:13:17
33
|
44
LL | let _ = opt.map(|x| x > 1)
55
| _________________^
@@ -16,7 +16,7 @@ LL + let _ = opt.is_some_and(|x| x > 1);
1616
|
1717

1818
error: called `map(<f>).unwrap_or_default()` on an `Option` value
19-
--> $DIR/map_unwrap_or_default.rs:13:17
19+
--> $DIR/map_unwrap_or_default.rs:17:17
2020
|
2121
LL | let _ = opt.map(|x| {
2222
| _________________^
@@ -34,7 +34,7 @@ LL ~ );
3434
|
3535

3636
error: called `map(<f>).unwrap_or_default()` on an `Option` value
37-
--> $DIR/map_unwrap_or_default.rs:17:17
37+
--> $DIR/map_unwrap_or_default.rs:21:17
3838
|
3939
LL | let _ = opt.map(|x| x > 1).unwrap_or_default();
4040
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -46,7 +46,7 @@ LL + let _ = opt.is_some_and(|x| x > 1);
4646
|
4747

4848
error: called `map(<f>).unwrap_or_default()` on an `Option` value
49-
--> $DIR/map_unwrap_or_default.rs:19:10
49+
--> $DIR/map_unwrap_or_default.rs:23:10
5050
|
5151
LL | .map(|x| x > 1)
5252
| __________^
@@ -59,8 +59,20 @@ LL - .map(|x| x > 1)
5959
LL + .is_some_and(|x| x > 1);
6060
|
6161

62+
error: called `map(<f>).unwrap_or_default()` on an `Option` value
63+
--> $DIR/map_unwrap_or_default.rs:30:18
64+
|
65+
LL | let _ = opt2.map(char::is_alphanumeric).unwrap_or_default(); // should lint
66+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
67+
|
68+
help: use is_some_and instead
69+
|
70+
LL - let _ = opt2.map(char::is_alphanumeric).unwrap_or_default(); // should lint
71+
LL + let _ = opt2.is_some_and(char::is_alphanumeric); // should lint
72+
|
73+
6274
error: called `map(<f>).unwrap_or_default()` on a `Result` value
63-
--> $DIR/map_unwrap_or_default.rs:31:17
75+
--> $DIR/map_unwrap_or_default.rs:39:17
6476
|
6577
LL | let _ = res.map(|x| {
6678
| _________________^
@@ -78,7 +90,7 @@ LL ~ );
7890
|
7991

8092
error: called `map(<f>).unwrap_or_default()` on a `Result` value
81-
--> $DIR/map_unwrap_or_default.rs:35:17
93+
--> $DIR/map_unwrap_or_default.rs:43:17
8294
|
8395
LL | let _ = res.map(|x| x > 1)
8496
| _________________^
@@ -91,5 +103,17 @@ LL - let _ = res.map(|x| x > 1)
91103
LL + let _ = res.is_ok_and(|x| x > 1);
92104
|
93105

94-
error: aborting due to 6 previous errors
106+
error: called `map(<f>).unwrap_or_default()` on a `Result` value
107+
--> $DIR/map_unwrap_or_default.rs:50:18
108+
|
109+
LL | let _ = res2.map(char::is_alphanumeric).unwrap_or_default(); // should lint
110+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
111+
|
112+
help: use is_ok_and instead
113+
|
114+
LL - let _ = res2.map(char::is_alphanumeric).unwrap_or_default(); // should lint
115+
LL + let _ = res2.is_ok_and(char::is_alphanumeric); // should lint
116+
|
117+
118+
error: aborting due to 8 previous errors
95119

0 commit comments

Comments
 (0)