diff options
| author | bors <bors@rust-lang.org> | 2024-07-12 14:37:10 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-07-12 14:37:10 +0000 |
| commit | 0cbbee1e6e7aff3829953895320cb5fa109702f0 (patch) | |
| tree | 644c543065ca61d2a3db12384cbcf4352ac21c8a | |
| parent | 51a1cf07876d36a5008f9dc07cd36ba9d412a5b7 (diff) | |
| parent | 983b4c37203e841c1a362e1cf3a16ef388269499 (diff) | |
| download | rust-0cbbee1e6e7aff3829953895320cb5fa109702f0.tar.gz rust-0cbbee1e6e7aff3829953895320cb5fa109702f0.zip | |
Auto merge of #13090 - J-ZhengLi:issue9790, r=blyxyas
[`unwrap_or_default`]: skip warning when calling inside of suggested method's implementation fixes: #10228 --- changelog: [`unwrap_or_default`]: skip warning when calling inside of suggested method's implementation
| -rw-r--r-- | clippy_lints/src/methods/or_fun_call.rs | 23 | ||||
| -rw-r--r-- | tests/ui/or_fun_call.fixed | 12 | ||||
| -rw-r--r-- | tests/ui/or_fun_call.rs | 12 |
3 files changed, 42 insertions, 5 deletions
diff --git a/clippy_lints/src/methods/or_fun_call.rs b/clippy_lints/src/methods/or_fun_call.rs index 583e04fb4b1..baa60454419 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 c76f7a81843..35015592971 100644 --- a/tests/ui/or_fun_call.fixed +++ b/tests/ui/or_fun_call.fixed @@ -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() {} diff --git a/tests/ui/or_fun_call.rs b/tests/ui/or_fun_call.rs index 97cf496d3ac..3dcf657d743 100644 --- a/tests/ui/or_fun_call.rs +++ b/tests/ui/or_fun_call.rs @@ -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() {} |
