From 2551bd8924e17b8d10f8331d11cf0e469558b5f1 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 25 Sep 2017 19:44:50 -0700 Subject: [PATCH] Reduce cyclomatic complexity of types::check_ty --- clippy_lints/src/types.rs | 96 ++++++++++++++++++------------------ clippy_lints/src/use_self.rs | 7 ++- 2 files changed, 55 insertions(+), 48 deletions(-) diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index 50683d1fb40..9d57527faee 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -184,16 +184,16 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool) { check_ty(cx, ty, is_local); for ty in p.segments .iter() - .filter_map(|ref seg| seg.parameters.as_ref()) - .flat_map(|ref params| params.types.iter()) + .filter_map(|seg| seg.parameters.as_ref()) + .flat_map(|params| params.types.iter()) { check_ty(cx, ty, is_local); } }, QPath::Resolved(None, ref p) => for ty in p.segments .iter() - .filter_map(|ref seg| seg.parameters.as_ref()) - .flat_map(|ref params| params.types.iter()) + .filter_map(|seg| seg.parameters.as_ref()) + .flat_map(|params| params.types.iter()) { check_ty(cx, ty, is_local); }, @@ -207,49 +207,7 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool) { }, } }, - TyRptr(ref lt, MutTy { ref ty, ref mutbl }) => { - match ty.node { - TyPath(ref qpath) => { - let hir_id = cx.tcx.hir.node_to_hir_id(ty.id); - let def = cx.tables.qpath_def(qpath, hir_id); - if_let_chain! {[ - let Some(def_id) = opt_def_id(def), - Some(def_id) == cx.tcx.lang_items().owned_box(), - let QPath::Resolved(None, ref path) = *qpath, - let [ref bx] = *path.segments, - let Some(ref params) = bx.parameters, - !params.parenthesized, - let [ref inner] = *params.types - ], { - if is_any_trait(inner) { - // Ignore `Box` types, see #1884 for details. - return; - } - - let ltopt = if lt.is_elided() { - "".to_owned() - } else { - format!("{} ", lt.name.name().as_str()) - }; - let mutopt = if *mutbl == Mutability::MutMutable { - "mut " - } else { - "" - }; - span_lint_and_sugg(cx, - BORROWED_BOX, - ast_ty.span, - "you seem to be trying to use `&Box`. Consider using just `&T`", - "try", - format!("&{}{}{}", ltopt, mutopt, &snippet(cx, inner.span, "..")) - ); - return; // don't recurse into the type - }}; - check_ty(cx, ty, is_local); - }, - _ => check_ty(cx, ty, is_local), - } - }, + TyRptr(ref lt, ref mut_ty) => check_ty_rptr(cx, ast_ty, is_local, lt, mut_ty), // recurse TySlice(ref ty) | TyArray(ref ty, _) | TyPtr(MutTy { ref ty, .. }) => check_ty(cx, ty, is_local), TyTup(ref tys) => for ty in tys { @@ -259,6 +217,50 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool) { } } +fn check_ty_rptr(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool, lt: &Lifetime, mut_ty: &MutTy) { + match mut_ty.ty.node { + TyPath(ref qpath) => { + let hir_id = cx.tcx.hir.node_to_hir_id(mut_ty.ty.id); + let def = cx.tables.qpath_def(qpath, hir_id); + if_let_chain! {[ + let Some(def_id) = opt_def_id(def), + Some(def_id) == cx.tcx.lang_items().owned_box(), + let QPath::Resolved(None, ref path) = *qpath, + let [ref bx] = *path.segments, + let Some(ref params) = bx.parameters, + !params.parenthesized, + let [ref inner] = *params.types + ], { + if is_any_trait(inner) { + // Ignore `Box` types, see #1884 for details. + return; + } + + let ltopt = if lt.is_elided() { + "".to_owned() + } else { + format!("{} ", lt.name.name().as_str()) + }; + let mutopt = if mut_ty.mutbl == Mutability::MutMutable { + "mut " + } else { + "" + }; + span_lint_and_sugg(cx, + BORROWED_BOX, + ast_ty.span, + "you seem to be trying to use `&Box`. Consider using just `&T`", + "try", + format!("&{}{}{}", ltopt, mutopt, &snippet(cx, inner.span, "..")) + ); + return; // don't recurse into the type + }}; + check_ty(cx, &mut_ty.ty, is_local); + }, + _ => check_ty(cx, &mut_ty.ty, is_local), + } +} + // Returns true if given type is `Any` trait. fn is_any_trait(t: &hir::Ty) -> bool { if_let_chain! {[ diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index d4166f6a2bd..946df625cb6 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -59,7 +59,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf { let Ty_::TyPath(QPath::Resolved(_, ref item_path)) = item_type.node, ], { let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).parameters; - if parameters.is_none() { + let should_check = if let Some(ref params) = *parameters { + !params.parenthesized && params.lifetimes.len() == 0 + } else { + true + }; + if should_check { let visitor = &mut UseSelfVisitor { item_path: item_path, cx: cx,