-
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.
This commit adds a new lint named `filter_map_identity`. This lint is the same as `flat_map_identity` except that it checks for `filter_map`. Closes #6643
- Loading branch information
1 parent
a507c27
commit a60c143
Showing
7 changed files
with
143 additions
and
1 deletion.
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,56 @@ | ||
use crate::utils::{match_qpath, match_trait_method, paths, span_lint_and_sugg}; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_span::source_map::Span; | ||
|
||
use super::FILTER_MAP_IDENTITY; | ||
|
||
pub(super) fn check( | ||
cx: &LateContext<'_>, | ||
expr: &hir::Expr<'_>, | ||
filter_map_args: &[hir::Expr<'_>], | ||
filter_map_span: Span, | ||
) { | ||
if match_trait_method(cx, expr, &paths::ITERATOR) { | ||
let arg_node = &filter_map_args[1].kind; | ||
|
||
let apply_lint = |message: &str| { | ||
span_lint_and_sugg( | ||
cx, | ||
FILTER_MAP_IDENTITY, | ||
filter_map_span.with_hi(expr.span.hi()), | ||
message, | ||
"try", | ||
"flatten()".to_string(), | ||
Applicability::MachineApplicable, | ||
); | ||
}; | ||
|
||
if_chain! { | ||
if let hir::ExprKind::Closure(_, _, body_id, _, _) = arg_node; | ||
let body = cx.tcx.hir().body(*body_id); | ||
|
||
if let hir::PatKind::Binding(_, _, binding_ident, _) = body.params[0].pat.kind; | ||
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = body.value.kind; | ||
|
||
if path.segments.len() == 1; | ||
if path.segments[0].ident.name == binding_ident.name; | ||
|
||
then { | ||
apply_lint("called `filter_map(|x| x)` on an `Iterator`"); | ||
} | ||
} | ||
|
||
if_chain! { | ||
if let hir::ExprKind::Path(ref qpath) = arg_node; | ||
|
||
if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY); | ||
|
||
then { | ||
apply_lint("called `filter_map(std::convert::identity)` on an `Iterator`"); | ||
} | ||
} | ||
} | ||
} |
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,16 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused_imports)] | ||
#![warn(clippy::filter_map_identity)] | ||
|
||
fn main() { | ||
let iterator = vec![Some(1), None, Some(2)].into_iter(); | ||
let _ = iterator.flatten(); | ||
|
||
let iterator = vec![Some(1), None, Some(2)].into_iter(); | ||
let _ = iterator.flatten(); | ||
|
||
use std::convert::identity; | ||
let iterator = vec![Some(1), None, Some(2)].into_iter(); | ||
let _ = iterator.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,16 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused_imports)] | ||
#![warn(clippy::filter_map_identity)] | ||
|
||
fn main() { | ||
let iterator = vec![Some(1), None, Some(2)].into_iter(); | ||
let _ = iterator.filter_map(|x| x); | ||
|
||
let iterator = vec![Some(1), None, Some(2)].into_iter(); | ||
let _ = iterator.filter_map(std::convert::identity); | ||
|
||
use std::convert::identity; | ||
let iterator = vec![Some(1), None, Some(2)].into_iter(); | ||
let _ = iterator.filter_map(identity); | ||
} |
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,22 @@ | ||
error: called `filter_map(|x| x)` on an `Iterator` | ||
--> $DIR/filter_map_identity.rs:8:22 | ||
| | ||
LL | let _ = iterator.filter_map(|x| x); | ||
| ^^^^^^^^^^^^^^^^^ help: try: `flatten()` | ||
| | ||
= note: `-D clippy::filter-map-identity` implied by `-D warnings` | ||
|
||
error: called `filter_map(std::convert::identity)` on an `Iterator` | ||
--> $DIR/filter_map_identity.rs:11:22 | ||
| | ||
LL | let _ = iterator.filter_map(std::convert::identity); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `flatten()` | ||
|
||
error: called `filter_map(std::convert::identity)` on an `Iterator` | ||
--> $DIR/filter_map_identity.rs:15:22 | ||
| | ||
LL | let _ = iterator.filter_map(identity); | ||
| ^^^^^^^^^^^^^^^^^^^^ help: try: `flatten()` | ||
|
||
error: aborting due to 3 previous errors | ||
|