Skip to content

Commit

Permalink
Fix items_after_statements for sub-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
devonhollowood committed Oct 8, 2018
1 parent 6528749 commit be983fb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
40 changes: 20 additions & 20 deletions clippy_lints/src/methods/mod.rs
Expand Up @@ -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) => {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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) => {
Expand Down
16 changes: 8 additions & 8 deletions clippy_lints/src/utils/higher.rs
Expand Up @@ -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<Range<'b>> {
/// 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),
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit be983fb

Please sign in to comment.