Skip to content

Commit 22517f4

Browse files
committed
Also check Querys that are nested in Tuples
1 parent ba04cc2 commit 22517f4

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

crates/bevy_lint/src/unnecessary_with.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,36 @@ impl<'hir> LateLintPass<'hir> for UnnecessaryWith {
4949
_: HirId,
5050
) {
5151
for typ in decl.inputs {
52-
if let TyKind::Path(QPath::Resolved(_, path)) = &typ.kind {
53-
if bevy_helpers::path_matches_symbol_path(ctx, path, bevy_paths::QUERY) {
54-
check_for_unnecesarry_with(ctx, &typ);
55-
}
52+
recursively_search_type(ctx, typ, bevy_paths::QUERY, &check_for_unnecesarry_with);
53+
}
54+
}
55+
}
56+
57+
fn recursively_search_type<'hir, T: Fn(&LateContext<'hir>, &'hir Ty<'hir>) -> ()>(
58+
ctx: &LateContext<'hir>,
59+
typ: &'hir Ty,
60+
symbol_path: &[&str],
61+
function: &T,
62+
) {
63+
match &typ.kind {
64+
TyKind::Path(QPath::Resolved(_, path)) => {
65+
if bevy_helpers::path_matches_symbol_path(ctx, path, symbol_path) {
66+
(function)(ctx, &typ)
67+
}
68+
}
69+
TyKind::Tup(types) => {
70+
for tup_typ in *types {
71+
// Todo: Filter out Types that dont implement SystemParam
72+
// -> Is it possible to go from rustc_hit::Ty to rustc_middle::Ty?
73+
// Required for using clippy_utils::ty::implements_trait.
74+
recursively_search_type(ctx, tup_typ, symbol_path, function);
5675
}
5776
}
77+
_ => (),
5878
}
5979
}
6080

61-
fn check_for_unnecesarry_with<'hir>(ctx: &LateContext<'hir>, query: &'hir Ty) {
81+
fn check_for_unnecesarry_with<'hir>(ctx: &LateContext<'hir>, query: &'hir Ty<'hir>) {
6282
if let Some((world, Some(filter))) = bevy_helpers::get_generics_of_query(ctx, query) {
6383
let mut required_types = Vec::new();
6484
let mut with_types = Vec::new();

crates/bevy_lint/ui/unnecessary_with.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,25 @@ fn test_query21(_query: Query<&A, (Or<(With<A>, With<B>)>, With<C>)>) {
7979
fn test_query22(_query: Query<&mut A, (Or<(With<A>, With<B>)>, With<C>)>) {
8080
test_query22.system();
8181
}
82+
83+
fn test_query23(_query: (Query<&A, With<A>>,)) {
84+
test_query23.system();
85+
}
86+
fn test_query24(_query: (((((((((Query<&A, With<A>>,),),),),),),),),)) {
87+
test_query24.system();
88+
}
89+
fn test_query25(
90+
_query: (
91+
(),
92+
(((
93+
(),
94+
(
95+
((((Query<&A, With<A>>,), ()),),),
96+
(),
97+
(Query<&A, With<A>>, ()),
98+
),
99+
),),),
100+
),
101+
) {
102+
test_query25.system();
103+
}

crates/bevy_lint/ui/unnecessary_with.stderr

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,29 @@ error: Unnecessary `With` Filter
144144
LL | fn test_query22(_query: Query<&mut A, (Or<(With<A>, With<B>)>, With<C>)>) {
145145
| ^^^^^^^
146146

147-
error: aborting due to 24 previous errors
147+
error: Unnecessary `With` Filter
148+
--> $DIR/unnecessary_with.rs:83:36
149+
|
150+
LL | fn test_query23(_query: (Query<&A, With<A>>,)) {
151+
| ^^^^^^^
152+
153+
error: Unnecessary `With` Filter
154+
--> $DIR/unnecessary_with.rs:86:44
155+
|
156+
LL | fn test_query24(_query: (((((((((Query<&A, With<A>>,),),),),),),),),)) {
157+
| ^^^^^^^
158+
159+
error: Unnecessary `With` Filter
160+
--> $DIR/unnecessary_with.rs:95:31
161+
|
162+
LL | ((((Query<&A, With<A>>,), ()),),),
163+
| ^^^^^^^
164+
165+
error: Unnecessary `With` Filter
166+
--> $DIR/unnecessary_with.rs:97:28
167+
|
168+
LL | (Query<&A, With<A>>, ()),
169+
| ^^^^^^^
170+
171+
error: aborting due to 28 previous errors
148172

0 commit comments

Comments
 (0)