Skip to content

Commit ff7435d

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

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

clippy_lints/src/methods/useless_asref.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ 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 cx.tcx.trait_of_item(def_id).is_none()
83+
&& let Some(impl_id) = cx.tcx.opt_parent(def_id)
8384
&& let Some(adt) = cx.tcx.type_of(impl_id).instantiate_identity().ty_adt_def()
8485
&& (cx.tcx.lang_items().option_type() == Some(adt.did()) || cx.tcx.is_diagnostic_item(sym::Result, adt.did()))
8586
{

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)