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
This commit is contained in:
Alex Macleod
2025-05-18 12:22:57 +00:00
committed by GitHub
3 changed files with 22 additions and 2 deletions
+2 -2
View File
@@ -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();
+10
View File
@@ -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();
+10
View File
@@ -248,6 +248,16 @@ fn g(&self) {
}
}
fn issue_14828() {
pub trait T {
fn as_ref(&self) {}
}
impl T for () {}
().as_ref();
}
fn main() {
not_ok();
ok();