33In some scenarios we might want to check for methods when developing
44a lint. There are two kinds of questions that we might be curious about:
55
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?
88
99## Checking if an ` expr ` is calling a specific method
1010
@@ -23,7 +23,7 @@ impl<'tcx> LateLintPass<'tcx> for OurFancyMethodLint {
2323 // Check our expr is calling a method with pattern matching
2424 if let hir :: ExprKind :: MethodCall (path , _ , [self_arg , .. ]) = & expr . kind
2525 // 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"
2727 // We can check the type of the self argument whenever necessary.
2828 // (It's necessary if we want to check that method is specifically belonging to a specific trait,
2929 // 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
4141Lints] ( defining_lints.md#lint-types ) , the ` methods ` lint type is full of pattern
4242matching with ` MethodCall ` in case the reader wishes to explore more.
4343
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-
4844## Checking if a ` impl ` block implements a method
4945
5046While sometimes we want to check whether a method is being called or not, other
@@ -71,7 +67,7 @@ impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
7167 // Check if item is a method/function
7268 if let ImplItemKind :: Fn (ref signature , _ ) = impl_item . kind
7369 // 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"
7571 // We can also check it has a parameter `self`
7672 && signature . decl. implicit_self. has_implicit_self ()
7773 // We can go even further and even check if its return type is `String`
@@ -85,9 +81,6 @@ impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
8581
8682[ `check_impl_item` ] : https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html#method.check_impl_item
8783[ `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
8984[ `ImplItem` ] : https://doc.rust-lang.org/stable/nightly-rustc/rustc_hir/hir/struct.ImplItem.html
9085[ `LateLintPass` ] : https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html
9186[ `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