@@ -68,7 +68,7 @@ impl<'tcx> LateLintPass<'tcx> for MyStructLint {
6868 // Check our expr is calling a method
6969 if let hir :: ExprKind :: MethodCall (path , _ , _self_arg , .. ) = & expr . kind
7070 // Check the name of this method is `some_method`
71- && path . ident. name. as_str () == " some_method"
71+ && path . ident. name == sym :: some_method
7272 // Optionally, check the type of the self argument.
7373 // - See "Checking for a specific type"
7474 {
@@ -85,9 +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};
89- use clippy_utils :: paths;
90- use rustc_span :: symbol :: sym;
88+ use clippy_utils :: {paths, sym};
89+ use clippy_utils :: res :: MaybeDef ;
9190use rustc_hir :: LangItem ;
9291
9392impl LateLintPass <'_ > for MyStructLint {
@@ -97,12 +96,12 @@ impl LateLintPass<'_> for MyStructLint {
9796
9897 // 1. Using diagnostic items
9998 // The last argument is the diagnostic item to check for
100- if is_type_diagnostic_item (cx , ty , sym :: Option ) {
99+ if ty . is_diag_item (cx , sym :: Option ) {
101100 // The type is an `Option`
102101 }
103102
104103 // 2. Using lang items
105- if is_type_lang_item (cx , ty , LangItem :: RangeFull ) {
104+ if ty . is_lang_item (cx , LangItem :: RangeFull ) {
106105 // The type is a full range like `.drain(..)`
107106 }
108107
@@ -123,27 +122,29 @@ There are three ways to do this, depending on if the target trait has a
123122diagnostic item, lang item or neither.
124123
125124``` rust
125+ use clippy_utils :: sym;
126126use clippy_utils :: ty :: implements_trait;
127- use clippy_utils :: is_trait_method;
128- use rustc_span :: symbol :: sym;
129127
130128impl LateLintPass <'_ > for MyStructLint {
131129 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- }
137130
138- // 2. Using lang items with the expression type
131+ // 1. Get the `DefId` of the trait.
132+ // via lang items
133+ let trait_id = cx . tcx. lang_items (). drop_trait ();
134+ // via diagnostic items
135+ let trait_id = cx . tcx. get_diagnostic_item (sym :: Eq );
136+
137+ // 2. Check for the trait implementation via the `implements_trait` util.
139138 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- }
139+ if trait_id . is_some_and (| id | implements_trait (cx , ty , id , & [])) {
140+ // `ty` implements the trait.
141+ }
142+
143+ // 3. If the trait requires additional generic arguments
144+ let trait_id = cx . tcx. lang_items (). eq_trait ();
145+ if trait_id . is_some_and (| id | implements_trait (cx , ty , id , & [ty ])) {
146+ // `ty` implements `PartialEq<Self>`
147+ }
147148 }
148149}
149150```
@@ -173,7 +174,7 @@ impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
173174 // We can also check it has a parameter `self`
174175 && signature . decl. implicit_self. has_implicit_self ()
175176 // 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 )
177+ && return_ty (cx , impl_item . hir_id). is_lang_item ( cx , LangItem :: String )
177178 {
178179 // ...
179180 }
0 commit comments