about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-08-26 10:04:12 +0000
committerbors <bors@rust-lang.org>2021-08-26 10:04:12 +0000
commit387dbd7da9f027d9f1402118195a5510e044b319 (patch)
tree4b76ceee1d30cb0a2ec889b31689a09b71cd78ae
parentde80188c642d266fcbaab9e0e09bebf9777f41e1 (diff)
parentf0cb8a789acfac8a25e60e4e4e791e4c51ecf1ee (diff)
downloadrust-387dbd7da9f027d9f1402118195a5510e044b319.tar.gz
rust-387dbd7da9f027d9f1402118195a5510e044b319.zip
Auto merge of #7583 - dswij:match-trait-docs, r=xFrednet
Tweak common tool docs on type-implement-trait check

See https://github.com/rust-lang/rust-clippy/pull/7562#issuecomment-898240963.

changelog: none
-rw-r--r--doc/common_tools_writing_lints.md26
1 files changed, 16 insertions, 10 deletions
diff --git a/doc/common_tools_writing_lints.md b/doc/common_tools_writing_lints.md
index 0a85f650011..5f45951090e 100644
--- a/doc/common_tools_writing_lints.md
+++ b/doc/common_tools_writing_lints.md
@@ -75,20 +75,21 @@ impl LateLintPass<'_> for MyStructLint {
 
 # Checking if a type implements a specific trait
 
-There are two ways to do this, depending if the target trait is part of lang items.
+There are three ways to do this, depending on if the target trait has a diagnostic item, lang item or neither.
 
 ```rust
-use clippy_utils::{implements_trait, match_trait_method, paths};
+use clippy_utils::{implements_trait, is_trait_method, match_trait_method, paths};
+use rustc_span::symbol::sym;
 
 impl LateLintPass<'_> for MyStructLint {
     fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
-        // 1. Using expression and Clippy's convenient method
-        // we use `match_trait_method` function from Clippy's toolbox
-        if match_trait_method(cx, expr, &paths::INTO) {
-            // `expr` implements `Into` trait
+        // 1. Using diagnostic items with the expression
+        // we use `is_trait_method` function from Clippy's utils
+        if is_trait_method(cx, expr, sym::Iterator) {
+            // method call in `expr` belongs to `Iterator` trait
         }
 
-        // 2. Using type context `TyCtxt`
+        // 2. Using lang items with the expression type
         let ty = cx.typeck_results().expr_ty(expr);
         if cx.tcx.lang_items()
             // we are looking for the `DefId` of `Drop` trait in lang items
@@ -97,15 +98,20 @@ impl LateLintPass<'_> for MyStructLint {
             .map_or(false, |id| implements_trait(cx, ty, id, &[])) {
                 // `expr` implements `Drop` trait
             }
+
+        // 3. Using the type path with the expression
+        // we use `match_trait_method` function from Clippy's utils
+        if match_trait_method(cx, expr, &paths::INTO) {
+            // `expr` implements `Into` trait
+        }
     }
 }
 ```
 
-> Prefer using lang items, if the target trait is available there.
-
-A list of defined paths for Clippy can be found in [paths.rs][paths]
+> Prefer using diagnostic and lang items, if the target trait has one.
 
 We access lang items through the type context `tcx`. `tcx` is of type [`TyCtxt`][TyCtxt] and is defined in the `rustc_middle` crate.
+A list of defined paths for Clippy can be found in [paths.rs][paths]
 
 # Checking if a type defines a specific method