Skip to content

Commit 3a65e4e

Browse files
committed
Minor refactoring
1 parent 09e9568 commit 3a65e4e

File tree

1 file changed

+22
-27
lines changed
  • clippy_lints/src/methods

1 file changed

+22
-27
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,42 +2172,37 @@ fn lint_flat_map_identity<'a, 'tcx>(
21722172
expr: &'tcx hir::Expr,
21732173
flat_map_args: &'tcx [hir::Expr],
21742174
) {
2175-
if_chain! {
2176-
if match_trait_method(cx, expr, &paths::ITERATOR);
2175+
if match_trait_method(cx, expr, &paths::ITERATOR) {
2176+
let arg_node = &flat_map_args[1].node;
21772177

2178-
if flat_map_args.len() == 2;
2178+
let apply_lint = |message: &str| {
2179+
span_lint(cx, FLAT_MAP_IDENTITY, expr.span, message);
2180+
};
21792181

2180-
then {
2181-
if_chain! {
2182-
if let hir::ExprKind::Closure(_, _, body_id, _, _) = flat_map_args[1].node;
2183-
let body = cx.tcx.hir().body(body_id);
2182+
if_chain! {
2183+
if let hir::ExprKind::Closure(_, _, body_id, _, _) = arg_node;
2184+
let body = cx.tcx.hir().body(*body_id);
21842185

2185-
if body.arguments.len() == 1;
2186-
if let hir::PatKind::Binding(_, _, binding_ident, _) = body.arguments[0].pat.node;
2187-
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = body.value.node;
2186+
if let hir::PatKind::Binding(_, _, binding_ident, _) = body.arguments[0].pat.node;
2187+
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = body.value.node;
21882188

2189-
if path.segments.len() == 1;
2190-
if path.segments[0].ident.as_str() == binding_ident.as_str();
2189+
if path.segments.len() == 1;
2190+
if path.segments[0].ident.as_str() == binding_ident.as_str();
21912191

2192-
then {
2193-
let msg = "called `flat_map(|x| x)` on an `Iterator`. \
2194-
This can be simplified by calling `flatten().`";
2195-
span_lint(cx, FLAT_MAP_IDENTITY, expr.span, msg);
2196-
}
2192+
then {
2193+
apply_lint("called `flat_map(|x| x)` on an `Iterator`. \
2194+
This can be simplified by calling `flatten().`");
21972195
}
2196+
}
21982197

2199-
if_chain! {
2200-
let expr = &flat_map_args[1];
2201-
2202-
if let hir::ExprKind::Path(ref qpath) = expr.node;
2198+
if_chain! {
2199+
if let hir::ExprKind::Path(ref qpath) = arg_node;
22032200

2204-
if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY);
2201+
if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY);
22052202

2206-
then {
2207-
let msg = "called `flat_map(std::convert::identity)` on an `Iterator`. \
2208-
This can be simplified by calling `flatten().`";
2209-
span_lint(cx, FLAT_MAP_IDENTITY, expr.span, msg);
2210-
}
2203+
then {
2204+
apply_lint("called `flat_map(std::convert::identity)` on an `Iterator`. \
2205+
This can be simplified by calling `flatten().`");
22112206
}
22122207
}
22132208
}

0 commit comments

Comments
 (0)