@@ -263,24 +263,18 @@ pub fn is_res_lang_ctor(cx: &LateContext<'_>, res: Res, lang_item: LangItem) ->
263
263
}
264
264
}
265
265
266
- pub fn is_res_diagnostic_ctor ( cx : & LateContext < ' _ > , res : Res , diag_item : Symbol ) -> bool {
267
- if let Res :: Def ( DefKind :: Ctor ( ..) , id) = res
268
- && let Some ( id) = cx. tcx . opt_parent ( id)
269
- {
270
- cx. tcx . is_diagnostic_item ( diag_item, id)
271
- } else {
272
- false
273
- }
274
- }
275
266
276
- /// Checks if a `QPath` resolves to a constructor of a diagnostic item.
277
- pub fn is_diagnostic_ctor ( cx : & LateContext < ' _ > , qpath : & QPath < ' _ > , diagnostic_item : Symbol ) -> bool {
278
- if let QPath :: Resolved ( _, path) = qpath {
279
- if let Res :: Def ( DefKind :: Ctor ( ..) , ctor_id) = path. res {
280
- return cx. tcx . is_diagnostic_item ( diagnostic_item, cx. tcx . parent ( ctor_id) ) ;
281
- }
282
- }
283
- false
267
+ /// Checks if `{ctor_call_id}(...)` is `{enum_item}::{variant_name}(...)`.
268
+ pub fn is_enum_variant_ctor ( cx : & LateContext < ' _ > , enum_item : Symbol , variant_name : Symbol , ctor_call_id : DefId ) -> bool {
269
+ let Some ( enum_def_id) = cx. tcx . get_diagnostic_item ( enum_item) else {
270
+ return false ;
271
+ } ;
272
+
273
+ let variants = cx. tcx . adt_def ( enum_def_id) . variants ( ) . iter ( ) ;
274
+ variants
275
+ . filter ( |variant| variant. name == variant_name)
276
+ . filter_map ( |variant| variant. ctor . as_ref ( ) )
277
+ . any ( |( _, ctor_def_id) | * ctor_def_id == ctor_call_id)
284
278
}
285
279
286
280
/// Checks if the `DefId` matches the given diagnostic item or it's constructor.
0 commit comments