From 079f9f45b5a44a0feaf60d56576758dd7b0c9fdd Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Fri, 19 Oct 2018 17:54:25 -0700 Subject: [PATCH] new_ret_no_self walk return type to check for self --- clippy_lints/src/methods/mod.rs | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 0992067636ed..f0810c906eff 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -936,13 +936,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { if let hir::ImplItemKind::Method(_, _) = implitem.node { let ret_ty = return_ty(cx, implitem.id); -// println!("ret_ty: {:?}", ret_ty); -// println!("ret_ty.sty {:?}", ret_ty.sty); + // walk the return type and check for Self (this does not check associated types) + for inner_type in ret_ty.walk() { + if same_tys(cx, ty, inner_type) { return; } + } - // if return type is impl trait + // if return type is impl trait, check the associated types if let TyKind::Opaque(def_id, _) = ret_ty.sty { - // then one of the associated types must be Self + // one of the associated types must be Self for predicate in cx.tcx.predicates_of(def_id).predicates.iter() { match predicate { (Predicate::Projection(poly_projection_predicate), _) => { @@ -958,20 +960,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { } } - // if return type is tuple - if let TyKind::Tuple(list) = ret_ty.sty { - // then at least one of the types in the tuple must be Self - for ret_type in list { - if same_tys(cx, ty, ret_type) { return; } - } - } - - // if return type is mutable pointer - if let TyKind::RawPtr(ty::TypeAndMut{ty: ret_type, ..}) = ret_ty.sty { - // then the pointer must point to Self - if same_tys(cx, ty, ret_type) { return; } - } - if name == "new" && !same_tys(cx, ret_ty, ty) { span_lint(cx, NEW_RET_NO_SELF,