Skip to content

Commit

Permalink
Move fixable map_unwrap_or cases to rustfixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibsG committed Oct 23, 2020
1 parent fd8c566 commit e75534e
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 92 deletions.
41 changes: 0 additions & 41 deletions tests/ui/map_unwrap_or.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// FIXME: Add "run-rustfix" once it's supported for multipart suggestions
// aux-build:option_helpers.rs

#![warn(clippy::map_unwrap_or)]
Expand All @@ -13,10 +12,6 @@ fn option_methods() {
let opt = Some(1);

// 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.
let _ = opt.map(|x| {
x + 1
Expand Down Expand Up @@ -47,10 +42,6 @@ fn option_methods() {
let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);

// 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.
let _ = opt.map(|x| {
x + 1
Expand All @@ -60,40 +51,8 @@ fn option_methods() {
.unwrap_or_else(||
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() {
option_methods();
result_methods();
}
61 changes: 10 additions & 51 deletions tests/ui/map_unwrap_or.stderr
Original file line number Diff line number Diff line change
@@ -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
--> $DIR/map_unwrap_or.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(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
--> $DIR/map_unwrap_or.rs:16:13
|
LL | let _ = opt.map(|x| {
| _____________^
Expand All @@ -23,6 +8,7 @@ LL | | }
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| {
Expand All @@ -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
--> $DIR/map_unwrap_or.rs:25:13
--> $DIR/map_unwrap_or.rs:20:13
|
LL | let _ = opt.map(|x| x + 1)
| _____________^
Expand All @@ -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
--> $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);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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
--> $DIR/map_unwrap_or.rs:32:13
--> $DIR/map_unwrap_or.rs:27:13
|
LL | let _ = opt.map(|x| {
| _____________^
Expand All @@ -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
--> $DIR/map_unwrap_or.rs:36:13
--> $DIR/map_unwrap_or.rs:31:13
|
LL | let _ = opt
| _____________^
Expand All @@ -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
--> $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);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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
--> $DIR/map_unwrap_or.rs:51: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
--> $DIR/map_unwrap_or.rs:46:13
|
LL | let _ = opt.map(|x| {
| _____________^
Expand All @@ -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
--> $DIR/map_unwrap_or.rs:59:13
--> $DIR/map_unwrap_or.rs:50:13
|
LL | let _ = opt.map(|x| x + 1)
| _____________^
Expand All @@ -131,23 +108,5 @@ LL | | 0
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
--> $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
error: aborting due to 8 previous errors

59 changes: 59 additions & 0 deletions tests/ui/map_unwrap_or_else_fixable.fixed
Original file line number Diff line number Diff line change
@@ -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 changes: 63 additions & 0 deletions tests/ui/map_unwrap_or_else_fixable.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 40 additions & 0 deletions tests/ui/map_unwrap_or_else_fixable.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e75534e

Please sign in to comment.