From be983fbf52c581bfa51e8b5dd95fb9f599a67639 Mon Sep 17 00:00:00 2001 From: Devon Hollowood Date: Sun, 7 Oct 2018 17:07:10 -0700 Subject: [PATCH] Fix items_after_statements for sub-functions --- clippy_lints/src/methods/mod.rs | 40 ++++++++++++++++---------------- clippy_lints/src/utils/higher.rs | 16 ++++++------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index e0d858bd270..01cd6f25832 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -1081,18 +1081,6 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span: arg: &hir::Expr, span: Span, ) { - if name != "expect" { - return; - } - - let self_type = cx.tables.expr_ty(self_expr); - let known_types = &[&paths::OPTION, &paths::RESULT]; - - // if not a known type, return early - if known_types.iter().all(|&k| !match_type(cx, self_type, k)) { - return; - } - fn is_call(node: &hir::ExprKind) -> bool { match node { hir::ExprKind::AddrOf(_, expr) => { @@ -1107,6 +1095,18 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span: } } + if name != "expect" { + return; + } + + let self_type = cx.tables.expr_ty(self_expr); + let known_types = &[&paths::OPTION, &paths::RESULT]; + + // if not a known type, return early + if known_types.iter().all(|&k| !match_type(cx, self_type, k)) { + return; + } + if !is_call(&arg.node) { return; } @@ -1338,14 +1338,6 @@ fn lint_iter_cloned_collect(cx: &LateContext<'_, '_>, expr: &hir::Expr, iter_arg } fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args: &[hir::Expr]) { - // Check that this is a call to Iterator::fold rather than just some function called fold - if !match_trait_method(cx, expr, &paths::ITERATOR) { - return; - } - - assert!(fold_args.len() == 3, - "Expected fold_args to have three entries - the receiver, the initial value and the closure"); - fn check_fold_with_op( cx: &LateContext<'_, '_>, fold_args: &[hir::Expr], @@ -1402,6 +1394,14 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args: } } + // Check that this is a call to Iterator::fold rather than just some function called fold + if !match_trait_method(cx, expr, &paths::ITERATOR) { + return; + } + + assert!(fold_args.len() == 3, + "Expected fold_args to have three entries - the receiver, the initial value and the closure"); + // Check if the first argument to .fold is a suitable literal match fold_args[1].node { hir::ExprKind::Lit(ref lit) => { diff --git a/clippy_lints/src/utils/higher.rs b/clippy_lints/src/utils/higher.rs index cfedad49f31..88c875c3102 100644 --- a/clippy_lints/src/utils/higher.rs +++ b/clippy_lints/src/utils/higher.rs @@ -46,6 +46,14 @@ pub struct Range<'a> { /// Higher a `hir` range to something similar to `ast::ExprKind::Range`. pub fn range<'a, 'b, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'b hir::Expr) -> Option> { + /// Find the field named `name` in the field. Always return `Some` for + /// convenience. + fn get_field<'a>(name: &str, fields: &'a [hir::Field]) -> Option<&'a hir::Expr> { + let expr = &fields.iter().find(|field| field.ident.name == name)?.expr; + + Some(expr) + } + let def_path = match cx.tables.expr_ty(expr).sty { ty::Adt(def, _) => cx.tcx.def_path(def.did), @@ -75,14 +83,6 @@ pub fn range<'a, 'b, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'b hir::Expr) -> O return None; } - /// Find the field named `name` in the field. Always return `Some` for - /// convenience. - fn get_field<'a>(name: &str, fields: &'a [hir::Field]) -> Option<&'a hir::Expr> { - let expr = &fields.iter().find(|field| field.ident.name == name)?.expr; - - Some(expr) - } - // The range syntax is expanded to literal paths starting with `core` or `std` // depending on // `#[no_std]`. Testing both instead of resolving the paths.