diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2025-05-17 21:33:26 +0200 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2025-05-18 09:38:19 +0200 |
| commit | b7caa1b3e0642a4c304a4d8240d4d1c3252b7c0c (patch) | |
| tree | 08c66d77ca658211d248de3d9aff0cf8ae1fad8a | |
| parent | da7b678992dd65dbd644bfe30ba61a9a0d2c695c (diff) | |
| download | rust-b7caa1b3e0642a4c304a4d8240d4d1c3252b7c0c.tar.gz rust-b7caa1b3e0642a4c304a4d8240d4d1c3252b7c0c.zip | |
Do not call `TyCtxt::type_of()` on a trait
| -rw-r--r-- | clippy_lints/src/methods/useless_asref.rs | 4 | ||||
| -rw-r--r-- | tests/ui/useless_asref.fixed | 10 | ||||
| -rw-r--r-- | tests/ui/useless_asref.rs | 10 |
3 files changed, 22 insertions, 2 deletions
diff --git a/clippy_lints/src/methods/useless_asref.rs b/clippy_lints/src/methods/useless_asref.rs index 17e2620d9dd..56d2c407c05 100644 --- a/clippy_lints/src/methods/useless_asref.rs +++ b/clippy_lints/src/methods/useless_asref.rs @@ -79,9 +79,9 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, applicability, ); } - } else if let Some(impl_id) = cx.tcx.opt_parent(def_id) + } else if let Some(impl_id) = cx.tcx.impl_of_method(def_id) && let Some(adt) = cx.tcx.type_of(impl_id).instantiate_identity().ty_adt_def() - && (cx.tcx.lang_items().option_type() == Some(adt.did()) || cx.tcx.is_diagnostic_item(sym::Result, adt.did())) + && matches!(cx.tcx.get_diagnostic_name(adt.did()), Some(sym::Option | sym::Result)) { let rcv_ty = cx.typeck_results().expr_ty(recvr).peel_refs(); let res_ty = cx.typeck_results().expr_ty(expr).peel_refs(); diff --git a/tests/ui/useless_asref.fixed b/tests/ui/useless_asref.fixed index 8c1f948fb58..3c3ea5a736d 100644 --- a/tests/ui/useless_asref.fixed +++ b/tests/ui/useless_asref.fixed @@ -248,6 +248,16 @@ impl Issue12357 { } } +fn issue_14828() { + pub trait T { + fn as_ref(&self) {} + } + + impl T for () {} + + ().as_ref(); +} + fn main() { not_ok(); ok(); diff --git a/tests/ui/useless_asref.rs b/tests/ui/useless_asref.rs index d9db2d4f559..c173dd67715 100644 --- a/tests/ui/useless_asref.rs +++ b/tests/ui/useless_asref.rs @@ -248,6 +248,16 @@ impl Issue12357 { } } +fn issue_14828() { + pub trait T { + fn as_ref(&self) {} + } + + impl T for () {} + + ().as_ref(); +} + fn main() { not_ok(); ok(); |
