From 111f0229da738ad4374c56c216b08c7cc403a0ef Mon Sep 17 00:00:00 2001 From: blyxyas Date: Thu, 13 Apr 2023 21:57:47 +0200 Subject: Add "Method Checking" Co-authored-by: Nahua --- book/src/development/method_checking.md | 93 +++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 book/src/development/method_checking.md (limited to 'book/src/development/method_checking.md') diff --git a/book/src/development/method_checking.md b/book/src/development/method_checking.md new file mode 100644 index 00000000000..604fcf193df --- /dev/null +++ b/book/src/development/method_checking.md @@ -0,0 +1,93 @@ +# Method Checking + +In some scenarios we might want to check for methods when developing +a lint. There are two kinds of questions that we might be curious about: + +- Invocation: Does an expression call a specific method? +- Definition: Does the type `Ty` of an expression define a method? + +## Checking if an `expr` is calling a specific method + +Suppose we have an `expr`, we can check whether it calls a specific +method, e.g. `our_fancy_method`, by performing a pattern match on +the [ExprKind] that we can access from `expr.kind`: + +```rust +use rustc_hir as hir; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_span::sym; + +impl<'tcx> LateLintPass<'tcx> for OurFancyMethodLint { + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { + // Check our expr is calling a method with pattern matching + if let hir::ExprKind::MethodCall(path, _, [_self_arg, ..]) = &expr.kind + // Check if the name of this method is `our_fancy_method` + && path.ident.name == sym!(our_fancy_method) + // We can check the type of the self argument whenever necessary. + // (It's necessary if we want to check that method is specifically belonging to a specific trait, + // for example, a `map` method could belong to user-defined trait instead of to `Iterator`) + // See the "Type Checking" chapter of the Clippy book for more information. + { + println!("`expr` is a method call for `our_fancy_method`"); + } + } +} +``` + +Take a closer look at the `ExprKind` enum variant [MethodCall] for more +information on the pattern matching. +As mentioned in [Define Lints](define_lints.md#lint-types), +the `methods` lint type is full of pattern matching with `Methodcall` +in case the reader wishes to explore more. + +Additionally, we use the [`clippy_utils::sym!`][sym] macro to conveniently convert +an input `our_fancy_method` into a `Symbol` and compare that symbol to the [ident][Ident]'s name in the [PathSegment] +in the [MethodCall]. + +## Checking if a type defines a specific method + +While sometimes we want to check whether a method is being called or not, +other times we want to know if our type `Ty` defines a method. + +To check if our type defines a method called `our_fancy_method`, +we will utilize the [check_impl_item] method that is available +in our beloved [LateLintPass] (for more information, refer to the +["Lint Passes"](lint_passes.md) chapter in Clippy book). +This method provides us with an [ImplItem] struct, which represents +anything within an `impl` block. + +Let us take a look at how we might check for the implementation of +`our_fancy_method` on a type: + +```rust +use clippy_utils::ty::is_type_diagnostic_item; +use clippy_utils::return_ty; +use rustc_hir::{ImplItem, ImplItemKind}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_span::symbol::sym; + +impl<'tcx> LateLintPass<'tcx> for MyTypeImpl { + fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) { + // Check if item is a method/function + if let ImplItemKind::Fn(ref signature, _) = impl_item.kind + // Check the method is named `our_fancy_method` + && impl_item.ident.name == sym!(our_fancy_method) + // We can also check it has a parameter `self` + && signature.decl.implicit_self.has_implicit_self() + // We can go even further and even check if its return type is `String` + && is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id), sym::String) + { + println!("`our_fancy_method` is implemented!"); + } + } +} +``` + +[check_impl_item]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html#method.check_impl_item +[ExprKind]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html +[Ident]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_span/symbol/struct.Ident.html +[ImplItem]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_hir/hir/struct.ImplItem.html +[LateLintPass]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html +[MethodCall]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html#variant.MethodCall +[PathSegment]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/struct.PathSegment.html +[sym]: https://doc.rust-lang.org/stable/nightly-rustc/clippy_utils/macro.sym.html -- cgit 1.4.1-3-g733a5 From 9e4a2d7fcab0a32a8137f52e4b3a1442604ba61c Mon Sep 17 00:00:00 2001 From: blyxyas Date: Mon, 17 Apr 2023 23:08:44 +0200 Subject: Fixes based on reviews --- book/src/development/method_checking.md | 47 +++++++++++++++++---------------- 1 file changed, 24 insertions(+), 23 deletions(-) (limited to 'book/src/development/method_checking.md') diff --git a/book/src/development/method_checking.md b/book/src/development/method_checking.md index 604fcf193df..c93af6f21d2 100644 --- a/book/src/development/method_checking.md +++ b/book/src/development/method_checking.md @@ -4,56 +4,58 @@ In some scenarios we might want to check for methods when developing a lint. There are two kinds of questions that we might be curious about: - Invocation: Does an expression call a specific method? -- Definition: Does the type `Ty` of an expression define a method? +- Definition: Does an `impl` define a method? ## Checking if an `expr` is calling a specific method Suppose we have an `expr`, we can check whether it calls a specific method, e.g. `our_fancy_method`, by performing a pattern match on -the [ExprKind] that we can access from `expr.kind`: +the [`ExprKind`] that we can access from `expr.kind`: ```rust use rustc_hir as hir; use rustc_lint::{LateContext, LateLintPass}; use rustc_span::sym; +use clippy_utils::is_trait_method; impl<'tcx> LateLintPass<'tcx> for OurFancyMethodLint { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { // Check our expr is calling a method with pattern matching - if let hir::ExprKind::MethodCall(path, _, [_self_arg, ..]) = &expr.kind + if let hir::ExprKind::MethodCall(path, _, [self_arg, ..]) = &expr.kind // Check if the name of this method is `our_fancy_method` && path.ident.name == sym!(our_fancy_method) // We can check the type of the self argument whenever necessary. - // (It's necessary if we want to check that method is specifically belonging to a specific trait, - // for example, a `map` method could belong to user-defined trait instead of to `Iterator`) - // See the "Type Checking" chapter of the Clippy book for more information. + // (It's necessary if we want to check that method is specifically belonging to a specific trait, + // for example, a `map` method could belong to user-defined trait instead of to `Iterator`) + // See the next section for more information. + && is_trait_method(cx, self_arg, sym::OurFancyTrait) { - println!("`expr` is a method call for `our_fancy_method`"); + println!("`expr` is a method call for `our_fancy_method`"); } } } ``` -Take a closer look at the `ExprKind` enum variant [MethodCall] for more +Take a closer look at the `ExprKind` enum variant [`MethodCall`] for more information on the pattern matching. As mentioned in [Define Lints](define_lints.md#lint-types), the `methods` lint type is full of pattern matching with `Methodcall` in case the reader wishes to explore more. Additionally, we use the [`clippy_utils::sym!`][sym] macro to conveniently convert -an input `our_fancy_method` into a `Symbol` and compare that symbol to the [ident][Ident]'s name in the [PathSegment] -in the [MethodCall]. +an input `our_fancy_method` into a `Symbol` and compare that symbol to the [`ident`][Ident]'s name in the [`PathSegment`] +in the [`MethodCall`]. -## Checking if a type defines a specific method +## Checking if a `impl` block implements a method While sometimes we want to check whether a method is being called or not, -other times we want to know if our type `Ty` defines a method. +other times we want to know if our `Ty` defines a method. -To check if our type defines a method called `our_fancy_method`, -we will utilize the [check_impl_item] method that is available -in our beloved [LateLintPass] (for more information, refer to the +To check if our `impl` block defines a method `our_fancy_method`, +we will utilize the [`check_impl_item`] method that is available +in our beloved [`LateLintPass`] (for more information, refer to the ["Lint Passes"](lint_passes.md) chapter in Clippy book). -This method provides us with an [ImplItem] struct, which represents +This method provides us with an [`ImplItem`] struct, which represents anything within an `impl` block. Let us take a look at how we might check for the implementation of @@ -65,7 +67,6 @@ use clippy_utils::return_ty; use rustc_hir::{ImplItem, ImplItemKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_span::symbol::sym; - impl<'tcx> LateLintPass<'tcx> for MyTypeImpl { fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) { // Check if item is a method/function @@ -83,11 +84,11 @@ impl<'tcx> LateLintPass<'tcx> for MyTypeImpl { } ``` -[check_impl_item]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html#method.check_impl_item -[ExprKind]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html +[`check_impl_item`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html#method.check_impl_item +[`ExprKind`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html [Ident]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_span/symbol/struct.Ident.html -[ImplItem]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_hir/hir/struct.ImplItem.html -[LateLintPass]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html -[MethodCall]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html#variant.MethodCall -[PathSegment]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/struct.PathSegment.html +[`ImplItem`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_hir/hir/struct.ImplItem.html +[`LateLintPass`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html +[`MethodCall`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html#variant.MethodCall +[`PathSegment`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/struct.PathSegment.html [sym]: https://doc.rust-lang.org/stable/nightly-rustc/clippy_utils/macro.sym.html -- cgit 1.4.1-3-g733a5 From e8d79b86454ffd7c18ce65c98dda5df51633eedd Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Wed, 16 Aug 2023 14:44:26 +0200 Subject: Formatting --- book/src/development/method_checking.md | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) (limited to 'book/src/development/method_checking.md') diff --git a/book/src/development/method_checking.md b/book/src/development/method_checking.md index c93af6f21d2..f061e6f5b37 100644 --- a/book/src/development/method_checking.md +++ b/book/src/development/method_checking.md @@ -37,26 +37,24 @@ impl<'tcx> LateLintPass<'tcx> for OurFancyMethodLint { ``` Take a closer look at the `ExprKind` enum variant [`MethodCall`] for more -information on the pattern matching. -As mentioned in [Define Lints](define_lints.md#lint-types), -the `methods` lint type is full of pattern matching with `Methodcall` -in case the reader wishes to explore more. +information on the pattern matching. As mentioned in [Define +Lints](define_lints.md#lint-types), the `methods` lint type is full of pattern +matching with `MethodCall` in case the reader wishes to explore more. -Additionally, we use the [`clippy_utils::sym!`][sym] macro to conveniently convert -an input `our_fancy_method` into a `Symbol` and compare that symbol to the [`ident`][Ident]'s name in the [`PathSegment`] -in the [`MethodCall`]. +Additionally, we use the [`clippy_utils::sym!`][sym] macro to conveniently +convert an input `our_fancy_method` into a `Symbol` and compare that symbol to +the [`Ident`]'s name in the [`PathSegment`] in the [`MethodCall`]. ## Checking if a `impl` block implements a method -While sometimes we want to check whether a method is being called or not, -other times we want to know if our `Ty` defines a method. +While sometimes we want to check whether a method is being called or not, other +times we want to know if our `Ty` defines a method. -To check if our `impl` block defines a method `our_fancy_method`, -we will utilize the [`check_impl_item`] method that is available -in our beloved [`LateLintPass`] (for more information, refer to the -["Lint Passes"](lint_passes.md) chapter in Clippy book). -This method provides us with an [`ImplItem`] struct, which represents -anything within an `impl` block. +To check if our `impl` block defines a method `our_fancy_method`, we will +utilize the [`check_impl_item`] method that is available in our beloved +[`LateLintPass`] (for more information, refer to the ["Lint +Passes"](lint_passes.md) chapter in the Clippy book). This method provides us +with an [`ImplItem`] struct, which represents anything within an `impl` block. Let us take a look at how we might check for the implementation of `our_fancy_method` on a type: @@ -67,6 +65,7 @@ use clippy_utils::return_ty; use rustc_hir::{ImplItem, ImplItemKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_span::symbol::sym; + impl<'tcx> LateLintPass<'tcx> for MyTypeImpl { fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) { // Check if item is a method/function @@ -86,7 +85,7 @@ impl<'tcx> LateLintPass<'tcx> for MyTypeImpl { [`check_impl_item`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html#method.check_impl_item [`ExprKind`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html -[Ident]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_span/symbol/struct.Ident.html +[`Ident`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_span/symbol/struct.Ident.html [`ImplItem`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_hir/hir/struct.ImplItem.html [`LateLintPass`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html [`MethodCall`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html#variant.MethodCall -- cgit 1.4.1-3-g733a5 From 1dbc8dd0cf51ddff5a585d128f376d26ee619196 Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Wed, 16 Aug 2023 14:54:47 +0200 Subject: Fix define_lints->defining_lints typo in link --- book/src/development/method_checking.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'book/src/development/method_checking.md') diff --git a/book/src/development/method_checking.md b/book/src/development/method_checking.md index f061e6f5b37..56d1be37519 100644 --- a/book/src/development/method_checking.md +++ b/book/src/development/method_checking.md @@ -38,7 +38,7 @@ impl<'tcx> LateLintPass<'tcx> for OurFancyMethodLint { Take a closer look at the `ExprKind` enum variant [`MethodCall`] for more information on the pattern matching. As mentioned in [Define -Lints](define_lints.md#lint-types), the `methods` lint type is full of pattern +Lints](defining_lints.md#lint-types), the `methods` lint type is full of pattern matching with `MethodCall` in case the reader wishes to explore more. Additionally, we use the [`clippy_utils::sym!`][sym] macro to conveniently -- cgit 1.4.1-3-g733a5 From 979e2971897e8aa4a02d914a6336b5291a0839c7 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Sun, 13 Oct 2024 18:44:21 +0100 Subject: Update book --- book/src/development/common_tools_writing_lints.md | 4 ++-- book/src/development/method_checking.md | 15 ++++----------- 2 files changed, 6 insertions(+), 13 deletions(-) (limited to 'book/src/development/method_checking.md') diff --git a/book/src/development/common_tools_writing_lints.md b/book/src/development/common_tools_writing_lints.md index 09171d86a20..77910917963 100644 --- a/book/src/development/common_tools_writing_lints.md +++ b/book/src/development/common_tools_writing_lints.md @@ -68,7 +68,7 @@ impl<'tcx> LateLintPass<'tcx> for MyStructLint { // Check our expr is calling a method if let hir::ExprKind::MethodCall(path, _, _self_arg, ..) = &expr.kind // Check the name of this method is `some_method` - && path.ident.name == sym!(some_method) + && path.ident.name.as_str() == "some_method" // Optionally, check the type of the self argument. // - See "Checking for a specific type" { @@ -167,7 +167,7 @@ impl<'tcx> LateLintPass<'tcx> for MyTypeImpl { // Check if item is a method/function if let ImplItemKind::Fn(ref signature, _) = impl_item.kind // Check the method is named `some_method` - && impl_item.ident.name == sym!(some_method) + && impl_item.ident.name.as_str() == "some_method" // We can also check it has a parameter `self` && signature.decl.implicit_self.has_implicit_self() // We can go further and even check if its return type is `String` diff --git a/book/src/development/method_checking.md b/book/src/development/method_checking.md index 56d1be37519..9c5d4b516db 100644 --- a/book/src/development/method_checking.md +++ b/book/src/development/method_checking.md @@ -3,8 +3,8 @@ In some scenarios we might want to check for methods when developing a lint. There are two kinds of questions that we might be curious about: -- Invocation: Does an expression call a specific method? -- Definition: Does an `impl` define a method? +- Invocation: Does an expression call a specific method? +- Definition: Does an `impl` define a method? ## Checking if an `expr` is calling a specific method @@ -23,7 +23,7 @@ impl<'tcx> LateLintPass<'tcx> for OurFancyMethodLint { // Check our expr is calling a method with pattern matching if let hir::ExprKind::MethodCall(path, _, [self_arg, ..]) = &expr.kind // Check if the name of this method is `our_fancy_method` - && path.ident.name == sym!(our_fancy_method) + && path.ident.name.as_str() == "our_fancy_method" // We can check the type of the self argument whenever necessary. // (It's necessary if we want to check that method is specifically belonging to a specific trait, // 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 Lints](defining_lints.md#lint-types), the `methods` lint type is full of pattern matching with `MethodCall` in case the reader wishes to explore more. -Additionally, we use the [`clippy_utils::sym!`][sym] macro to conveniently -convert an input `our_fancy_method` into a `Symbol` and compare that symbol to -the [`Ident`]'s name in the [`PathSegment`] in the [`MethodCall`]. - ## Checking if a `impl` block implements a method 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 { // Check if item is a method/function if let ImplItemKind::Fn(ref signature, _) = impl_item.kind // Check the method is named `our_fancy_method` - && impl_item.ident.name == sym!(our_fancy_method) + && impl_item.ident.name.as_str() == "our_fancy_method" // We can also check it has a parameter `self` && signature.decl.implicit_self.has_implicit_self() // We can go even further and even check if its return type is `String` @@ -85,9 +81,6 @@ impl<'tcx> LateLintPass<'tcx> for MyTypeImpl { [`check_impl_item`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html#method.check_impl_item [`ExprKind`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html -[`Ident`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_span/symbol/struct.Ident.html [`ImplItem`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_hir/hir/struct.ImplItem.html [`LateLintPass`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html [`MethodCall`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html#variant.MethodCall -[`PathSegment`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/struct.PathSegment.html -[sym]: https://doc.rust-lang.org/stable/nightly-rustc/clippy_utils/macro.sym.html -- cgit 1.4.1-3-g733a5 From 09022bbd4515ba12edb4188dac221b1b068ee981 Mon Sep 17 00:00:00 2001 From: lapla-cogito Date: Sat, 28 Dec 2024 22:41:29 +0900 Subject: fix arguments of ExprKind::MethodCall --- book/src/development/method_checking.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'book/src/development/method_checking.md') diff --git a/book/src/development/method_checking.md b/book/src/development/method_checking.md index 9c5d4b516db..b3126024b99 100644 --- a/book/src/development/method_checking.md +++ b/book/src/development/method_checking.md @@ -21,7 +21,7 @@ use clippy_utils::is_trait_method; impl<'tcx> LateLintPass<'tcx> for OurFancyMethodLint { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { // Check our expr is calling a method with pattern matching - if let hir::ExprKind::MethodCall(path, _, [self_arg, ..]) = &expr.kind + if let hir::ExprKind::MethodCall(path, _, [self_arg, ..], _) = &expr.kind // Check if the name of this method is `our_fancy_method` && path.ident.name.as_str() == "our_fancy_method" // We can check the type of the self argument whenever necessary. -- cgit 1.4.1-3-g733a5