Skip to content

Commit cdf65c8

Browse files
committed
Attempt to check method call sites for ambiguity
1 parent 35e0f6c commit cdf65c8

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

clippy_lints/src/ambiguous_method_calls.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl<'tcx> LateLintPass<'tcx> for AmbiguousMethodCalls {
8888

8989
// Check methods in trait impls and struct impls
9090
if let FnKind::Method(ident, _) = kind {
91+
// FIXME: also keep track of the Ty of the struct for call site checking
9192
insert_method(is_trait_impl, ident);
9293

9394
if has_ambiguous_name(ident) {
@@ -110,8 +111,24 @@ impl<'tcx> LateLintPass<'tcx> for AmbiguousMethodCalls {
110111
);
111112
}
112113
}
114+
}
113115

114-
// TODO: check method call sites as well
116+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'_>) {
117+
if let rustc_hir::ExprKind::MethodCall(path, _receiver, _, call_span) = &expr.kind
118+
{
119+
let struct_methods = struct_methods().lock().unwrap();
120+
if struct_methods.contains_key(&path.ident.name) {
121+
// FIXME: only report after checking receiver's Ty
122+
span_lint_and_help(
123+
cx,
124+
AMBIGUOUS_METHOD_CALLS,
125+
*call_span,
126+
"ambiguous struct method call",
127+
None,
128+
"consider renaming the struct impl's method or explicitly qualifying the call site",
129+
);
130+
}
131+
}
115132
}
116133
}
117134

tests/ui/ambiguous_method_calls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(dead_code)]
12
#![warn(clippy::ambiguous_method_calls)]
23

34
trait MyTrait {
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
error: ambiguous method call
2-
--> $DIR/ambiguous_method_calls.rs:21:5
1+
error: ambiguous trait method name
2+
--> $DIR/ambiguous_method_calls.rs:22:8
33
|
4-
LL | / fn ambiguous(&self) {
5-
LL | | println!("trait impl");
6-
LL | | }
7-
| |_____^
4+
LL | fn ambiguous(&self) {
5+
| ^^^^^^^^^
86
|
9-
= help: consider renaming the struct impl's method
107
= note: `-D clippy::ambiguous-method-calls` implied by `-D warnings`
118
= help: to override `-D warnings` add `#[allow(clippy::ambiguous_method_calls)]`
129

13-
error: ambiguous method call
14-
--> $DIR/ambiguous_method_calls.rs:11:8
10+
error: ambiguous struct method name
11+
--> $DIR/ambiguous_method_calls.rs:12:8
1512
|
1613
LL | fn ambiguous(&self) {
1714
| ^^^^^^^^^
1815
|
1916
= help: consider renaming the struct impl's method
2017

21-
error: aborting due to 2 previous errors
18+
error: ambiguous struct method call
19+
--> $DIR/ambiguous_method_calls.rs:34:10
20+
|
21+
LL | data.ambiguous();
22+
| ^^^^^^^^^^^
23+
|
24+
= help: consider renaming the struct impl's method or explicitly qualifying the call site
25+
26+
error: aborting due to 3 previous errors
2227

0 commit comments

Comments
 (0)