1- use rustc_hir:: def_id:: DefId ;
1+ use clippy_utils:: peel_hir_ty_options;
2+ use rustc_hir:: { FnSig , HirId , ItemKind , Node , def_id:: DefId , intravisit:: FnKind } ;
3+ use rustc_hir_analysis:: lower_ty;
24use rustc_lint:: LateContext ;
35use rustc_middle:: ty:: { Ty , TyKind } ;
46use rustc_span:: symbol:: Symbol ;
@@ -19,6 +21,23 @@ pub fn match_def_path(cx: &LateContext<'_>, did: DefId, syms: &[&str]) -> bool {
1921 . eq ( path. iter ( ) . copied ( ) )
2022}
2123
24+ pub fn match_def_paths ( cx : & LateContext < ' _ > , did : DefId , syms : & [ & [ & str ] ] ) -> bool {
25+ let path = cx. get_def_path ( did) ;
26+ syms. iter ( ) . any ( |syms| {
27+ syms. iter ( )
28+ . map ( |x| Symbol :: intern ( x) )
29+ . eq ( path. iter ( ) . copied ( ) )
30+ } )
31+ }
32+
33+ pub fn is_trait_item ( cx : & LateContext < ' _ > , hir_id : HirId ) -> bool {
34+ if let Node :: Item ( item) = cx. tcx . parent_hir_node ( hir_id) {
35+ matches ! ( item. kind, ItemKind :: Trait ( ..) )
36+ } else {
37+ false
38+ }
39+ }
40+
2241pub fn is_param_ty ( ty : & Ty ) -> bool {
2342 matches ! ( ty. kind( ) , TyKind :: Param ( _) )
2443}
@@ -35,16 +54,27 @@ pub fn is_agent_ty(cx: &LateContext<'_>, ty: &Ty) -> bool {
3554}
3655
3756pub fn is_gc_scope_ty ( cx : & LateContext < ' _ > , ty : & Ty ) -> bool {
38- match ty. kind ( ) {
57+ match ty. peel_refs ( ) . kind ( ) {
3958 TyKind :: Adt ( def, _) => {
4059 match_def_path ( cx, def. did ( ) , & [ "nova_vm" , "engine" , "context" , "GcScope" ] )
4160 }
4261 _ => false ,
4362 }
4463}
4564
65+ pub fn is_no_gc_method ( cx : & LateContext < ' _ > , did : DefId ) -> bool {
66+ match_def_paths (
67+ cx,
68+ did,
69+ & [
70+ & [ "nova_vm" , "engine" , "context" , "GcScope" , "nogc" ] ,
71+ & [ "nova_vm" , "engine" , "context" , "GcScope" , "into_nogc" ] ,
72+ ] ,
73+ )
74+ }
75+
4676pub fn is_no_gc_scope_ty ( cx : & LateContext < ' _ > , ty : & Ty ) -> bool {
47- match ty. kind ( ) {
77+ match ty. peel_refs ( ) . kind ( ) {
4878 TyKind :: Adt ( def, _) => match_def_path (
4979 cx,
5080 def. did ( ) ,
@@ -53,3 +83,85 @@ pub fn is_no_gc_scope_ty(cx: &LateContext<'_>, ty: &Ty) -> bool {
5383 _ => false ,
5484 }
5585}
86+
87+ pub fn is_value_ty ( cx : & LateContext < ' _ > , ty : & Ty ) -> bool {
88+ match ty. peel_refs ( ) . kind ( ) {
89+ TyKind :: Adt ( def, _) => match_def_path (
90+ cx,
91+ def. did ( ) ,
92+ & [
93+ "nova_vm" ,
94+ "ecmascript" ,
95+ "types" ,
96+ "language" ,
97+ "value" ,
98+ "Value" ,
99+ ] ,
100+ ) ,
101+ _ => false ,
102+ }
103+ }
104+
105+ pub fn is_object_ty ( cx : & LateContext < ' _ > , ty : & Ty ) -> bool {
106+ match ty. peel_refs ( ) . kind ( ) {
107+ TyKind :: Adt ( def, _) => match_def_path (
108+ cx,
109+ def. did ( ) ,
110+ & [
111+ "nova_vm" ,
112+ "ecmascript" ,
113+ "types" ,
114+ "language" ,
115+ "object" ,
116+ "Object" ,
117+ ] ,
118+ ) ,
119+ _ => false ,
120+ }
121+ }
122+
123+ pub fn is_arguments_list_ty ( cx : & LateContext < ' _ > , ty : & Ty ) -> bool {
124+ match ty. peel_refs ( ) . kind ( ) {
125+ TyKind :: Adt ( def, _) => match_def_path (
126+ cx,
127+ def. did ( ) ,
128+ & [
129+ "nova_vm" ,
130+ "ecmascript" ,
131+ "builtins" ,
132+ "builtin_function" ,
133+ "ArgumentsList" ,
134+ ] ,
135+ ) ,
136+ _ => false ,
137+ }
138+ }
139+
140+ pub fn could_be_builtin_method_sig < ' tcx > ( cx : & LateContext < ' tcx > , sig : & FnSig < ' tcx > ) -> bool {
141+ sig. decl . inputs . len ( ) == 4
142+ && is_agent_ty ( cx, & lower_ty ( cx. tcx , & sig. decl . inputs [ 0 ] ) )
143+ && is_value_ty ( cx, & lower_ty ( cx. tcx , & sig. decl . inputs [ 1 ] ) )
144+ && is_arguments_list_ty ( cx, & lower_ty ( cx. tcx , & sig. decl . inputs [ 2 ] ) )
145+ && is_gc_scope_ty ( cx, & lower_ty ( cx. tcx , & sig. decl . inputs [ 3 ] ) )
146+ }
147+
148+ pub fn could_be_builtin_constructor_sig < ' tcx > ( cx : & LateContext < ' tcx > , sig : & FnSig < ' tcx > ) -> bool {
149+ sig. decl . inputs . len ( ) == 5
150+ && is_agent_ty ( cx, & lower_ty ( cx. tcx , & sig. decl . inputs [ 0 ] ) )
151+ && is_value_ty ( cx, & lower_ty ( cx. tcx , & sig. decl . inputs [ 1 ] ) )
152+ && is_arguments_list_ty ( cx, & lower_ty ( cx. tcx , & sig. decl . inputs [ 2 ] ) )
153+ && is_object_ty (
154+ cx,
155+ & lower_ty ( cx. tcx , peel_hir_ty_options ( cx, & sig. decl . inputs [ 3 ] ) ) ,
156+ )
157+ && is_gc_scope_ty ( cx, & lower_ty ( cx. tcx , & sig. decl . inputs [ 4 ] ) )
158+ }
159+
160+ pub fn could_be_builtin_method_def < ' tcx > ( cx : & LateContext < ' tcx > , kind : FnKind < ' tcx > ) -> bool {
161+ match kind {
162+ FnKind :: Method ( _, sig) => {
163+ could_be_builtin_method_sig ( cx, sig) || could_be_builtin_constructor_sig ( cx, sig)
164+ }
165+ _ => false ,
166+ }
167+ }
0 commit comments