-
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.
Auto merge of #4039 - andrehjr:add-find-map-lints, r=flip1995
Add lints for find_map changelog: adds lints for find_map and filter_map_next. Closes issue #3474 Hope I got everything correctly this time! Let me know if I missed something.
- Loading branch information
Showing
11 changed files
with
208 additions
and
10 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
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
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,20 @@ | ||
#![warn(clippy::all, clippy::pedantic)] | ||
|
||
fn main() { | ||
let a = ["1", "lol", "3", "NaN", "5"]; | ||
|
||
let element: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next(); | ||
assert_eq!(element, Some(1)); | ||
|
||
#[rustfmt::skip] | ||
let _: Option<u32> = vec![1, 2, 3, 4, 5, 6] | ||
.into_iter() | ||
.filter_map(|x| { | ||
if x == 2 { | ||
Some(x * 2) | ||
} else { | ||
None | ||
} | ||
}) | ||
.next(); | ||
} |
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,24 @@ | ||
error: called `filter_map(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find_map(p)` instead. | ||
--> $DIR/filter_map_next.rs:6:32 | ||
| | ||
LL | let element: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::filter-map-next` implied by `-D warnings` | ||
= note: replace `filter_map(|s| s.parse().ok()).next()` with `find_map(|s| s.parse().ok())` | ||
|
||
error: called `filter_map(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find_map(p)` instead. | ||
--> $DIR/filter_map_next.rs:10:26 | ||
| | ||
LL | let _: Option<u32> = vec![1, 2, 3, 4, 5, 6] | ||
| __________________________^ | ||
LL | | .into_iter() | ||
LL | | .filter_map(|x| { | ||
LL | | if x == 2 { | ||
... | | ||
LL | | }) | ||
LL | | .next(); | ||
| |_______________^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
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,32 @@ | ||
#![warn(clippy::all, clippy::pedantic)] | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
enum Flavor { | ||
Chocolate, | ||
} | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
enum Dessert { | ||
Banana, | ||
Pudding, | ||
Cake(Flavor), | ||
} | ||
|
||
fn main() { | ||
let desserts_of_the_week = vec![Dessert::Banana, Dessert::Cake(Flavor::Chocolate), Dessert::Pudding]; | ||
|
||
let a = ["lol", "NaN", "2", "5", "Xunda"]; | ||
|
||
let _: Option<i32> = a.iter().find(|s| s.parse::<i32>().is_ok()).map(|s| s.parse().unwrap()); | ||
|
||
let _: Option<Flavor> = desserts_of_the_week | ||
.iter() | ||
.find(|dessert| match *dessert { | ||
Dessert::Cake(_) => true, | ||
_ => false, | ||
}) | ||
.map(|dessert| match *dessert { | ||
Dessert::Cake(ref flavor) => *flavor, | ||
_ => unreachable!(), | ||
}); | ||
} |
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,23 @@ | ||
error: called `find(p).map(q)` on an `Iterator`. This is more succinctly expressed by calling `.find_map(..)` instead. | ||
--> $DIR/find_map.rs:20:26 | ||
| | ||
LL | let _: Option<i32> = a.iter().find(|s| s.parse::<i32>().is_ok()).map(|s| s.parse().unwrap()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::find-map` implied by `-D warnings` | ||
|
||
error: called `find(p).map(q)` on an `Iterator`. This is more succinctly expressed by calling `.find_map(..)` instead. | ||
--> $DIR/find_map.rs:22:29 | ||
| | ||
LL | let _: Option<Flavor> = desserts_of_the_week | ||
| _____________________________^ | ||
LL | | .iter() | ||
LL | | .find(|dessert| match *dessert { | ||
LL | | Dessert::Cake(_) => true, | ||
... | | ||
LL | | _ => unreachable!(), | ||
LL | | }); | ||
| |__________^ | ||
|
||
error: aborting due to 2 previous errors | ||
|