mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-09 16:37:36 +00:00
new_ret_no_self walk return type to check for self
This commit is contained in:
parent
097df8f223
commit
079f9f45b5
@ -936,13 +936,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
|||||||
if let hir::ImplItemKind::Method(_, _) = implitem.node {
|
if let hir::ImplItemKind::Method(_, _) = implitem.node {
|
||||||
let ret_ty = return_ty(cx, implitem.id);
|
let ret_ty = return_ty(cx, implitem.id);
|
||||||
|
|
||||||
// println!("ret_ty: {:?}", ret_ty);
|
// walk the return type and check for Self (this does not check associated types)
|
||||||
// println!("ret_ty.sty {:?}", ret_ty.sty);
|
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 {
|
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() {
|
for predicate in cx.tcx.predicates_of(def_id).predicates.iter() {
|
||||||
match predicate {
|
match predicate {
|
||||||
(Predicate::Projection(poly_projection_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) {
|
if name == "new" && !same_tys(cx, ret_ty, ty) {
|
||||||
span_lint(cx,
|
span_lint(cx,
|
||||||
NEW_RET_NO_SELF,
|
NEW_RET_NO_SELF,
|
||||||
|
Loading…
Reference in New Issue
Block a user