Skip to content

Commit 1055bce

Browse files
blyxyasnahuakang
andcommitted
Add "Method Checking"
Co-authored-by: Nahua <[email protected]>
1 parent 4904d75 commit 1055bce

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

book/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [Basics](development/basics.md)
1515
- [Adding Lints](development/adding_lints.md)
1616
- [Type Checking](development/type_checking.md)
17+
- [Method Checking](development/method_checking.md)
1718
- [Common Tools](development/common_tools_writing_lints.md)
1819
- [Infrastructure](development/infrastructure/README.md)
1920
- [Syncing changes between Clippy and rust-lang/rust](development/infrastructure/sync.md)
+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Method Checking
2+
3+
In some scenarios we might want to check for methods when developing
4+
a lint. There are two kinds of questions that we might be curious about:
5+
6+
- Invocation: Does an expression call a specific method?
7+
- Definition: Does the type `Ty` of an expression define a method?
8+
9+
## Checking if an `expr` is calling a specific method
10+
11+
Suppose we have an `expr`, we can check whether it calls a specific
12+
method, e.g. `our_fancy_method`, by performing a pattern match on
13+
the [ExprKind] that we can access from `expr.kind`:
14+
15+
```rust
16+
use rustc_hir as hir;
17+
use rustc_lint::{LateContext, LateLintPass};
18+
use rustc_span::sym;
19+
20+
impl<'tcx> LateLintPass<'tcx> for OurFancyMethodLint {
21+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
22+
// Check our expr is calling a method with pattern matching
23+
if let hir::ExprKind::MethodCall(path, _, [_self_arg, ..]) = &expr.kind
24+
// Check if the name of this method is `our_fancy_method`
25+
&& path.ident.name == sym!(our_fancy_method)
26+
// We can check the type of the self argument whenever necessary.
27+
// (It's necessary if we want to check that method is specifically belonging to a specific trait,
28+
// for example, a `map` method could belong to user-defined trait instead of to `Iterator`)
29+
// See the "Type Checking" chapter of the Clippy book for more information.
30+
{
31+
println!("`expr` is a method call for `our_fancy_method`");
32+
}
33+
}
34+
}
35+
```
36+
37+
Take a closer look at the `ExprKind` enum variant [MethodCall] for more
38+
information on the pattern matching.
39+
As mentioned in [Define Lints](define_lints.md#lint-types),
40+
the `methods` lint type is full of pattern matching with `Methodcall`
41+
in case the reader wishes to explore more.
42+
43+
Additionally, we use the [`clippy_utils::sym!`][sym] macro to conveniently convert
44+
an input `our_fancy_method` into a `Symbol` and compare that symbol to the [ident][Ident]'s name in the [PathSegment] in the [MethodCall].
45+
46+
## Checking if a type defines a specific method
47+
48+
While sometimes we want to check whether a method is being called or not,
49+
other times we want to know if our type `Ty` defines a method.
50+
51+
To check if our type defines a method called `our_fancy_method`,
52+
we will utilize the [check_impl_item] method that is available
53+
in our beloved [LateLintPass] (for more information, refer to the
54+
["Lint Passes"](lint_passes.md) chapter in Clippy book).
55+
This method provides us with an [ImplItem] struct, which represents
56+
anything within an `impl` block.
57+
58+
Let us take a look at how we might check for the implementation of
59+
`our_fancy_method` on a type:
60+
61+
```rust
62+
use clippy_utils::ty::is_type_diagnostic_item;
63+
use clippy_utils::return_ty;
64+
use rustc_hir::{ImplItem, ImplItemKind};
65+
use rustc_lint::{LateContext, LateLintPass};
66+
use rustc_span::symbol::sym;
67+
68+
impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
69+
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {
70+
// Check if item is a method/function
71+
if let ImplItemKind::Fn(ref signature, _) = impl_item.kind
72+
// Check the method is named `our_fancy_method`
73+
&& impl_item.ident.name == sym!(our_fancy_method)
74+
// We can also check it has a parameter `self`
75+
&& signature.decl.implicit_self.has_implicit_self()
76+
// We can go even further and even check if its return type is `String`
77+
&& is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id), sym::String)
78+
{
79+
println!("`our_fancy_method` is implemented!");
80+
}
81+
}
82+
}
83+
```
84+
85+
[check_impl_item]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html#method.check_impl_item
86+
[ExprKind]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html
87+
[Ident]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_span/symbol/struct.Ident.html
88+
[ImplItem]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_hir/hir/struct.ImplItem.html
89+
[LateLintPass]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html
90+
[MethodCall]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html#variant.MethodCall
91+
[PathSegment]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/struct.PathSegment.html
92+
[sym]: https://doc.rust-lang.org/stable/nightly-rustc/clippy_utils/macro.sym.html

0 commit comments

Comments
 (0)