Skip to content

Commit bb3b58c

Browse files
committed
Reuse is_expr_identity_function for flat_map_identity
1 parent 967d815 commit bb3b58c

File tree

4 files changed

+28
-38
lines changed

4 files changed

+28
-38
lines changed
Lines changed: 11 additions & 33 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, 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;
@@ -15,36 +14,15 @@ pub(super) fn check<'tcx>(
1514
flat_map_arg: &'tcx hir::Expr<'_>,
1615
flat_map_span: Span,
1716
) {
18-
if is_trait_method(cx, expr, sym::Iterator) {
19-
let apply_lint = |message: &str| {
20-
span_lint_and_sugg(
21-
cx,
22-
FLAT_MAP_IDENTITY,
23-
flat_map_span.with_hi(expr.span.hi()),
24-
message,
25-
"try",
26-
"flatten()".to_string(),
27-
Applicability::MachineApplicable,
28-
);
29-
};
30-
31-
if_chain! {
32-
if let hir::ExprKind::Closure(_, _, body_id, _, _) = flat_map_arg.kind;
33-
let body = cx.tcx.hir().body(body_id);
34-
35-
if let hir::PatKind::Binding(_, _, binding_ident, _) = body.params[0].pat.kind;
36-
if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = body.value.kind;
37-
38-
if path.segments.len() == 1;
39-
if path.segments[0].ident.name == binding_ident.name;
40-
41-
then {
42-
apply_lint("called `flat_map(|x| x)` on an `Iterator`");
43-
}
44-
}
45-
46-
if is_expr_path_def_path(cx, flat_map_arg, &paths::CONVERT_IDENTITY) {
47-
apply_lint("called `flat_map(std::convert::identity)` on an `Iterator`");
48-
}
17+
if is_trait_method(cx, expr, sym::Iterator) && is_expr_identity_function(cx, flat_map_arg) {
18+
span_lint_and_sugg(
19+
cx,
20+
FLAT_MAP_IDENTITY,
21+
flat_map_span.with_hi(expr.span.hi()),
22+
"use of `flat_map` with an identity function",
23+
"try",
24+
"flatten()".to_string(),
25+
Applicability::MachineApplicable,
26+
);
4927
}
5028
}

tests/ui/flat_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::flat_map_identity)]
55

66
use std::convert;
@@ -11,4 +11,7 @@ fn main() {
1111

1212
let iterator = [[0, 1], [2, 3], [4, 5]].iter();
1313
let _ = iterator.flatten();
14+
15+
let iterator = [[0, 1], [2, 3], [4, 5]].iter();
16+
let _ = iterator.flatten();
1417
}

tests/ui/flat_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::flat_map_identity)]
55

66
use std::convert;
@@ -11,4 +11,7 @@ fn main() {
1111

1212
let iterator = [[0, 1], [2, 3], [4, 5]].iter();
1313
let _ = iterator.flat_map(convert::identity);
14+
15+
let iterator = [[0, 1], [2, 3], [4, 5]].iter();
16+
let _ = iterator.flat_map(|x| return x);
1417
}

tests/ui/flat_map_identity.stderr

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

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

15-
error: aborting due to 2 previous errors
15+
error: use of `flat_map` with an identity function
16+
--> $DIR/flat_map_identity.rs:16:22
17+
|
18+
LL | let _ = iterator.flat_map(|x| return x);
19+
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `flatten()`
20+
21+
error: aborting due to 3 previous errors
1622

0 commit comments

Comments
 (0)