-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move fixable
map_unwrap_or
cases to rustfixed test
- Loading branch information
Showing
5 changed files
with
172 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|