|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use clippy_utils::{get_parent_as_impl, get_trait_def_id, path_res}; |
| 3 | +use rustc_ast::BinOpKind; |
| 4 | +use rustc_hir::def::Res; |
| 5 | +use rustc_hir::def_id::{DefId, LocalDefId}; |
| 6 | +use rustc_hir::intravisit::FnKind; |
| 7 | +use rustc_hir::{Body, Expr, ExprKind, FnDecl}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_middle::ty::{self, Ty}; |
| 10 | +use rustc_session::declare_lint_pass; |
| 11 | +use rustc_span::{sym, Span}; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Checks that there isn't an infinite recursion in `PartialEq` trait |
| 16 | + /// implementation. |
| 17 | + /// |
| 18 | + /// ### Why is this bad? |
| 19 | + /// This is a hard to find infinite recursion which will crashing any code |
| 20 | + /// using it. |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// ```no_run |
| 24 | + /// enum Foo { |
| 25 | + /// A, |
| 26 | + /// B, |
| 27 | + /// } |
| 28 | + /// |
| 29 | + /// impl PartialEq for Foo { |
| 30 | + /// fn eq(&self, other: &Self) -> bool { |
| 31 | + /// self == other // bad! |
| 32 | + /// } |
| 33 | + /// } |
| 34 | + /// ``` |
| 35 | + /// Use instead: |
| 36 | + /// |
| 37 | + /// In such cases, either use `#[derive(PartialEq)]` or don't implement it. |
| 38 | + #[clippy::version = "1.76.0"] |
| 39 | + pub UNCONDITIONAL_RECURSION, |
| 40 | + suspicious, |
| 41 | + "detect unconditional recursion in some traits implementation" |
| 42 | +} |
| 43 | + |
| 44 | +declare_lint_pass!(UnconditionalRecursion => [UNCONDITIONAL_RECURSION]); |
| 45 | + |
| 46 | +fn get_ty_def_id(ty: Ty<'_>) -> Option<DefId> { |
| 47 | + match ty.peel_refs().kind() { |
| 48 | + ty::Adt(adt, _) => Some(adt.did()), |
| 49 | + ty::Foreign(def_id) => Some(*def_id), |
| 50 | + _ => None, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +fn is_local(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { |
| 55 | + matches!(path_res(cx, expr), Res::Local(_)) |
| 56 | +} |
| 57 | + |
| 58 | +impl<'tcx> LateLintPass<'tcx> for UnconditionalRecursion { |
| 59 | + #[allow(clippy::unnecessary_def_path)] |
| 60 | + fn check_fn( |
| 61 | + &mut self, |
| 62 | + cx: &LateContext<'tcx>, |
| 63 | + kind: FnKind<'tcx>, |
| 64 | + _decl: &'tcx FnDecl<'tcx>, |
| 65 | + body: &'tcx Body<'tcx>, |
| 66 | + method_span: Span, |
| 67 | + def_id: LocalDefId, |
| 68 | + ) { |
| 69 | + // We don't check code generated from (proc) macro. |
| 70 | + if method_span.from_expansion() { |
| 71 | + return; |
| 72 | + } |
| 73 | + if let FnKind::Method(name, _) = kind |
| 74 | + && let [self_arg, other_arg] = cx |
| 75 | + .tcx |
| 76 | + .instantiate_bound_regions_with_erased(cx.tcx.fn_sig(def_id).skip_binder()) |
| 77 | + .inputs() |
| 78 | + && let Some(self_arg) = get_ty_def_id(*self_arg) |
| 79 | + && let Some(other_arg) = get_ty_def_id(*other_arg) |
| 80 | + && self_arg == other_arg |
| 81 | + && let hir_id = cx.tcx.local_def_id_to_hir_id(def_id) |
| 82 | + && let Some(impl_) = get_parent_as_impl(cx.tcx, hir_id) |
| 83 | + && let Some(trait_) = impl_.of_trait |
| 84 | + && let Some(trait_def_id) = trait_.trait_def_id() |
| 85 | + && Some(trait_def_id) == get_trait_def_id(cx, &["core", "cmp", "PartialEq"]) |
| 86 | + { |
| 87 | + let to_check_op = if name.name == sym::eq { |
| 88 | + BinOpKind::Eq |
| 89 | + } else { |
| 90 | + BinOpKind::Ne |
| 91 | + }; |
| 92 | + let expr = body.value.peel_blocks(); |
| 93 | + let is_bad = match expr.kind { |
| 94 | + ExprKind::Binary(op, left, right) if op.node == to_check_op => { |
| 95 | + is_local(cx, left) && is_local(cx, right) |
| 96 | + }, |
| 97 | + ExprKind::MethodCall(segment, receiver, &[arg], _) if segment.ident.name == name.name => { |
| 98 | + if is_local(cx, receiver) |
| 99 | + && is_local(cx, &arg) |
| 100 | + && let Some(fn_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) |
| 101 | + && let Some(trait_id) = cx.tcx.trait_of_item(fn_id) |
| 102 | + && trait_id == trait_def_id |
| 103 | + { |
| 104 | + true |
| 105 | + } else { |
| 106 | + false |
| 107 | + } |
| 108 | + }, |
| 109 | + _ => false, |
| 110 | + }; |
| 111 | + if is_bad { |
| 112 | + span_lint_and_then( |
| 113 | + cx, |
| 114 | + UNCONDITIONAL_RECURSION, |
| 115 | + method_span, |
| 116 | + "function cannot return without recursing", |
| 117 | + |diag| { |
| 118 | + diag.span_note(expr.span, "recursive call site"); |
| 119 | + }, |
| 120 | + ); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments