Skip to content

Commit b7caa1b

Browse files
committed
Do not call TyCtxt::type_of() on a trait
1 parent da7b678 commit b7caa1b

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

clippy_lints/src/methods/useless_asref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str,
7979
applicability,
8080
);
8181
}
82-
} else if let Some(impl_id) = cx.tcx.opt_parent(def_id)
82+
} else if let Some(impl_id) = cx.tcx.impl_of_method(def_id)
8383
&& let Some(adt) = cx.tcx.type_of(impl_id).instantiate_identity().ty_adt_def()
84-
&& (cx.tcx.lang_items().option_type() == Some(adt.did()) || cx.tcx.is_diagnostic_item(sym::Result, adt.did()))
84+
&& matches!(cx.tcx.get_diagnostic_name(adt.did()), Some(sym::Option | sym::Result))
8585
{
8686
let rcv_ty = cx.typeck_results().expr_ty(recvr).peel_refs();
8787
let res_ty = cx.typeck_results().expr_ty(expr).peel_refs();

tests/ui/useless_asref.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ impl Issue12357 {
248248
}
249249
}
250250

251+
fn issue_14828() {
252+
pub trait T {
253+
fn as_ref(&self) {}
254+
}
255+
256+
impl T for () {}
257+
258+
().as_ref();
259+
}
260+
251261
fn main() {
252262
not_ok();
253263
ok();

tests/ui/useless_asref.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ impl Issue12357 {
248248
}
249249
}
250250

251+
fn issue_14828() {
252+
pub trait T {
253+
fn as_ref(&self) {}
254+
}
255+
256+
impl T for () {}
257+
258+
().as_ref();
259+
}
260+
251261
fn main() {
252262
not_ok();
253263
ok();

0 commit comments

Comments
 (0)