Skip to content

Commit b4176e3

Browse files
author
Guanqun Lu
committed
add unit test for result_map_unwrap_or
1 parent b373248 commit b4176e3

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

tests/ui/result_map_unwrap_or.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![warn(clippy::result_map_unwrap_or)]
2+
3+
fn main() {
4+
let res: Result<usize, ()> = Ok(1);
5+
6+
// Check `RESULT_MAP_OR_NONE`.
7+
// Single line case.
8+
let _ = res.map(|x| x + 1).unwrap_or(0);
9+
// Multi-line case.
10+
#[rustfmt::skip]
11+
let _ = res.map(|x| {
12+
x + 1
13+
}).unwrap_or(0);
14+
}

tests/ui/result_map_unwrap_or.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: called `map(f).unwrap_or(a)` on a Result value. This can be done more directly by calling `map_or(a, f)` instead
2+
--> $DIR/result_map_unwrap_or.rs:8:13
3+
|
4+
LL | let _ = res.map(|x| x + 1).unwrap_or(0);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::result-map-unwrap-or` implied by `-D warnings`
8+
= note: replace `map(|x| x + 1).unwrap_or(0)` with `map_or(0, |x| x + 1)`
9+
10+
error: called `map(f).unwrap_or(a)` on a Result value. This can be done more directly by calling `map_or(a, f)` instead
11+
--> $DIR/result_map_unwrap_or.rs:11:13
12+
|
13+
LL | let _ = res.map(|x| {
14+
| _____________^
15+
LL | | x + 1
16+
LL | | }).unwrap_or(0);
17+
| |______________________________________^
18+
19+
error: aborting due to 2 previous errors
20+

0 commit comments

Comments
 (0)