Skip to content

Commit 9e54ce8

Browse files
committed
Reuse is_expr_identity_function for filter_map_identity
1 parent bb3b58c commit 9e54ce8

File tree

4 files changed

+29
-35
lines changed

4 files changed

+29
-35
lines changed
Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::{is_expr_path_def_path, is_trait_method, path_to_local_id, paths};
3-
use if_chain::if_chain;
2+
use clippy_utils::{is_expr_identity_function, is_trait_method};
43
use rustc_errors::Applicability;
54
use rustc_hir as hir;
65
use rustc_lint::LateContext;
@@ -9,32 +8,15 @@ use rustc_span::{source_map::Span, sym};
98
use super::FILTER_MAP_IDENTITY;
109

1110
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, filter_map_arg: &hir::Expr<'_>, filter_map_span: Span) {
12-
if is_trait_method(cx, expr, sym::Iterator) {
13-
let apply_lint = |message: &str| {
14-
span_lint_and_sugg(
15-
cx,
16-
FILTER_MAP_IDENTITY,
17-
filter_map_span.with_hi(expr.span.hi()),
18-
message,
19-
"try",
20-
"flatten()".to_string(),
21-
Applicability::MachineApplicable,
22-
);
23-
};
24-
25-
if_chain! {
26-
if let hir::ExprKind::Closure(_, _, body_id, _, _) = filter_map_arg.kind;
27-
let body = cx.tcx.hir().body(body_id);
28-
29-
if let hir::PatKind::Binding(_, binding_id, ..) = body.params[0].pat.kind;
30-
if path_to_local_id(&body.value, binding_id);
31-
then {
32-
apply_lint("called `filter_map(|x| x)` on an `Iterator`");
33-
}
34-
}
35-
36-
if is_expr_path_def_path(cx, filter_map_arg, &paths::CONVERT_IDENTITY) {
37-
apply_lint("called `filter_map(std::convert::identity)` on an `Iterator`");
38-
}
11+
if is_trait_method(cx, expr, sym::Iterator) && is_expr_identity_function(cx, filter_map_arg) {
12+
span_lint_and_sugg(
13+
cx,
14+
FILTER_MAP_IDENTITY,
15+
filter_map_span.with_hi(expr.span.hi()),
16+
"use of `filter_map` with an identity function",
17+
"try",
18+
"flatten()".to_string(),
19+
Applicability::MachineApplicable,
20+
);
3921
}
4022
}

tests/ui/filter_map_identity.fixed

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// run-rustfix
22

3-
#![allow(unused_imports)]
3+
#![allow(unused_imports, clippy::needless_return)]
44
#![warn(clippy::filter_map_identity)]
55

66
fn main() {
@@ -13,4 +13,7 @@ fn main() {
1313
use std::convert::identity;
1414
let iterator = vec![Some(1), None, Some(2)].into_iter();
1515
let _ = iterator.flatten();
16+
17+
let iterator = vec![Some(1), None, Some(2)].into_iter();
18+
let _ = iterator.flatten();
1619
}

tests/ui/filter_map_identity.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// run-rustfix
22

3-
#![allow(unused_imports)]
3+
#![allow(unused_imports, clippy::needless_return)]
44
#![warn(clippy::filter_map_identity)]
55

66
fn main() {
@@ -13,4 +13,7 @@ fn main() {
1313
use std::convert::identity;
1414
let iterator = vec![Some(1), None, Some(2)].into_iter();
1515
let _ = iterator.filter_map(identity);
16+
17+
let iterator = vec![Some(1), None, Some(2)].into_iter();
18+
let _ = iterator.filter_map(|x| return x);
1619
}

tests/ui/filter_map_identity.stderr

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
error: called `filter_map(|x| x)` on an `Iterator`
1+
error: use of `filter_map` with an identity function
22
--> $DIR/filter_map_identity.rs:8:22
33
|
44
LL | let _ = iterator.filter_map(|x| x);
55
| ^^^^^^^^^^^^^^^^^ help: try: `flatten()`
66
|
77
= note: `-D clippy::filter-map-identity` implied by `-D warnings`
88

9-
error: called `filter_map(std::convert::identity)` on an `Iterator`
9+
error: use of `filter_map` with an identity function
1010
--> $DIR/filter_map_identity.rs:11:22
1111
|
1212
LL | let _ = iterator.filter_map(std::convert::identity);
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `flatten()`
1414

15-
error: called `filter_map(std::convert::identity)` on an `Iterator`
15+
error: use of `filter_map` with an identity function
1616
--> $DIR/filter_map_identity.rs:15:22
1717
|
1818
LL | let _ = iterator.filter_map(identity);
1919
| ^^^^^^^^^^^^^^^^^^^^ help: try: `flatten()`
2020

21-
error: aborting due to 3 previous errors
21+
error: use of `filter_map` with an identity function
22+
--> $DIR/filter_map_identity.rs:18:22
23+
|
24+
LL | let _ = iterator.filter_map(|x| return x);
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `flatten()`
26+
27+
error: aborting due to 4 previous errors
2228

0 commit comments

Comments
 (0)