Skip to content

Commit b6f1998

Browse files
committed
Fix ICE when calling implements_trait
1 parent 3e012b0 commit b6f1998

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

clippy_lints/src/ambiguous_method_names.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use clippy_utils::ty::implements_trait;
44
use hir::{ImplItem, Item};
55
use rustc_hir as hir;
66
use rustc_lint::{LateContext, LateLintPass};
7+
use rustc_middle::ty;
78
use rustc_session::{declare_tool_lint, impl_lint_pass};
8-
use rustc_span::def_id::LocalDefId;
9+
use rustc_span::def_id::DefId;
910
use rustc_span::symbol::Ident;
1011

1112
declare_clippy_lint! {
@@ -72,9 +73,9 @@ declare_clippy_lint! {
7273
#[derive(Clone)]
7374
pub struct AmbiguousMethodNames {
7475
// Keeps track of trait methods
75-
trait_methods: Vec<(LocalDefId, Ident)>,
76+
trait_methods: Vec<(DefId, Ident)>,
7677
// Keeps track of inherent methods
77-
inherent_methods: Vec<(LocalDefId, Ident)>,
78+
inherent_methods: Vec<(DefId, Ident)>,
7879
}
7980

8081
impl AmbiguousMethodNames {
@@ -94,7 +95,8 @@ impl<'tcx> LateLintPass<'tcx> for AmbiguousMethodNames {
9495
if let hir::ItemKind::Trait(_, _, _, _, tr_items) = item.kind {
9596
for tr_item in tr_items {
9697
if let hir::AssocItemKind::Fn { .. } = tr_item.kind {
97-
self.trait_methods.push((item.owner_id.def_id, tr_item.ident))
98+
self.trait_methods
99+
.push((item.owner_id.def_id.to_def_id(), tr_item.ident));
98100
}
99101
}
100102
}
@@ -106,16 +108,25 @@ impl<'tcx> LateLintPass<'tcx> for AmbiguousMethodNames {
106108
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(impl_item.owner_id.def_id);
107109
if !is_trait_impl_item(cx, hir_id) {
108110
let struct_id = cx.tcx.hir().get_parent_item(hir_id);
109-
self.inherent_methods.push((struct_id.def_id, impl_item.ident))
111+
self.inherent_methods
112+
.push((struct_id.def_id.to_def_id(), impl_item.ident));
110113
}
111114
}
112115
}
113116

114117
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
115118
for (r#trait, ident) in &self.trait_methods {
116119
for (r#struct, inherent_ident) in &self.inherent_methods {
117-
let struct_ty = cx.tcx.type_of(r#struct.to_def_id()).skip_binder();
118-
if implements_trait(cx, struct_ty, r#trait.to_def_id(), &[]) && ident.name == inherent_ident.name {
120+
let struct_ty = cx.tcx.type_of(r#struct).skip_binder();
121+
let trait_ref = ty::TraitRef::identity(cx.tcx, *r#trait);
122+
let args = &trait_ref.args[1..];
123+
if implements_trait(
124+
cx,
125+
struct_ty,
126+
*r#trait,
127+
&args[..cx.tcx.generics_of(r#trait).params.len() - 1],
128+
) && ident.name == inherent_ident.name
129+
{
119130
span_lint_and_note(
120131
cx,
121132
AMBIGUOUS_METHOD_NAMES,

tests/ui/ambiguous_method_names.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ trait Another {
1515
fn another(&self);
1616
}
1717

18+
trait G<T, U> {
19+
fn g(&self, a: T, b: U);
20+
}
21+
1822
struct A;
1923

2024
impl A {

tests/ui/ambiguous_method_names.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: ambiguous inherent method name
2-
--> $DIR/ambiguous_method_names.rs:21:8
2+
--> $DIR/ambiguous_method_names.rs:25:8
33
|
44
LL | fn ambiguous(&self) {}
55
| ^^^^^^^^^
@@ -13,7 +13,7 @@ LL | fn ambiguous(&self);
1313
= help: to override `-D warnings` add `#[allow(clippy::ambiguous_method_names)]`
1414

1515
error: ambiguous inherent method name
16-
--> $DIR/ambiguous_method_names.rs:40:8
16+
--> $DIR/ambiguous_method_names.rs:44:8
1717
|
1818
LL | fn ambiguous(&self) {}
1919
| ^^^^^^^^^
@@ -25,7 +25,7 @@ LL | fn ambiguous(&self);
2525
| ^^^^^^^^^
2626

2727
error: ambiguous inherent method name
28-
--> $DIR/ambiguous_method_names.rs:22:8
28+
--> $DIR/ambiguous_method_names.rs:26:8
2929
|
3030
LL | fn also_ambiguous(&self) {}
3131
| ^^^^^^^^^^^^^^
@@ -37,7 +37,7 @@ LL | fn also_ambiguous(&self);
3737
| ^^^^^^^^^^^^^^
3838

3939
error: ambiguous inherent method name
40-
--> $DIR/ambiguous_method_names.rs:41:8
40+
--> $DIR/ambiguous_method_names.rs:45:8
4141
|
4242
LL | fn also_ambiguous(&self) {}
4343
| ^^^^^^^^^^^^^^
@@ -49,7 +49,7 @@ LL | fn also_ambiguous(&self);
4949
| ^^^^^^^^^^^^^^
5050

5151
error: ambiguous inherent method name
52-
--> $DIR/ambiguous_method_names.rs:23:8
52+
--> $DIR/ambiguous_method_names.rs:27:8
5353
|
5454
LL | fn ambiguous_default(&self) {}
5555
| ^^^^^^^^^^^^^^^^^
@@ -61,7 +61,7 @@ LL | fn ambiguous_default(&self) {}
6161
| ^^^^^^^^^^^^^^^^^
6262

6363
error: ambiguous inherent method name
64-
--> $DIR/ambiguous_method_names.rs:42:8
64+
--> $DIR/ambiguous_method_names.rs:46:8
6565
|
6666
LL | fn ambiguous_default(&self) {}
6767
| ^^^^^^^^^^^^^^^^^
@@ -73,7 +73,7 @@ LL | fn ambiguous_default(&self) {}
7373
| ^^^^^^^^^^^^^^^^^
7474

7575
error: ambiguous inherent method name
76-
--> $DIR/ambiguous_method_names.rs:25:8
76+
--> $DIR/ambiguous_method_names.rs:29:8
7777
|
7878
LL | fn another(&self) {}
7979
| ^^^^^^^
@@ -85,7 +85,7 @@ LL | fn another(&self);
8585
| ^^^^^^^
8686

8787
error: ambiguous inherent method name
88-
--> $DIR/ambiguous_method_names.rs:43:8
88+
--> $DIR/ambiguous_method_names.rs:47:8
8989
|
9090
LL | fn another(&self) {}
9191
| ^^^^^^^

0 commit comments

Comments
 (0)