Skip to content

Implement flat_map lint #4231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 14, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor if_chain
Co-authored-by: Philipp Krones <hello@philkrones.com>
  • Loading branch information
jeremystucki and flip1995 committed Aug 11, 2019
commit 5fd7d44f36ae530c4cb1ccfda08f028918e53ea6
49 changes: 25 additions & 24 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2174,38 +2174,39 @@ fn lint_flat_map_identity<'a, 'tcx>(
if match_trait_method(cx, expr, &paths::ITERATOR);

if flat_map_args.len() == 2;
if let hir::ExprKind::Closure(_, _, body_id, _, _) = flat_map_args[1].node;
let body = cx.tcx.hir().body(body_id);

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

if path.segments.len() == 1;
if path.segments[0].ident.as_str() == binding_ident.as_str();

then {
let msg = "called `flat_map(|x| x)` on an `Iterator`. \
This can be simplified by calling `flatten().`";
span_lint(cx, FLAT_MAP_IDENTITY, expr.span, msg);
}
}
if_chain! {
if let hir::ExprKind::Closure(_, _, body_id, _, _) = flat_map_args[1].node;
let body = cx.tcx.hir().body(body_id);

if_chain! {
if match_trait_method(cx, expr, &paths::ITERATOR);
if body.arguments.len() == 1;
if let hir::PatKind::Binding(_, _, binding_ident, _) = body.arguments[0].pat.node;
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = body.value.node;

if flat_map_args.len() == 2;
if path.segments.len() == 1;
if path.segments[0].ident.as_str() == binding_ident.as_str();

let expr = &flat_map_args[1];
then {
let msg = "called `flat_map(|x| x)` on an `Iterator`. \
This can be simplified by calling `flatten().`";
span_lint(cx, FLAT_MAP_IDENTITY, expr.span, msg);
}
}

if let hir::ExprKind::Path(ref qpath) = expr.node;
if_chain! {
let expr = &flat_map_args[1];

if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY);
if let hir::ExprKind::Path(ref qpath) = expr.node;

then {
let msg = "called `flat_map(std::convert::identity)` on an `Iterator`. \
This can be simplified by calling `flatten().`";
span_lint(cx, FLAT_MAP_IDENTITY, expr.span, msg);
if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY);

then {
let msg = "called `flat_map(std::convert::identity)` on an `Iterator`. \
This can be simplified by calling `flatten().`";
span_lint(cx, FLAT_MAP_IDENTITY, expr.span, msg);
}
}
}
}
}
Expand Down