-
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.
New lints
iter_filter_is_some
and iter_filter_is_ok
Adds a pair of lints that check for cases of an iterator over `Result` and `Option` followed by `filter` without being followed by `map` as that is covered already by a different, specialized lint. changelog: New Lint: [`iter_filter_is_some`] changelog: New Lint: [`iter_filter_is_ok`]
- Loading branch information
Showing
11 changed files
with
231 additions
and
3 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,79 @@ | ||
use rustc_lint::LateContext; | ||
|
||
use super::{ITER_FILTER_IS_OK, ITER_FILTER_IS_SOME}; | ||
|
||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::{indent_of, reindent_multiline}; | ||
use clippy_utils::{is_trait_method, peel_blocks}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_hir::def::Res; | ||
use rustc_hir::QPath; | ||
use rustc_span::symbol::{sym, Symbol}; | ||
use rustc_span::Span; | ||
use std::borrow::Cow; | ||
|
||
fn is_method(cx: &LateContext<'_>, expr: &hir::Expr<'_>, method_name: Symbol) -> bool { | ||
match &expr.kind { | ||
hir::ExprKind::Path(QPath::TypeRelative(_, mname)) => mname.ident.name == method_name, | ||
hir::ExprKind::Path(QPath::Resolved(_, segments)) => { | ||
segments.segments.last().unwrap().ident.name == method_name | ||
}, | ||
hir::ExprKind::MethodCall(segment, _, _, _) => segment.ident.name == method_name, | ||
hir::ExprKind::Closure(&hir::Closure { body, .. }) => { | ||
let body = cx.tcx.hir().body(body); | ||
let closure_expr = peel_blocks(body.value); | ||
let arg_id = body.params[0].pat.hir_id; | ||
match closure_expr.kind { | ||
hir::ExprKind::MethodCall(hir::PathSegment { ident, .. }, receiver, ..) => { | ||
if ident.name == method_name | ||
&& let hir::ExprKind::Path(path) = &receiver.kind | ||
&& let Res::Local(ref local) = cx.qpath_res(path, receiver.hir_id) | ||
{ | ||
return arg_id == *local; | ||
} | ||
false | ||
}, | ||
_ => false, | ||
} | ||
}, | ||
_ => false, | ||
} | ||
} | ||
|
||
fn parent_is_map(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool { | ||
if let hir::Node::Expr(parent_expr) = cx.tcx.hir().get_parent(expr.hir_id) { | ||
is_method(cx, parent_expr, rustc_span::sym::map) | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
#[allow(clippy::too_many_arguments)] | ||
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, filter_arg: &hir::Expr<'_>, filter_span: Span) { | ||
let is_iterator = is_trait_method(cx, expr, sym::Iterator); | ||
let parent_is_not_map = !parent_is_map(cx, expr); | ||
|
||
if is_iterator && parent_is_not_map && is_method(cx, filter_arg, sym!(is_some)) { | ||
span_lint_and_sugg( | ||
cx, | ||
ITER_FILTER_IS_SOME, | ||
filter_span.with_hi(expr.span.hi()), | ||
"`filter` for `is_some` on iterator over `Option`", | ||
"consider using `flatten` instead", | ||
reindent_multiline(Cow::Borrowed("flatten()"), true, indent_of(cx, filter_span)).into_owned(), | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
if is_iterator && parent_is_not_map && is_method(cx, filter_arg, sym!(is_ok)) { | ||
span_lint_and_sugg( | ||
cx, | ||
ITER_FILTER_IS_OK, | ||
filter_span.with_hi(expr.span.hi()), | ||
"`filter` for `is_ok` on iterator over `Result`s", | ||
"consider using `flatten` instead", | ||
reindent_multiline(Cow::Borrowed("flatten()"), true, indent_of(cx, filter_span)).into_owned(), | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} |
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,8 @@ | ||
#![warn(clippy::iter_filter_is_ok)] | ||
|
||
fn main() { | ||
let _ = vec![Ok(1), Err(2), Ok(3)].into_iter().flatten(); | ||
//~^ HELP: consider using `flatten` instead | ||
let _ = vec![Ok(1), Err(2), Ok(3)].into_iter().flatten(); | ||
//~^ HELP: consider using `flatten` instead | ||
} |
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,8 @@ | ||
#![warn(clippy::iter_filter_is_ok)] | ||
|
||
fn main() { | ||
let _ = vec![Ok(1), Err(2), Ok(3)].into_iter().filter(Result::is_ok); | ||
//~^ HELP: consider using `flatten` instead | ||
let _ = vec![Ok(1), Err(2), Ok(3)].into_iter().filter(|a| a.is_ok()); | ||
//~^ HELP: consider using `flatten` instead | ||
} |
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,17 @@ | ||
error: `filter` for `is_ok` on iterator over `Result`s | ||
--> $DIR/iter_filter_is_ok.rs:4:52 | ||
| | ||
LL | let _ = vec![Ok(1), Err(2), Ok(3)].into_iter().filter(Result::is_ok); | ||
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()` | ||
| | ||
= note: `-D clippy::iter-filter-is-ok` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::iter_filter_is_ok)]` | ||
|
||
error: `filter` for `is_ok` on iterator over `Result`s | ||
--> $DIR/iter_filter_is_ok.rs:6:52 | ||
| | ||
LL | let _ = vec![Ok(1), Err(2), Ok(3)].into_iter().filter(|a| a.is_ok()); | ||
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()` | ||
|
||
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,12 @@ | ||
#![warn(clippy::iter_filter_is_some)] | ||
|
||
fn odds_out(x: i32) -> Option<i32> { | ||
if x % 2 == 0 { Some(x) } else { None } | ||
} | ||
|
||
fn main() { | ||
let _ = vec![Some(1)].into_iter().flatten(); | ||
//~^ HELP: consider using `flatten` instead | ||
let _ = vec![Some(1)].into_iter().flatten(); | ||
//~^ HELP: consider using `flatten` instead | ||
} |
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,12 @@ | ||
#![warn(clippy::iter_filter_is_some)] | ||
|
||
fn odds_out(x: i32) -> Option<i32> { | ||
if x % 2 == 0 { Some(x) } else { None } | ||
} | ||
|
||
fn main() { | ||
let _ = vec![Some(1)].into_iter().filter(Option::is_some); | ||
//~^ HELP: consider using `flatten` instead | ||
let _ = vec![Some(1)].into_iter().filter(|o| o.is_some()); | ||
//~^ HELP: consider using `flatten` instead | ||
} |
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,17 @@ | ||
error: `filter` for `is_some` on iterator over `Option` | ||
--> $DIR/iter_filter_is_some.rs:8:39 | ||
| | ||
LL | let _ = vec![Some(1)].into_iter().filter(Option::is_some); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()` | ||
| | ||
= note: `-D clippy::iter-filter-is-some` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::iter_filter_is_some)]` | ||
|
||
error: `filter` for `is_some` on iterator over `Option` | ||
--> $DIR/iter_filter_is_some.rs:10:39 | ||
| | ||
LL | let _ = vec![Some(1)].into_iter().filter(|o| o.is_some()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()` | ||
|
||
error: aborting due to 2 previous errors | ||
|