|
| 1 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 2 | +use rustc_hir::{FnSig, ImplItem, ImplItemKind}; |
| 3 | +use rustc_lint::LateContext; |
| 4 | +use rustc_middle::ty::{self, Ty}; |
| 5 | +use rustc_span::Span; |
| 6 | + |
| 7 | +use super::METHOD_WITHOUT_SELF_RELATION; |
| 8 | + |
| 9 | +/// Check if a type contains a reference to `Self` anywhere in its structure. |
| 10 | +/// This includes direct references and generic parameters. |
| 11 | +fn contains_self<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, self_ty: Ty<'tcx>) -> bool { |
| 12 | + // Direct comparison with Self type |
| 13 | + if ty == self_ty { |
| 14 | + return true; |
| 15 | + } |
| 16 | + |
| 17 | + match ty.kind() { |
| 18 | + // Check if this is a reference to Self |
| 19 | + ty::Ref(_, inner_ty, _) => contains_self(cx, *inner_ty, self_ty), |
| 20 | + |
| 21 | + // Check if this is a raw pointer to Self |
| 22 | + ty::RawPtr(inner_ty, _) => contains_self(cx, *inner_ty, self_ty), |
| 23 | + |
| 24 | + // Check generic types like Option<Self>, Vec<Self>, Result<Self, E>, etc. |
| 25 | + ty::Adt(_, args) => args.types().any(|arg_ty| contains_self(cx, arg_ty, self_ty)), |
| 26 | + |
| 27 | + // Check tuples like (Self, i32) or (String, Self) |
| 28 | + ty::Tuple(types) => types.iter().any(|ty| contains_self(cx, ty, self_ty)), |
| 29 | + |
| 30 | + // Check array types like [Self; 10] |
| 31 | + ty::Array(elem_ty, _) | ty::Slice(elem_ty) => contains_self(cx, *elem_ty, self_ty), |
| 32 | + |
| 33 | + // Check function pointer types |
| 34 | + ty::FnPtr(sig, _) => { |
| 35 | + let sig = sig.skip_binder(); |
| 36 | + sig.inputs().iter().any(|&ty| contains_self(cx, ty, self_ty)) || contains_self(cx, sig.output(), self_ty) |
| 37 | + }, |
| 38 | + |
| 39 | + // Check closures (uncommon but possible) |
| 40 | + ty::Closure(_, args) => { |
| 41 | + args.as_closure() |
| 42 | + .sig() |
| 43 | + .inputs() |
| 44 | + .skip_binder() |
| 45 | + .iter() |
| 46 | + .any(|&ty| contains_self(cx, ty, self_ty)) |
| 47 | + || contains_self(cx, args.as_closure().sig().output().skip_binder(), self_ty) |
| 48 | + }, |
| 49 | + |
| 50 | + // Check opaque types (impl Trait, async fn return types) |
| 51 | + ty::Alias(ty::AliasTyKind::Opaque, alias_ty) => { |
| 52 | + // Check the bounds of the opaque type |
| 53 | + alias_ty.args.types().any(|arg_ty| contains_self(cx, arg_ty, self_ty)) |
| 54 | + }, |
| 55 | + |
| 56 | + // Check trait objects (dyn Trait) |
| 57 | + ty::Dynamic(predicates, _) => { |
| 58 | + use rustc_middle::ty::ExistentialPredicate; |
| 59 | + // Check if any of the trait bounds reference Self |
| 60 | + predicates.iter().any(|predicate| match predicate.skip_binder() { |
| 61 | + ExistentialPredicate::Trait(trait_ref) => { |
| 62 | + trait_ref.args.types().any(|arg_ty| contains_self(cx, arg_ty, self_ty)) |
| 63 | + }, |
| 64 | + ExistentialPredicate::Projection(projection) => { |
| 65 | + projection.args.types().any(|arg_ty| contains_self(cx, arg_ty, self_ty)) |
| 66 | + || contains_self(cx, projection.term.as_type().unwrap(), self_ty) |
| 67 | + }, |
| 68 | + ExistentialPredicate::AutoTrait(_) => false, |
| 69 | + }) |
| 70 | + }, |
| 71 | + |
| 72 | + _ => false, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>, self_ty: Ty<'tcx>) { |
| 77 | + if let ImplItemKind::Fn(ref sig, _) = impl_item.kind { |
| 78 | + // Get the method signature from the type system |
| 79 | + let method_sig = cx.tcx.fn_sig(impl_item.owner_id).instantiate_identity(); |
| 80 | + let method_sig = method_sig.skip_binder(); |
| 81 | + |
| 82 | + // Check if there's a self parameter (self, &self, &mut self, self: Arc<Self>, etc.) |
| 83 | + if has_self_parameter(sig) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + // Check all input parameters for Self references |
| 88 | + for ¶m_ty in method_sig.inputs().iter() { |
| 89 | + if contains_self(cx, param_ty, self_ty) { |
| 90 | + return; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Check return type for Self references |
| 95 | + let return_ty = method_sig.output(); |
| 96 | + if contains_self(cx, return_ty, self_ty) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + // If we reach here, the method has no relationship to Self |
| 101 | + emit_lint(cx, impl_item.span, impl_item.ident.name.as_str()); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/// Check if the function signature has an explicit self parameter |
| 106 | +fn has_self_parameter(sig: &FnSig<'_>) -> bool { |
| 107 | + sig.decl.implicit_self.has_implicit_self() |
| 108 | +} |
| 109 | + |
| 110 | +fn emit_lint(cx: &LateContext<'_>, span: Span, method_name: &str) { |
| 111 | + span_lint_and_help( |
| 112 | + cx, |
| 113 | + METHOD_WITHOUT_SELF_RELATION, |
| 114 | + span, |
| 115 | + format!("method `{method_name}` has no relationship to `Self`"), |
| 116 | + None, |
| 117 | + "consider making this a standalone function instead of a method", |
| 118 | + ); |
| 119 | +} |
0 commit comments