@@ -4,56 +4,58 @@ In some scenarios we might want to check for methods when developing
4
4
a lint. There are two kinds of questions that we might be curious about:
5
5
6
6
- Invocation: Does an expression call a specific method?
7
- - Definition: Does the type `Ty` of an expression define a method?
7
+ - Definition: Does an `impl` define a method?
8
8
9
9
## Checking if an `expr` is calling a specific method
10
10
11
11
Suppose we have an `expr`, we can check whether it calls a specific
12
12
method, e.g. `our_fancy_method`, by performing a pattern match on
13
- the [ExprKind] that we can access from `expr.kind`:
13
+ the [` ExprKind` ] that we can access from `expr.kind`:
14
14
15
15
```rust
16
16
use rustc_hir as hir;
17
17
use rustc_lint::{LateContext, LateLintPass};
18
18
use rustc_span::sym;
19
+ use clippy_utils::is_trait_method;
19
20
20
21
impl<'tcx> LateLintPass<'tcx> for OurFancyMethodLint {
21
22
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
22
23
// Check our expr is calling a method with pattern matching
23
- if let hir::ExprKind::MethodCall(path, _, [_self_arg , ..]) = &expr.kind
24
+ if let hir::ExprKind::MethodCall(path, _, [self_arg , ..]) = &expr.kind
24
25
// Check if the name of this method is `our_fancy_method`
25
26
&& path.ident.name == sym!(our_fancy_method)
26
27
// 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.
28
+ // (It's necessary if we want to check that method is specifically belonging to a specific trait,
29
+ // for example, a `map` method could belong to user-defined trait instead of to `Iterator`)
30
+ // See the next section for more information.
31
+ && is_trait_method(cx, self_arg, sym::OurFancyTrait)
30
32
{
31
- println!("`expr` is a method call for `our_fancy_method`");
33
+ println!("`expr` is a method call for `our_fancy_method`");
32
34
}
33
35
}
34
36
}
35
37
```
36
38
37
- Take a closer look at the `ExprKind` enum variant [MethodCall] for more
39
+ Take a closer look at the `ExprKind` enum variant [` MethodCall` ] for more
38
40
information on the pattern matching.
39
41
As mentioned in [Define Lints](define_lints.md#lint-types),
40
42
the `methods` lint type is full of pattern matching with `Methodcall`
41
43
in case the reader wishes to explore more.
42
44
43
45
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
+ an input `our_fancy_method` into a `Symbol` and compare that symbol to the [` ident` ][Ident]'s name in the [` PathSegment` ]
47
+ in the [` MethodCall` ].
46
48
47
- ## Checking if a type defines a specific method
49
+ ## Checking if a `impl` block implements a method
48
50
49
51
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.
52
+ other times we want to know if our `Ty` defines a method.
51
53
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
54
+ To check if our `impl` block defines a method `our_fancy_method`,
55
+ we will utilize the [` check_impl_item` ] method that is available
56
+ in our beloved [` LateLintPass` ] (for more information, refer to the
55
57
["Lint Passes"](lint_passes.md) chapter in Clippy book).
56
- This method provides us with an [ImplItem] struct, which represents
58
+ This method provides us with an [` ImplItem` ] struct, which represents
57
59
anything within an `impl` block.
58
60
59
61
Let us take a look at how we might check for the implementation of
@@ -65,7 +67,6 @@ use clippy_utils::return_ty;
65
67
use rustc_hir::{ImplItem, ImplItemKind};
66
68
use rustc_lint::{LateContext, LateLintPass};
67
69
use rustc_span::symbol::sym;
68
-
69
70
impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
70
71
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {
71
72
// Check if item is a method/function
@@ -83,11 +84,11 @@ impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
83
84
}
84
85
```
85
86
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
87
+ [` check_impl_item` ]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html#method.check_impl_item
88
+ [` ExprKind` ]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html
88
89
[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
90
+ [` ImplItem` ]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_hir/hir/struct.ImplItem.html
91
+ [` LateLintPass` ]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html
92
+ [` MethodCall` ]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html#variant.MethodCall
93
+ [` PathSegment` ]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/struct.PathSegment.html
93
94
[sym]: https://doc.rust-lang.org/stable/nightly-rustc/clippy_utils/macro.sym.html
0 commit comments