3
3
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
- - Invocation: Does an expression call a specific method?
7
- - Definition: Does an ` impl ` define a method?
6
+ - Invocation: Does an expression call a specific method?
7
+ - Definition: Does an ` impl ` define a method?
8
8
9
9
## Checking if an ` expr ` is calling a specific method
10
10
@@ -23,7 +23,7 @@ impl<'tcx> LateLintPass<'tcx> for OurFancyMethodLint {
23
23
// Check our expr is calling a method with pattern matching
24
24
if let hir :: ExprKind :: MethodCall (path , _ , [self_arg , .. ]) = & expr . kind
25
25
// Check if the name of this method is `our_fancy_method`
26
- && path . ident. name == sym! ( our_fancy_method )
26
+ && path . ident. name. as_str () == " our_fancy_method"
27
27
// We can check the type of the self argument whenever necessary.
28
28
// (It's necessary if we want to check that method is specifically belonging to a specific trait,
29
29
// for example, a `map` method could belong to user-defined trait instead of to `Iterator`)
@@ -41,10 +41,6 @@ information on the pattern matching. As mentioned in [Define
41
41
Lints] ( defining_lints.md#lint-types ) , the ` methods ` lint type is full of pattern
42
42
matching with ` MethodCall ` in case the reader wishes to explore more.
43
43
44
- Additionally, we use the [ ` clippy_utils::sym! ` ] [ sym ] macro to conveniently
45
- convert an input ` our_fancy_method ` into a ` Symbol ` and compare that symbol to
46
- the [ ` Ident ` ] 's name in the [ ` PathSegment ` ] in the [ ` MethodCall ` ] .
47
-
48
44
## Checking if a ` impl ` block implements a method
49
45
50
46
While sometimes we want to check whether a method is being called or not, other
@@ -71,7 +67,7 @@ impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
71
67
// Check if item is a method/function
72
68
if let ImplItemKind :: Fn (ref signature , _ ) = impl_item . kind
73
69
// Check the method is named `our_fancy_method`
74
- && impl_item . ident. name == sym! ( our_fancy_method )
70
+ && impl_item . ident. name. as_str () == " our_fancy_method"
75
71
// We can also check it has a parameter `self`
76
72
&& signature . decl. implicit_self. has_implicit_self ()
77
73
// We can go even further and even check if its return type is `String`
@@ -85,9 +81,6 @@ impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
85
81
86
82
[ `check_impl_item` ] : https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html#method.check_impl_item
87
83
[ `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
84
[ `ImplItem` ] : https://doc.rust-lang.org/stable/nightly-rustc/rustc_hir/hir/struct.ImplItem.html
90
85
[ `LateLintPass` ] : https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html
91
86
[ `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