forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial draft. fixes rust-lang#6061.
- Loading branch information
Eric Loren
committed
Nov 29, 2020
1 parent
68cf94f
commit f722bb5
Showing
6 changed files
with
93 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use crate::utils::{match_qpath, paths}; | ||
use rustc_hir::{self, Expr, ExprKind}; | ||
use std::iter; | ||
|
||
fn calls_fn<'tcx>(expr: &'tcx [Expr<'_>], fn_name: &str) -> bool { | ||
expr.get(1).map_or(false, |x| match &x.kind { | ||
ExprKind::Path(qp) => match_qpath(qp, &paths::OPTION.iter().cloned().chain(iter::once(fn_name)).collect::<Vec<&str>>()), | ||
_ => false, | ||
}) | ||
} | ||
|
||
pub fn in_scope<'tcx>(filter_args: &'tcx [Expr<'_>], map_args: &'tcx [Expr<'_>]) -> bool { | ||
calls_fn(map_args, "unwrap") && calls_fn(filter_args, "is_some") | ||
} |
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,16 @@ | ||
fn odds_out(x: i32) -> Option<i32> { | ||
if x % 2 == 0 { | ||
Some(x) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn main() { | ||
let evens: Vec<i32> = vec![1, 2, 3] | ||
.into_iter() | ||
.map(odds_out) | ||
.filter(Option::is_some) | ||
.map(Option::unwrap) | ||
.collect(); | ||
} |
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,16 @@ | ||
error: `filter` for `Some` followed by `unwrap` | ||
--> $DIR/option_filter_map.rs:10:27 | ||
| | ||
LL | let evens: Vec<i32> = vec![1, 2, 3] | ||
| ___________________________^ | ||
LL | | .into_iter() | ||
LL | | .map(odds_out) | ||
LL | | .filter(Option::is_some) | ||
LL | | .map(Option::unwrap) | ||
| |____________________________^ | ||
| | ||
= note: `-D clippy::option-filter-map` implied by `-D warnings` | ||
= help: consider using `flatten` instead | ||
|
||
error: aborting due to previous error | ||
|