@@ -4,56 +4,58 @@ In 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
66- 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?
88
99## Checking if an ` expr ` is calling a specific method
1010
1111Suppose we have an ` expr ` , we can check whether it calls a specific
1212method, 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 ` :
1414
1515``` rust
1616use rustc_hir as hir;
1717use rustc_lint :: {LateContext , LateLintPass };
1818use rustc_span :: sym;
19+ use clippy_utils :: is_trait_method;
1920
2021impl <'tcx > LateLintPass <'tcx > for OurFancyMethodLint {
2122 fn check_expr (& mut self , cx : & LateContext <'tcx >, expr : & 'tcx hir :: Expr <'_ >) {
2223 // 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
2425 // Check if the name of this method is `our_fancy_method`
2526 && path . ident. name == sym! (our_fancy_method )
2627 // 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 )
3032 {
31- println! (" `expr` is a method call for `our_fancy_method`" );
33+ println! (" `expr` is a method call for `our_fancy_method`" );
3234 }
3335 }
3436}
3537```
3638
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
3840information on the pattern matching.
3941As mentioned in [ Define Lints] ( define_lints.md#lint-types ) ,
4042the ` methods ` lint type is full of pattern matching with ` Methodcall `
4143in case the reader wishes to explore more.
4244
4345Additionally, 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 ` ] .
4648
47- ## Checking if a type defines a specific method
49+ ## Checking if a ` impl ` block implements a method
4850
4951While 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.
5153
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
5557[ "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
5759anything within an ` impl ` block.
5860
5961Let us take a look at how we might check for the implementation of
@@ -65,7 +67,6 @@ use clippy_utils::return_ty;
6567use rustc_hir :: {ImplItem , ImplItemKind };
6668use rustc_lint :: {LateContext , LateLintPass };
6769use rustc_span :: symbol :: sym;
68-
6970impl <'tcx > LateLintPass <'tcx > for MyTypeImpl {
7071 fn check_impl_item (& mut self , cx : & LateContext <'tcx >, impl_item : & 'tcx ImplItem <'_ >) {
7172 // Check if item is a method/function
@@ -83,11 +84,11 @@ impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
8384}
8485```
8586
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
8889[ 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
9394[ sym ] : https://doc.rust-lang.org/stable/nightly-rustc/clippy_utils/macro.sym.html
0 commit comments