Fix tests for map_unwrap_or*

This commit is contained in:
ThibsG 2020-10-26 11:02:01 +01:00
parent e2d86b5b80
commit c0dd1f9f76
6 changed files with 95 additions and 66 deletions

View file

@ -12,6 +12,10 @@ fn option_methods() {
let opt = Some(1); let opt = Some(1);
// Check for `option.map(_).unwrap_or(_)` use. // Check for `option.map(_).unwrap_or(_)` use.
// Single line case.
let _ = opt.map(|x| x + 1)
// Should lint even though this call is on a separate line.
.unwrap_or(0);
// Multi-line cases. // Multi-line cases.
let _ = opt.map(|x| { let _ = opt.map(|x| {
x + 1 x + 1
@ -53,6 +57,25 @@ fn option_methods() {
); );
} }
#[rustfmt::skip]
fn result_methods() {
let res: Result<i32, ()> = Ok(1);
// Check for `result.map(_).unwrap_or_else(_)` use.
// multi line cases
let _ = res.map(|x| {
x + 1
}
).unwrap_or_else(|_e| 0);
let _ = res.map(|x| x + 1)
.unwrap_or_else(|_e| {
0
});
// macro case
let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint
}
fn main() { fn main() {
option_methods(); option_methods();
result_methods();
} }

View file

@ -1,6 +1,21 @@
error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
--> $DIR/map_unwrap_or.rs:16:13 --> $DIR/map_unwrap_or.rs:16:13
| |
LL | let _ = opt.map(|x| x + 1)
| _____________^
LL | | // Should lint even though this call is on a separate line.
LL | | .unwrap_or(0);
| |_____________________^
|
= note: `-D clippy::map-unwrap-or` implied by `-D warnings`
help: use `map_or(<a>, <f>)` instead
|
LL | let _ = opt.map_or(0, |x| x + 1);
| ^^^^^^ ^^ --
error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
--> $DIR/map_unwrap_or.rs:20:13
|
LL | let _ = opt.map(|x| { LL | let _ = opt.map(|x| {
| _____________^ | _____________^
LL | | x + 1 LL | | x + 1
@ -8,7 +23,6 @@ LL | | }
LL | | ).unwrap_or(0); LL | | ).unwrap_or(0);
| |__________________^ | |__________________^
| |
= note: `-D clippy::map-unwrap-or` implied by `-D warnings`
help: use `map_or(<a>, <f>)` instead help: use `map_or(<a>, <f>)` instead
| |
LL | let _ = opt.map_or(0, |x| { LL | let _ = opt.map_or(0, |x| {
@ -18,7 +32,7 @@ LL | );
| |
error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
--> $DIR/map_unwrap_or.rs:20:13 --> $DIR/map_unwrap_or.rs:24:13
| |
LL | let _ = opt.map(|x| x + 1) LL | let _ = opt.map(|x| x + 1)
| _____________^ | _____________^
@ -35,7 +49,7 @@ LL | }, |x| x + 1);
| |
error: called `map(<f>).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(<f>)` instead error: called `map(<f>).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(<f>)` instead
--> $DIR/map_unwrap_or.rs:25:13 --> $DIR/map_unwrap_or.rs:29:13
| |
LL | let _ = opt.map(|x| Some(x + 1)).unwrap_or(None); LL | let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -46,7 +60,7 @@ LL | let _ = opt.and_then(|x| Some(x + 1));
| ^^^^^^^^ -- | ^^^^^^^^ --
error: called `map(<f>).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(<f>)` instead error: called `map(<f>).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(<f>)` instead
--> $DIR/map_unwrap_or.rs:27:13 --> $DIR/map_unwrap_or.rs:31:13
| |
LL | let _ = opt.map(|x| { LL | let _ = opt.map(|x| {
| _____________^ | _____________^
@ -64,7 +78,7 @@ LL | );
| |
error: called `map(<f>).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(<f>)` instead error: called `map(<f>).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(<f>)` instead
--> $DIR/map_unwrap_or.rs:31:13 --> $DIR/map_unwrap_or.rs:35:13
| |
LL | let _ = opt LL | let _ = opt
| _____________^ | _____________^
@ -78,7 +92,7 @@ LL | .and_then(|x| Some(x + 1));
| ^^^^^^^^ -- | ^^^^^^^^ --
error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
--> $DIR/map_unwrap_or.rs:42:13 --> $DIR/map_unwrap_or.rs:46:13
| |
LL | let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id); LL | let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -89,7 +103,7 @@ LL | let _ = Some("prefix").map_or(id, |p| format!("{}.", p));
| ^^^^^^ ^^^ -- | ^^^^^^ ^^^ --
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 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
--> $DIR/map_unwrap_or.rs:46:13 --> $DIR/map_unwrap_or.rs:50:13
| |
LL | let _ = opt.map(|x| { LL | let _ = opt.map(|x| {
| _____________^ | _____________^
@ -99,7 +113,7 @@ LL | | ).unwrap_or_else(|| 0);
| |__________________________^ | |__________________________^
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 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
--> $DIR/map_unwrap_or.rs:50:13 --> $DIR/map_unwrap_or.rs:54:13
| |
LL | let _ = opt.map(|x| x + 1) LL | let _ = opt.map(|x| x + 1)
| _____________^ | _____________^
@ -108,5 +122,25 @@ LL | | 0
LL | | ); LL | | );
| |_________^ | |_________^
error: aborting due to 8 previous errors 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
--> $DIR/map_unwrap_or.rs:66:13
|
LL | let _ = res.map(|x| {
| _____________^
LL | | x + 1
LL | | }
LL | | ).unwrap_or_else(|_e| 0);
| |____________________________^
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
--> $DIR/map_unwrap_or.rs:70:13
|
LL | let _ = res.map(|x| x + 1)
| _____________^
LL | | .unwrap_or_else(|_e| {
LL | | 0
LL | | });
| |__________^
error: aborting due to 11 previous errors

View file

@ -1,40 +0,0 @@
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
--> $DIR/map_unwrap_or_else_fixable.rs:17:13
|
LL | let _ = opt.map(|x| x + 1)
| _____________^
LL | | // Should lint even though this call is on a separate line.
LL | | .unwrap_or_else(|| 0);
| |_____________________________^ help: try this: `opt.map_or_else(|| 0, |x| x + 1)`
|
= note: `-D clippy::map-unwrap-or` implied by `-D warnings`
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
--> $DIR/map_unwrap_or_else_fixable.rs:27:13
|
LL | let _ = opt.map(|x| x + 1)
| _____________^
LL | | // Should lint even though this call is on a separate line.
LL | | .unwrap_or_else(|| 0);
| |_____________________________^ help: try this: `opt.map_or_else(|| 0, |x| x + 1)`
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
--> $DIR/map_unwrap_or_else_fixable.rs:52:13
|
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0); // should lint even though this call is on a separate line
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|_e| 0, |x| x + 1)`
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
--> $DIR/map_unwrap_or_else_fixable.rs:54:13
|
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|_e| 0, |x| x + 1)`
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
--> $DIR/map_unwrap_or_else_fixable.rs:55:13
|
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|_e| 0, |x| x + 1)`
error: aborting due to 5 previous errors

View file

@ -20,10 +20,6 @@ fn option_methods() {
// Should not lint. // Should not lint.
let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0); let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
// Check for `option.map(_).unwrap_or_else(_)` use.
// single line case
let _ = opt.map_or_else(|| 0, |x| x + 1);
// Issue #4144 // Issue #4144
{ {
let mut frequencies = HashMap::new(); let mut frequencies = HashMap::new();
@ -40,15 +36,14 @@ fn option_methods() {
} }
} }
#[rustfmt::skip]
fn result_methods() { fn result_methods() {
let res: Result<i32, ()> = Ok(1); let res: Result<i32, ()> = Ok(1);
// Check for `result.map(_).unwrap_or_else(_)` use. // Check for `result.map(_).unwrap_or_else(_)` use.
// single line case // single line case
let _ = res.map_or_else(|_e| 0, |x| x + 1); // should lint even though this call is on a separate line
// multi line cases
let _ = res.map_or_else(|_e| 0, |x| x + 1);
let _ = res.map_or_else(|_e| 0, |x| x + 1); let _ = res.map_or_else(|_e| 0, |x| x + 1);
// macro case // macro case
let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint
} }

View file

@ -22,12 +22,6 @@ fn option_methods() {
// Should not lint. // Should not lint.
let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0); let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
// Check for `option.map(_).unwrap_or_else(_)` use.
// single line case
let _ = opt.map(|x| x + 1)
// Should lint even though this call is on a separate line.
.unwrap_or_else(|| 0);
// Issue #4144 // Issue #4144
{ {
let mut frequencies = HashMap::new(); let mut frequencies = HashMap::new();
@ -44,15 +38,16 @@ fn option_methods() {
} }
} }
#[rustfmt::skip]
fn result_methods() { fn result_methods() {
let res: Result<i32, ()> = Ok(1); let res: Result<i32, ()> = Ok(1);
// Check for `result.map(_).unwrap_or_else(_)` use. // Check for `result.map(_).unwrap_or_else(_)` use.
// single line case // single line case
let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0); // should lint even though this call is on a separate line let _ = res.map(|x| x + 1)
// multi line cases // should lint even though this call is on a separate line
let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0); .unwrap_or_else(|_e| 0);
let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0);
// macro case // macro case
let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint
} }

View file

@ -0,0 +1,22 @@
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
--> $DIR/map_unwrap_or_fixable.rs:17:13
|
LL | let _ = opt.map(|x| x + 1)
| _____________^
LL | | // Should lint even though this call is on a separate line.
LL | | .unwrap_or_else(|| 0);
| |_____________________________^ help: try this: `opt.map_or_else(|| 0, |x| x + 1)`
|
= note: `-D clippy::map-unwrap-or` implied by `-D warnings`
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
--> $DIR/map_unwrap_or_fixable.rs:47:13
|
LL | let _ = res.map(|x| x + 1)
| _____________^
LL | | // should lint even though this call is on a separate line
LL | | .unwrap_or_else(|_e| 0);
| |_______________________________^ help: try this: `res.map_or_else(|_e| 0, |x| x + 1)`
error: aborting due to 2 previous errors