@@ -85,8 +85,8 @@ to check for. All of these methods only check for the base type, generic
8585arguments have to be checked separately.
8686
8787``` rust
88- use clippy_utils :: ty :: {is_type_diagnostic_item, is_type_lang_item};
8988use clippy_utils :: paths;
89+ use clippy_utils :: res :: MaybeDef ;
9090use rustc_span :: symbol :: sym;
9191use rustc_hir :: LangItem ;
9292
@@ -97,12 +97,12 @@ impl LateLintPass<'_> for MyStructLint {
9797
9898 // 1. Using diagnostic items
9999 // The last argument is the diagnostic item to check for
100- if is_type_diagnostic_item (cx , ty , sym :: Option ) {
100+ if ty . is_diag_item (cx , sym :: Option ) {
101101 // The type is an `Option`
102102 }
103103
104104 // 2. Using lang items
105- if is_type_lang_item (cx , ty , LangItem :: RangeFull ) {
105+ if ty . is_lang_item (cx , LangItem :: RangeFull ) {
106106 // The type is a full range like `.drain(..)`
107107 }
108108
@@ -124,26 +124,28 @@ diagnostic item, lang item or neither.
124124
125125``` rust
126126use clippy_utils :: ty :: implements_trait;
127- use clippy_utils :: is_trait_method;
128127use rustc_span :: symbol :: sym;
129128
130129impl LateLintPass <'_ > for MyStructLint {
131130 fn check_expr (& mut self , cx : & LateContext <'_ >, expr : & Expr <'_ >) {
132- // 1. Using diagnostic items with the expression
133- // we use `is_trait_method` function from Clippy's utils
134- if is_trait_method (cx , expr , sym :: Iterator ) {
135- // method call in `expr` belongs to `Iterator` trait
136- }
137131
138- // 2. Using lang items with the expression type
132+ // 1. Get the `DefId` of the trait.
133+ // via lang items
134+ let trait_id = cx . tcx. lang_items (). drop_trait ();
135+ // via diagnostic items
136+ let trait_id = cx . tcx. get_diagnostic_item (sym :: Eq );
137+
138+ // 2. Check for the trait implementation via the `implements_trait` util.
139139 let ty = cx . typeck_results (). expr_ty (expr );
140- if cx . tcx. lang_items ()
141- // we are looking for the `DefId` of `Drop` trait in lang items
142- . drop_trait ()
143- // then we use it with our type `ty` by calling `implements_trait` from Clippy's utils
144- . is_some_and (| id | implements_trait (cx , ty , id , & [])) {
145- // `expr` implements `Drop` trait
146- }
140+ if trait_id . is_some_and (| id | implements_trait (cx , ty , id , & [])) {
141+ // `ty` implements the trait.
142+ }
143+
144+ // 3. If the trait requires additional generic arguments
145+ let trait_id = cx . tcx. lang_items (). eq_trait ();
146+ if trait_id . is_some_and (| id | implements_trait (cx , ty , id , & [ty ])) {
147+ // `ty` implements `PartialEq<Self>`
148+ }
147149 }
148150}
149151```
@@ -159,7 +161,7 @@ paths for Clippy can be found in [paths.rs][paths]
159161To check if our type defines a method called ` some_method ` :
160162
161163``` rust
162- use clippy_utils :: ty :: is_type_lang_item ;
164+ use clippy_utils :: res :: MaybeDef ;
163165use clippy_utils :: {sym, return_ty};
164166
165167impl <'tcx > LateLintPass <'tcx > for MyTypeImpl {
@@ -173,7 +175,7 @@ impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
173175 // We can also check it has a parameter `self`
174176 && signature . decl. implicit_self. has_implicit_self ()
175177 // We can go further and even check if its return type is `String`
176- && is_type_lang_item ( cx , return_ty (cx , impl_item . hir_id), LangItem :: String )
178+ && return_ty (cx , impl_item . hir_id). is_lang_item ( cx , LangItem :: String )
177179 {
178180 // ...
179181 }
0 commit comments