Skip to content

Commit 079f9f4

Browse files
committed
new_ret_no_self walk return type to check for self
1 parent 097df8f commit 079f9f4

File tree

1 file changed

+6
-18
lines changed
  • clippy_lints/src/methods

1 file changed

+6
-18
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -936,13 +936,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
936936
if let hir::ImplItemKind::Method(_, _) = implitem.node {
937937
let ret_ty = return_ty(cx, implitem.id);
938938

939-
// println!("ret_ty: {:?}", ret_ty);
940-
// println!("ret_ty.sty {:?}", ret_ty.sty);
939+
// walk the return type and check for Self (this does not check associated types)
940+
for inner_type in ret_ty.walk() {
941+
if same_tys(cx, ty, inner_type) { return; }
942+
}
941943

942-
// if return type is impl trait
944+
// if return type is impl trait, check the associated types
943945
if let TyKind::Opaque(def_id, _) = ret_ty.sty {
944946

945-
// then one of the associated types must be Self
947+
// one of the associated types must be Self
946948
for predicate in cx.tcx.predicates_of(def_id).predicates.iter() {
947949
match predicate {
948950
(Predicate::Projection(poly_projection_predicate), _) => {
@@ -958,20 +960,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
958960
}
959961
}
960962

961-
// if return type is tuple
962-
if let TyKind::Tuple(list) = ret_ty.sty {
963-
// then at least one of the types in the tuple must be Self
964-
for ret_type in list {
965-
if same_tys(cx, ty, ret_type) { return; }
966-
}
967-
}
968-
969-
// if return type is mutable pointer
970-
if let TyKind::RawPtr(ty::TypeAndMut{ty: ret_type, ..}) = ret_ty.sty {
971-
// then the pointer must point to Self
972-
if same_tys(cx, ty, ret_type) { return; }
973-
}
974-
975963
if name == "new" && !same_tys(cx, ret_ty, ty) {
976964
span_lint(cx,
977965
NEW_RET_NO_SELF,

0 commit comments

Comments
 (0)