diff --git a/clippy_lints/src/methods/or_fun_call.rs b/clippy_lints/src/methods/or_fun_call.rs index 583e04fb4..baa604544 100644 --- a/clippy_lints/src/methods/or_fun_call.rs +++ b/clippy_lints/src/methods/or_fun_call.rs @@ -70,18 +70,31 @@ pub(super) fn check<'tcx>( }; let receiver_ty = cx.typeck_results().expr_ty_adjusted(receiver).peel_refs(); - let has_suggested_method = receiver_ty.ty_adt_def().is_some_and(|adt_def| { + let Some(suggested_method_def_id) = receiver_ty.ty_adt_def().and_then(|adt_def| { cx.tcx .inherent_impls(adt_def.did()) .into_iter() .flatten() .flat_map(|impl_id| cx.tcx.associated_items(impl_id).filter_by_name_unhygienic(sugg)) - .any(|assoc| { - assoc.fn_has_self_parameter + .find_map(|assoc| { + if assoc.fn_has_self_parameter && cx.tcx.fn_sig(assoc.def_id).skip_binder().inputs().skip_binder().len() == 1 + { + Some(assoc.def_id) + } else { + None + } }) - }); - if !has_suggested_method { + }) else { + return false; + }; + let in_sugg_method_implementation = { + matches!( + suggested_method_def_id.as_local(), + Some(local_def_id) if local_def_id == cx.tcx.hir().get_parent_item(receiver.hir_id).def_id + ) + }; + if in_sugg_method_implementation { return false; } diff --git a/tests/ui/or_fun_call.fixed b/tests/ui/or_fun_call.fixed index c76f7a818..350155929 100644 --- a/tests/ui/or_fun_call.fixed +++ b/tests/ui/or_fun_call.fixed @@ -318,4 +318,16 @@ fn host_effect() { Add::::add(1, 1).add(i32::MIN); } +mod issue_10228 { + struct Entry; + + impl Entry { + fn or_insert(self, _default: i32) {} + fn or_default(self) { + // Don't lint, suggested code is an infinite recursion + self.or_insert(Default::default()) + } + } +} + fn main() {} diff --git a/tests/ui/or_fun_call.rs b/tests/ui/or_fun_call.rs index 97cf496d3..3dcf657d7 100644 --- a/tests/ui/or_fun_call.rs +++ b/tests/ui/or_fun_call.rs @@ -318,4 +318,16 @@ fn host_effect() { Add::::add(1, 1).add(i32::MIN); } +mod issue_10228 { + struct Entry; + + impl Entry { + fn or_insert(self, _default: i32) {} + fn or_default(self) { + // Don't lint, suggested code is an infinite recursion + self.or_insert(Default::default()) + } + } +} + fn main() {}