Skip to content

Commit 6cc5efb

Browse files
blyxyasnahuakang
andcommitted
Add "Method Checking"
Co-authored-by: Nahua <[email protected]>
1 parent 0049816 commit 6cc5efb

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-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)
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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]
45+
in the [MethodCall].
46+
47+
## Checking if a type defines a specific method
48+
49+
While sometimes we want to check whether a method is being called or not,
50+
other times we want to know if our type `Ty` defines a method.
51+
52+
To check if our type defines a method called `our_fancy_method`,
53+
we will utilize the [check_impl_item] method that is available
54+
in our beloved [LateLintPass] (for more information, refer to the
55+
["Lint Passes"](lint_passes.md) chapter in Clippy book).
56+
This method provides us with an [ImplItem] struct, which represents
57+
anything within an `impl` block.
58+
59+
Let us take a look at how we might check for the implementation of
60+
`our_fancy_method` on a type:
61+
62+
```rust
63+
use clippy_utils::ty::is_type_diagnostic_item;
64+
use clippy_utils::return_ty;
65+
use rustc_hir::{ImplItem, ImplItemKind};
66+
use rustc_lint::{LateContext, LateLintPass};
67+
use rustc_span::symbol::sym;
68+
69+
impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
70+
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {
71+
// Check if item is a method/function
72+
if let ImplItemKind::Fn(ref signature, _) = impl_item.kind
73+
// Check the method is named `our_fancy_method`
74+
&& impl_item.ident.name == sym!(our_fancy_method)
75+
// We can also check it has a parameter `self`
76+
&& signature.decl.implicit_self.has_implicit_self()
77+
// We can go even further and even check if its return type is `String`
78+
&& is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id), sym::String)
79+
{
80+
println!("`our_fancy_method` is implemented!");
81+
}
82+
}
83+
}
84+
```
85+
86+
[check_impl_item]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html#method.check_impl_item
87+
[ExprKind]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html
88+
[Ident]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_span/symbol/struct.Ident.html
89+
[ImplItem]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_hir/hir/struct.ImplItem.html
90+
[LateLintPass]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html
91+
[MethodCall]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html#variant.MethodCall
92+
[PathSegment]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/struct.PathSegment.html
93+
[sym]: https://doc.rust-lang.org/stable/nightly-rustc/clippy_utils/macro.sym.html

0 commit comments

Comments
 (0)