about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Macleod <alex@macleod.io>2025-05-18 12:22:57 +0000
committerGitHub <noreply@github.com>2025-05-18 12:22:57 +0000
commit618ccd7f033a37e4a1996db94a7179a466cb1829 (patch)
tree142416e4d13535b7d6660d67f7c1614e35fe914b
parent15d47d74bf2dc099414af0964ae2341fa0a5c4cf (diff)
parentb7caa1b3e0642a4c304a4d8240d4d1c3252b7c0c (diff)
downloadrust-618ccd7f033a37e4a1996db94a7179a466cb1829.tar.gz
rust-618ccd7f033a37e4a1996db94a7179a466cb1829.zip
Do not call `TyCtxt::type_of()` on a trait (#14830)
changelog: [`useless_as_ref`]: fix ICE on trait method

Fixes rust-lang/rust-clippy#14828
-rw-r--r--clippy_lints/src/methods/useless_asref.rs4
-rw-r--r--tests/ui/useless_asref.fixed10
-rw-r--r--tests/ui/useless_asref.rs10
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();