1
1
use clippy_utils:: diagnostics:: span_lint;
2
- use if_chain:: if_chain;
3
2
use rustc_hir:: {
4
- def:: Res , hir_id:: HirId , intravisit:: FnKind , Body , FnDecl , GenericArg , MutTy , Path , QPath , Ty ,
5
- TyKind ,
3
+ hir_id:: HirId , intravisit:: FnKind , Body , FnDecl , GenericArg , Path , QPath , Ty , TyKind ,
6
4
} ;
7
5
use rustc_lint:: { LateContext , LateLintPass } ;
8
6
use rustc_session:: { declare_lint, declare_lint_pass} ;
9
- use rustc_span:: { def_id:: DefId , symbol :: Symbol , Span } ;
7
+ use rustc_span:: { def_id:: DefId , Span } ;
10
8
11
- use crate :: bevy_paths;
9
+ use crate :: { bevy_helpers , bevy_paths} ;
12
10
13
11
declare_lint ! {
14
12
/// **What it does:**
@@ -51,15 +49,13 @@ impl<'hir> LateLintPass<'hir> for UnnecessaryWith {
51
49
_: HirId ,
52
50
) {
53
51
for typ in decl. inputs {
54
- if_chain ! {
55
- if let TyKind :: Path ( QPath :: Resolved ( _, path) ) = & typ. kind;
56
- if path_matches_symbol_path( ctx, path, bevy_paths:: QUERY ) ;
57
- if let Some ( segment) = path. segments. iter( ) . last( ) ;
58
- if let Some ( generic_args) = segment. args;
59
- if let Some ( GenericArg :: Type ( world) ) = & generic_args. args. get( 1 ) ;
60
- if let Some ( GenericArg :: Type ( filter) ) = & generic_args. args. get( 2 ) ;
61
- then {
62
- check_for_overlap( ctx, world, filter) ;
52
+ if let TyKind :: Path ( QPath :: Resolved ( _, path) ) = & typ. kind {
53
+ if bevy_helpers:: path_matches_symbol_path ( ctx, path, bevy_paths:: QUERY ) {
54
+ if let Some ( ( world, Some ( filter) ) ) =
55
+ bevy_helpers:: get_generics_of_query ( ctx, & typ)
56
+ {
57
+ check_for_overlap ( ctx, world, filter) ;
58
+ }
63
59
}
64
60
}
65
61
}
@@ -72,14 +68,14 @@ fn check_for_overlap<'hir>(ctx: &LateContext<'hir>, world: &Ty, filter: &Ty) {
72
68
73
69
match & world. kind {
74
70
TyKind :: Rptr ( _, mut_type) => {
75
- if let Some ( def_id) = get_def_id_of_reference ( & mut_type) {
71
+ if let Some ( def_id) = bevy_helpers :: get_def_id_of_referenced_type ( & mut_type) {
76
72
required_types. push ( def_id) ;
77
73
}
78
74
}
79
75
TyKind :: Tup ( types) => {
80
76
for typ in * types {
81
77
if let TyKind :: Rptr ( _, mut_type) = & typ. kind {
82
- if let Some ( def_id) = get_def_id_of_reference ( & mut_type) {
78
+ if let Some ( def_id) = bevy_helpers :: get_def_id_of_referenced_type ( & mut_type) {
83
79
required_types. push ( def_id) ;
84
80
}
85
81
}
@@ -90,30 +86,30 @@ fn check_for_overlap<'hir>(ctx: &LateContext<'hir>, world: &Ty, filter: &Ty) {
90
86
91
87
match & filter. kind {
92
88
TyKind :: Path ( QPath :: Resolved ( _, path) ) => {
93
- if path_matches_symbol_path ( ctx, path, bevy_paths:: OR ) {
89
+ if bevy_helpers :: path_matches_symbol_path ( ctx, path, bevy_paths:: OR ) {
94
90
with_types. extend ( check_or_filter ( ctx, path) ) ;
95
91
}
96
- if path_matches_symbol_path ( ctx, path, bevy_paths:: WITH ) {
97
- if let Some ( def_id) = get_def_id_of_first_generic_arg ( path) {
92
+ if bevy_helpers :: path_matches_symbol_path ( ctx, path, bevy_paths:: WITH ) {
93
+ if let Some ( def_id) = bevy_helpers :: get_def_id_of_first_generic_arg ( path) {
98
94
with_types. push ( ( def_id, filter. span ) ) ;
99
95
}
100
96
}
101
97
}
102
98
TyKind :: Tup ( types) => {
103
99
for typ in * types {
104
100
if let TyKind :: Path ( QPath :: Resolved ( _, path) ) = typ. kind {
105
- if path_matches_symbol_path ( ctx, path, bevy_paths:: OR ) {
101
+ if bevy_helpers :: path_matches_symbol_path ( ctx, path, bevy_paths:: OR ) {
106
102
with_types. extend ( check_or_filter ( ctx, path) ) ;
107
103
}
108
- if path_matches_symbol_path ( ctx, path, bevy_paths:: ADDED )
109
- || path_matches_symbol_path ( ctx, path, bevy_paths:: CHANGED )
104
+ if bevy_helpers :: path_matches_symbol_path ( ctx, path, bevy_paths:: ADDED )
105
+ || bevy_helpers :: path_matches_symbol_path ( ctx, path, bevy_paths:: CHANGED )
110
106
{
111
- if let Some ( def_id) = get_def_id_of_first_generic_arg ( path) {
107
+ if let Some ( def_id) = bevy_helpers :: get_def_id_of_first_generic_arg ( path) {
112
108
required_types. push ( def_id) ;
113
109
}
114
110
}
115
- if path_matches_symbol_path ( ctx, path, bevy_paths:: WITH ) {
116
- if let Some ( def_id) = get_def_id_of_first_generic_arg ( path) {
111
+ if bevy_helpers :: path_matches_symbol_path ( ctx, path, bevy_paths:: WITH ) {
112
+ if let Some ( def_id) = bevy_helpers :: get_def_id_of_first_generic_arg ( path) {
117
113
with_types. push ( ( def_id, typ. span ) ) ;
118
114
}
119
115
}
@@ -135,51 +131,6 @@ fn check_for_overlap<'hir>(ctx: &LateContext<'hir>, world: &Ty, filter: &Ty) {
135
131
}
136
132
}
137
133
138
- fn get_def_id_of_reference ( reference : & MutTy ) -> Option < DefId > {
139
- if let TyKind :: Path ( QPath :: Resolved ( _, path) ) = reference. ty . kind {
140
- if let Res :: Def ( _, def_id) = path. res {
141
- return Some ( def_id) ;
142
- }
143
- }
144
-
145
- None
146
- }
147
-
148
- fn path_matches_symbol_path < ' hir > (
149
- ctx : & LateContext < ' hir > ,
150
- path : & Path ,
151
- symbol_path : & [ & str ] ,
152
- ) -> bool {
153
- if let Res :: Def ( _, def_id) = path. res {
154
- return ctx. match_def_path (
155
- def_id,
156
- symbol_path
157
- . iter ( )
158
- . map ( |str| Symbol :: intern ( str) )
159
- . collect :: < Vec < _ > > ( )
160
- . as_slice ( ) ,
161
- ) ;
162
- } ;
163
-
164
- false
165
- }
166
-
167
- fn get_def_id_of_first_generic_arg ( path : & Path ) -> Option < DefId > {
168
- if let Some ( segment) = path. segments . iter ( ) . last ( ) {
169
- if let Some ( generic_args) = segment. args {
170
- if let Some ( GenericArg :: Type ( component) ) = & generic_args. args . get ( 0 ) {
171
- if let TyKind :: Path ( QPath :: Resolved ( _, path) ) = component. kind {
172
- if let Res :: Def ( _, def_id) = path. res {
173
- return Some ( def_id) ;
174
- }
175
- }
176
- }
177
- }
178
- }
179
-
180
- None
181
- }
182
-
183
134
fn check_or_filter < ' hir > ( ctx : & LateContext < ' hir > , path : & Path ) -> Vec < ( DefId , Span ) > {
184
135
let mut local_required_types = Vec :: new ( ) ;
185
136
let mut local_with_types = Vec :: new ( ) ;
@@ -190,15 +141,23 @@ fn check_or_filter<'hir>(ctx: &LateContext<'hir>, path: &Path) -> Vec<(DefId, Sp
190
141
if let TyKind :: Tup ( types) = tuple. kind {
191
142
for typ in types {
192
143
if let TyKind :: Path ( QPath :: Resolved ( _, path) ) = typ. kind {
193
- if path_matches_symbol_path ( ctx, path, bevy_paths:: ADDED )
194
- || path_matches_symbol_path ( ctx, path, bevy_paths:: CHANGED )
144
+ if bevy_helpers:: path_matches_symbol_path ( ctx, path, bevy_paths:: ADDED )
145
+ || bevy_helpers:: path_matches_symbol_path (
146
+ ctx,
147
+ path,
148
+ bevy_paths:: CHANGED ,
149
+ )
195
150
{
196
- if let Some ( def_id) = get_def_id_of_first_generic_arg ( path) {
151
+ if let Some ( def_id) =
152
+ bevy_helpers:: get_def_id_of_first_generic_arg ( path)
153
+ {
197
154
local_required_types. push ( def_id) ;
198
155
}
199
156
}
200
- if path_matches_symbol_path ( ctx, path, bevy_paths:: WITH ) {
201
- if let Some ( def_id) = get_def_id_of_first_generic_arg ( path) {
157
+ if bevy_helpers:: path_matches_symbol_path ( ctx, path, bevy_paths:: WITH ) {
158
+ if let Some ( def_id) =
159
+ bevy_helpers:: get_def_id_of_first_generic_arg ( path)
160
+ {
202
161
local_with_types. push ( ( def_id, typ. span ) ) ;
203
162
}
204
163
}
0 commit comments