@@ -11,10 +11,6 @@ that we want to examine has a [diagnostic item][diagnostic_items],
11
11
As explained in the [ Rust Compiler Development Guide] [ rustc_dev_guide ] , diagnostic items
12
12
are introduced for identifying types via [ Symbols] [ symbol ] .
13
13
14
- While the Rust Compiler Development Guide has [ a section] [ using_diagnostic_items ] on
15
- how to check for a specific trait on a type ` Ty ` , Clippy provides
16
- a helper function ` is_trait_method ` , which simplifies the process for us.
17
-
18
14
For instance, if we want to examine whether an expression implements
19
15
the ` Iterator ` trait, we could simply write the following code,
20
16
providing the ` LateContext ` (` cx ` ), our expression at hand, and
@@ -28,9 +24,13 @@ use rustc_span::symbol::sym;
28
24
29
25
impl LateLintPass <'_ > for CheckIteratorTraitLint {
30
26
fn check_expr (& mut self , cx : & LateContext <'_ >, expr : & Expr <'_ >) {
31
- if is_trait_method (cx , expr , sym :: Iterator ) {
32
- println! (" This expression implements `Iterator` trait!" );
33
- }
27
+ let implements_iterator = cx . tcx. get_diagnostic_item (sym :: Iterator ). map_or (false , | id | {
28
+ implements_trait (cx , cx . typeck_results (). expr_ty (arg ), id , & [])
29
+ });
30
+ if implements_iterator {
31
+ // [...]
32
+ }
33
+
34
34
}
35
35
}
36
36
```
@@ -40,9 +40,8 @@ impl LateLintPass<'_> for CheckIteratorTraitLint {
40
40
## Using Lang Items
41
41
42
42
Besides diagnostic items, we can also use [ ` lang_items ` ] [ lang_items ] .
43
- Take a look at the documentation and we find that ` LanguageItems ` contains
44
- all language items both from the current crate or its
45
- dependencies.
43
+ Take a look at the documentation to find that ` LanguageItems ` contains
44
+ all language items defined in the compiler.
46
45
47
46
Using one of its ` *_trait ` method, we could obtain the [ DefId] of any
48
47
specific item, such as ` Clone ` , ` Copy ` , ` Drop ` , ` Eq ` , which are familiar
@@ -73,11 +72,11 @@ impl LateLintPass<'_> for CheckDropTraitLint {
73
72
74
73
## Using Type Path
75
74
76
- If neither diagnostic item or lang item is available, we can use
75
+ If neither diagnostic item nor a language item is available, we can use
77
76
[ ` clippy_utils::paths ` ] [ paths ] with the ` match_trait_method ` to determine trait
78
77
implementation.
79
78
80
- > ** Note** : This approach should be avoided if possible.
79
+ > ** Note** : This approach should be avoided if possible, the best thing to do would be to make a PR to [ ` rust-lang/rust ` ] [ rust ] .
81
80
82
81
Below, we check if the given ` expr ` implements ` tokio ` 's
83
82
[ ` AsyncReadExt ` ] [ AsyncReadExt ] trait:
@@ -89,17 +88,13 @@ use rustc_lint::{LateContext, LateLintPass};
89
88
90
89
impl LateLintPass <'_ > for CheckTokioAsyncReadExtTrait {
91
90
fn check_expr (& mut self , cx : & LateContext <'_ >, expr : & Expr <'_ >) {
92
- if match_trait_method (cx , expr , & paths :: TOKIO_IO_ASYNCREADEXT ) {
93
- println! (" `expr` implements `TOKIO_IO_ASYNCREADEXT ` trait!" );
91
+ if match_trait_method (cx , expr , & paths :: CORE_ITER_CLONED ) {
92
+ println! (" `expr` implements `CORE_ITER_CLONED ` trait!" );
94
93
}
95
94
}
96
95
}
97
96
```
98
97
99
- > ** Note** : Even though all the ` clippy_utils ` methods we have seen in this
100
- > chapter takes ` expr ` as a parameter, these methods are actually using
101
- > each expression's ` HirId ` under the hood.
102
-
103
98
[ AsyncReadExt ] : https://docs.rs/tokio/latest/tokio/io/trait.AsyncReadExt.html
104
99
[ DefId ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.DefId.html
105
100
[ diagnostic_items ] : https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-items.html
@@ -110,3 +105,4 @@ impl LateLintPass<'_> for CheckTokioAsyncReadExtTrait {
110
105
[ symbol_index ] : https://doc.rust-lang.org/beta/nightly-rustc/rustc_span/symbol/sym/index.html
111
106
[ TyCtxt ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html
112
107
[ using_diagnostic_items ] : https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-items.html#using-diagnostic-items
108
+ [ rust ] : https://github.com/rust-lang/rust
0 commit comments