Method Checking
In some scenarios we might want to check for methods when developing a lint. There are two kinds of questions that we might be curious about:
- Invocation: Does an expression call a specific method?
- Definition: Does an
impl
define a method?
Checking if an expr
is calling a specific method
Suppose we have an expr
, we can check whether it calls a specific
method, e.g. our_fancy_method
, by performing a pattern match on
the ExprKind
that we can access from expr.kind
:
Take a closer look at the ExprKind
enum variant MethodCall
for more
information on the pattern matching. As mentioned in Define
Lints, the methods
lint type is full of pattern
matching with MethodCall
in case the reader wishes to explore more.
Checking if a impl
block implements a method
While sometimes we want to check whether a method is being called or not, other
times we want to know if our Ty
defines a method.
To check if our impl
block defines a method our_fancy_method
, we will
utilize the check_impl_item
method that is available in our beloved
LateLintPass
(for more information, refer to the "Lint
Passes" chapter in the Clippy book). This method provides us
with an ImplItem
struct, which represents anything within an impl
block.
Let us take a look at how we might check for the implementation of
our_fancy_method
on a type: