Skip to content

Method checking: error in the example? #15938

@ada4a

Description

@ada4a

I tried following the example in https://doc.rust-lang.org/clippy/development/method_checking.html#checking-if-an-expr-is-calling-a-specific-method (I'm still not 100% comfortable with the new API from #15682, so I need to look things up every time 😅):

// Check our expr is calling a method with pattern matching
if let hir::ExprKind::MethodCall(path, _, [self_arg, ..], _) = &expr.kind
// Check if the name of this method is `our_fancy_method`
&& path.ident.name == sym::our_fancy_method
// We can check the type of the self argument whenever necessary.
// (It's necessary if we want to check that method is specifically belonging to a specific trait,
// for example, a `map` method could belong to user-defined trait instead of to `Iterator`)
// See the next section for more information.
&& cx.ty_based_def(self_arg).opt_parent(cx).is_diag_item(cx, sym::OurFancyTrait)
{
println!("`expr` is a method call for `our_fancy_method`");
}

but kept getting a panic on (my version of) cx.ty_based_def(self_arg). Then I looked at that PR again for places where is_inherent_method_call was removed, and found this one:
https://github.com/rust-lang/rust-clippy/pull/15682/files#diff-9d3f62135af85bbb426d0e358666790f639f6777c8dc504a7527e2bc7f830fb1L740

Apparently there we pass to method call itself instead of the receiver. Plugging that into my code, everything finally worked.

So I'm pretty sure the example is wrong, but I'm not completely sure how to best fix it -- self_arg isn't needed anymore, but the comments kind of make a reference to it -- should it be removed anyway?

cc @Jarcho as the author of the PR

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions