mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 13:13:34 +00:00
[unwrap_or_default
]: skip warning when calling inside of suggested method's implementation
This commit is contained in:
parent
86d348df90
commit
983b4c3720
3 changed files with 42 additions and 5 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -318,4 +318,16 @@ fn host_effect() {
|
|||
Add::<i32>::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() {}
|
||||
|
|
|
@ -318,4 +318,16 @@ fn host_effect() {
|
|||
Add::<i32>::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() {}
|
||||
|
|
Loading…
Reference in a new issue