Move fixable map_unwrap_or
cases to rustfixed test
This commit is contained in:
parent
3fec6f568d
commit
2a3ae11485
@ -1,4 +1,3 @@
|
|||||||
// FIXME: Add "run-rustfix" once it's supported for multipart suggestions
|
|
||||||
// aux-build:option_helpers.rs
|
// aux-build:option_helpers.rs
|
||||||
|
|
||||||
#![warn(clippy::map_unwrap_or)]
|
#![warn(clippy::map_unwrap_or)]
|
||||||
@ -13,10 +12,6 @@ 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
|
||||||
@ -47,10 +42,6 @@ fn option_methods() {
|
|||||||
let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
|
let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
|
||||||
|
|
||||||
// Check for `option.map(_).unwrap_or_else(_)` use.
|
// 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);
|
|
||||||
// Multi-line cases.
|
// Multi-line cases.
|
||||||
let _ = opt.map(|x| {
|
let _ = opt.map(|x| {
|
||||||
x + 1
|
x + 1
|
||||||
@ -60,40 +51,8 @@ fn option_methods() {
|
|||||||
.unwrap_or_else(||
|
.unwrap_or_else(||
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
// Macro case.
|
|
||||||
// Should not lint.
|
|
||||||
let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
|
|
||||||
|
|
||||||
// Issue #4144
|
|
||||||
{
|
|
||||||
let mut frequencies = HashMap::new();
|
|
||||||
let word = "foo";
|
|
||||||
|
|
||||||
frequencies
|
|
||||||
.get_mut(word)
|
|
||||||
.map(|count| {
|
|
||||||
*count += 1;
|
|
||||||
})
|
|
||||||
.unwrap_or_else(|| {
|
|
||||||
frequencies.insert(word.to_owned(), 1);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn result_methods() {
|
|
||||||
let res: Result<i32, ()> = Ok(1);
|
|
||||||
|
|
||||||
// Check for `result.map(_).unwrap_or_else(_)` use.
|
|
||||||
// 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
|
|
||||||
// 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();
|
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,5 @@
|
|||||||
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:17: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:21:13
|
|
||||||
|
|
|
|
||||||
LL | let _ = opt.map(|x| {
|
LL | let _ = opt.map(|x| {
|
||||||
| _____________^
|
| _____________^
|
||||||
@ -23,6 +8,7 @@ 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| {
|
||||||
@ -32,7 +18,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:25:13
|
--> $DIR/map_unwrap_or.rs:20:13
|
||||||
|
|
|
|
||||||
LL | let _ = opt.map(|x| x + 1)
|
LL | let _ = opt.map(|x| x + 1)
|
||||||
| _____________^
|
| _____________^
|
||||||
@ -49,7 +35,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:30:13
|
--> $DIR/map_unwrap_or.rs:25:13
|
||||||
|
|
|
|
||||||
LL | let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
|
LL | let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
@ -60,7 +46,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:32:13
|
--> $DIR/map_unwrap_or.rs:27:13
|
||||||
|
|
|
|
||||||
LL | let _ = opt.map(|x| {
|
LL | let _ = opt.map(|x| {
|
||||||
| _____________^
|
| _____________^
|
||||||
@ -78,7 +64,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:36:13
|
--> $DIR/map_unwrap_or.rs:31:13
|
||||||
|
|
|
|
||||||
LL | let _ = opt
|
LL | let _ = opt
|
||||||
| _____________^
|
| _____________^
|
||||||
@ -92,7 +78,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:47:13
|
--> $DIR/map_unwrap_or.rs:42:13
|
||||||
|
|
|
|
||||||
LL | let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
|
LL | let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
@ -103,16 +89,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:51:13
|
--> $DIR/map_unwrap_or.rs:46: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 an `Option` value. This can be done more directly by calling `map_or_else(<g>, <f>)` instead
|
|
||||||
--> $DIR/map_unwrap_or.rs:55:13
|
|
||||||
|
|
|
|
||||||
LL | let _ = opt.map(|x| {
|
LL | let _ = opt.map(|x| {
|
||||||
| _____________^
|
| _____________^
|
||||||
@ -122,7 +99,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:59:13
|
--> $DIR/map_unwrap_or.rs:50:13
|
||||||
|
|
|
|
||||||
LL | let _ = opt.map(|x| x + 1)
|
LL | let _ = opt.map(|x| x + 1)
|
||||||
| _____________^
|
| _____________^
|
||||||
@ -131,23 +108,5 @@ LL | | 0
|
|||||||
LL | | );
|
LL | | );
|
||||||
| |_________^
|
| |_________^
|
||||||
|
|
||||||
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
|
error: aborting due to 8 previous errors
|
||||||
--> $DIR/map_unwrap_or.rs:88: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.rs:90: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.rs:91: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 13 previous errors
|
|
||||||
|
|
||||||
|
59
tests/ui/map_unwrap_or_else_fixable.fixed
Normal file
59
tests/ui/map_unwrap_or_else_fixable.fixed
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
// run-rustfix
|
||||||
|
// aux-build:option_helpers.rs
|
||||||
|
|
||||||
|
#![warn(clippy::map_unwrap_or)]
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate option_helpers;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[rustfmt::skip]
|
||||||
|
fn option_methods() {
|
||||||
|
let opt = Some(1);
|
||||||
|
|
||||||
|
// Check for `option.map(_).unwrap_or_else(_)` use.
|
||||||
|
// single line case
|
||||||
|
let _ = opt.map_or_else(|| 0, |x| x + 1);
|
||||||
|
|
||||||
|
// Macro case.
|
||||||
|
// Should not lint.
|
||||||
|
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
|
||||||
|
{
|
||||||
|
let mut frequencies = HashMap::new();
|
||||||
|
let word = "foo";
|
||||||
|
|
||||||
|
frequencies
|
||||||
|
.get_mut(word)
|
||||||
|
.map(|count| {
|
||||||
|
*count += 1;
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
frequencies.insert(word.to_owned(), 1);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn result_methods() {
|
||||||
|
let res: Result<i32, ()> = Ok(1);
|
||||||
|
|
||||||
|
// Check for `result.map(_).unwrap_or_else(_)` use.
|
||||||
|
// 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);
|
||||||
|
// macro case
|
||||||
|
let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
option_methods();
|
||||||
|
result_methods();
|
||||||
|
}
|
63
tests/ui/map_unwrap_or_else_fixable.rs
Normal file
63
tests/ui/map_unwrap_or_else_fixable.rs
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
// run-rustfix
|
||||||
|
// aux-build:option_helpers.rs
|
||||||
|
|
||||||
|
#![warn(clippy::map_unwrap_or)]
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate option_helpers;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[rustfmt::skip]
|
||||||
|
fn option_methods() {
|
||||||
|
let opt = Some(1);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// Macro case.
|
||||||
|
// Should not lint.
|
||||||
|
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
|
||||||
|
{
|
||||||
|
let mut frequencies = HashMap::new();
|
||||||
|
let word = "foo";
|
||||||
|
|
||||||
|
frequencies
|
||||||
|
.get_mut(word)
|
||||||
|
.map(|count| {
|
||||||
|
*count += 1;
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
frequencies.insert(word.to_owned(), 1);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn result_methods() {
|
||||||
|
let res: Result<i32, ()> = Ok(1);
|
||||||
|
|
||||||
|
// Check for `result.map(_).unwrap_or_else(_)` use.
|
||||||
|
// 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
|
||||||
|
// 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() {
|
||||||
|
option_methods();
|
||||||
|
result_methods();
|
||||||
|
}
|
40
tests/ui/map_unwrap_or_else_fixable.stderr
Normal file
40
tests/ui/map_unwrap_or_else_fixable.stderr
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
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
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user