forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
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 rust-lang#11869 - PartiallyTyped:result-filter-map, r=A…
…lexendoo New Lint: `result_filter_map` / Mirror of `option_filter_map` Added the `Result` mirror of `option_filter_map`. changelog: New Lint: [`result_filter_map`] I had to move around some code because the function def was too long 🙃. I have also added some pattern checks on `option_filter_map`
- Loading branch information
Showing
10 changed files
with
217 additions
and
31 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,27 @@ | ||
#![warn(clippy::result_filter_map)] | ||
#![allow(clippy::map_flatten, clippy::unnecessary_map_on_constructor)] | ||
|
||
fn odds_out(x: i32) -> Result<i32, ()> { | ||
if x % 2 == 0 { Ok(x) } else { Err(()) } | ||
} | ||
|
||
fn main() { | ||
// Unlike the Option version, Result does not have a filter operation => we will check for iterators | ||
// only | ||
let _ = vec![Ok(1) as Result<i32, ()>] | ||
.into_iter() | ||
.flatten(); | ||
|
||
let _ = vec![Ok(1) as Result<i32, ()>] | ||
.into_iter() | ||
.flatten(); | ||
|
||
let _ = vec![1] | ||
.into_iter() | ||
.map(odds_out) | ||
.flatten(); | ||
let _ = vec![1] | ||
.into_iter() | ||
.map(odds_out) | ||
.flatten(); | ||
} |
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,35 @@ | ||
#![warn(clippy::result_filter_map)] | ||
#![allow(clippy::map_flatten, clippy::unnecessary_map_on_constructor)] | ||
|
||
fn odds_out(x: i32) -> Result<i32, ()> { | ||
if x % 2 == 0 { Ok(x) } else { Err(()) } | ||
} | ||
|
||
fn main() { | ||
// Unlike the Option version, Result does not have a filter operation => we will check for iterators | ||
// only | ||
let _ = vec![Ok(1) as Result<i32, ()>] | ||
.into_iter() | ||
.filter(Result::is_ok) | ||
//~^ ERROR: `filter` for `Ok` followed by `unwrap` | ||
.map(Result::unwrap); | ||
|
||
let _ = vec![Ok(1) as Result<i32, ()>] | ||
.into_iter() | ||
.filter(|o| o.is_ok()) | ||
//~^ ERROR: `filter` for `Ok` followed by `unwrap` | ||
.map(|o| o.unwrap()); | ||
|
||
let _ = vec![1] | ||
.into_iter() | ||
.map(odds_out) | ||
.filter(Result::is_ok) | ||
//~^ ERROR: `filter` for `Ok` followed by `unwrap` | ||
.map(Result::unwrap); | ||
let _ = vec![1] | ||
.into_iter() | ||
.map(odds_out) | ||
.filter(|o| o.is_ok()) | ||
//~^ ERROR: `filter` for `Ok` followed by `unwrap` | ||
.map(|o| o.unwrap()); | ||
} |
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,41 @@ | ||
error: `filter` for `Ok` followed by `unwrap` | ||
--> $DIR/result_filter_map.rs:13:10 | ||
| | ||
LL | .filter(Result::is_ok) | ||
| __________^ | ||
LL | | | ||
LL | | .map(Result::unwrap); | ||
| |____________________________^ help: consider using `flatten` instead: `flatten()` | ||
| | ||
= note: `-D clippy::result-filter-map` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::result_filter_map)]` | ||
|
||
error: `filter` for `Ok` followed by `unwrap` | ||
--> $DIR/result_filter_map.rs:19:10 | ||
| | ||
LL | .filter(|o| o.is_ok()) | ||
| __________^ | ||
LL | | | ||
LL | | .map(|o| o.unwrap()); | ||
| |____________________________^ help: consider using `flatten` instead: `flatten()` | ||
|
||
error: `filter` for `Ok` followed by `unwrap` | ||
--> $DIR/result_filter_map.rs:26:10 | ||
| | ||
LL | .filter(Result::is_ok) | ||
| __________^ | ||
LL | | | ||
LL | | .map(Result::unwrap); | ||
| |____________________________^ help: consider using `flatten` instead: `flatten()` | ||
|
||
error: `filter` for `Ok` followed by `unwrap` | ||
--> $DIR/result_filter_map.rs:32:10 | ||
| | ||
LL | .filter(|o| o.is_ok()) | ||
| __________^ | ||
LL | | | ||
LL | | .map(|o| o.unwrap()); | ||
| |____________________________^ help: consider using `flatten` instead: `flatten()` | ||
|
||
error: aborting due to 4 previous errors | ||
|