bevy_lint/lints/suspicious/unit_in_bundle.rs
1//! Checks for `Bundle`s that contain the unit [`()`](unit) as a component.
2//!
3//! Specifically, this lint checks for when you pass a `Bundle` to a function or method, such as
4//! `Commands::spawn()`. If the bundle contains a unit, the lint will emit a warning.
5//!
6//! # Motivation
7//!
8//! It is possible to create bundles with a unit `()` component, since unit implements `Bundle`.
9//! Unit is not a `Component`, however, and will be ignored instead of added to the entity. Often,
10//! inserting a unit is unintentional and is a sign that the author intended to do something else.
11//!
12//! # Example
13//!
14//! ```
15//! # use bevy::prelude::*;
16//! # use std::f32::consts::PI;
17//! #
18//! fn spawn(mut commands: Commands) {
19//! commands.spawn(());
20//!
21//! commands.spawn((
22//! Name::new("Decal"),
23//! // This is likely a mistake! `Transform::rotate_z()` returns a unit `()`, not a
24//! // `Transform`! As such, no `Transform` will be inserted into the entity.
25//! Transform::from_translation(Vec3::new(0.75, 0.0, 0.0))
26//! .rotate_z(PI / 4.0),
27//! ));
28//! }
29//! #
30//! # bevy::ecs::system::assert_is_system(spawn);
31//! ```
32//!
33//! Use instead:
34//!
35//! ```
36//! # use bevy::prelude::*;
37//! # use std::f32::consts::PI;
38//! #
39//! fn spawn(mut commands: Commands) {
40//! // `Commands::spawn_empty()` is preferred if you do not need to add any components.
41//! commands.spawn_empty();
42//!
43//! commands.spawn((
44//! Name::new("Decal"),
45//! // `Transform::with_rotation()` returns a `Transform`, which was the intended behavior.
46//! Transform::from_translation(Vec3::new(0.75, 0.0, 0.0))
47//! .with_rotation(Quat::from_rotation_z(PI / 4.0)),
48//! ));
49//! }
50//! #
51//! # bevy::ecs::system::assert_is_system(spawn);
52//! ```
53
54use clippy_utils::{diagnostics::span_lint_hir_and_then, fn_def_id, paths::PathLookup};
55use rustc_errors::Applicability;
56use rustc_hir::{Expr, ExprKind, PathSegment, def_id::DefId};
57use rustc_lint::{LateContext, LateLintPass};
58use rustc_middle::ty::{self, Ty};
59use rustc_span::Symbol;
60#[allow(
61 rustc::direct_use_of_rustc_type_ir,
62 reason = "needed to correctly find `Bundle` trait bounds"
63)]
64use rustc_type_ir::PredicatePolarity;
65
66use crate::{
67 declare_bevy_lint, declare_bevy_lint_pass, paths, span_assert, span_assert_eq, sym,
68 utils::method_call::MethodCall,
69};
70
71declare_bevy_lint! {
72 pub(crate) UNIT_IN_BUNDLE,
73 super::Suspicious,
74 "created a `Bundle` containing a unit `()`",
75}
76
77declare_bevy_lint_pass! {
78 pub(crate) UnitInBundle => [UNIT_IN_BUNDLE],
79}
80
81impl<'tcx> LateLintPass<'tcx> for UnitInBundle {
82 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
83 if expr.span.in_external_macro(cx.tcx.sess.source_map()) {
84 return;
85 }
86
87 let (fn_id, fn_args, fn_arg_types) = if let Some(MethodCall {
88 method_path,
89 receiver,
90 args,
91 span,
92 ..
93 }) = MethodCall::try_from(cx, expr)
94 {
95 // There are a few methods named `spawn()` that can be substituted for `spawn_empty()`.
96 // This checks for those special cases and emits a machine-applicable suggestion when
97 // possible.
98 if let Some(bundle_expr) = can_be_spawn_empty(cx, method_path, receiver, args) {
99 span_lint_hir_and_then(
100 cx,
101 UNIT_IN_BUNDLE,
102 bundle_expr.hir_id,
103 bundle_expr.span,
104 UNIT_IN_BUNDLE.desc,
105 |diag| {
106 diag.note("units `()` are not `Component`s and will be skipped")
107 .span_suggestion(
108 span,
109 "`spawn_empty()` is more efficient",
110 "spawn_empty()",
111 Applicability::MachineApplicable,
112 );
113 },
114 );
115
116 return;
117 }
118
119 let Some(fn_id) = fn_def_id(cx, expr) else {
120 // This will be `None` if the function is a local closure. Since closures
121 // cannot have generic parameters, they cannot take bundles as an input, so we
122 // can skip them.
123 return;
124 };
125
126 // The first argument is `&self` because it's a method. We skip it because `&self`
127 // won't be in `args`, making the two slices two different lengths.
128 let fn_arg_types = &fn_arg_types(cx, fn_id)[1..];
129
130 (fn_id, args, fn_arg_types)
131 } else if let ExprKind::Call(_, fn_args) = expr.kind {
132 let Some(fn_id) = fn_def_id(cx, expr) else {
133 // This will be `None` if the function is a local closure. Since closures
134 // cannot have generic parameters, they cannot take bundles as an input, so we
135 // can skip them.
136 return;
137 };
138
139 let fn_arg_types = fn_arg_types(cx, fn_id);
140
141 (fn_id, fn_args, fn_arg_types)
142 } else {
143 return;
144 };
145
146 span_assert_eq!(expr.span, fn_args.len(), fn_arg_types.len());
147
148 let typeck_results = cx.typeck_results();
149
150 for bundle_expr in filter_bundle_args(cx, fn_id, fn_args, fn_arg_types) {
151 let bundle_ty = typeck_results.expr_ty(bundle_expr);
152
153 for tuple_path in find_units_in_tuple(bundle_ty) {
154 let unit_expr = tuple_path.into_expr(bundle_expr);
155
156 span_lint_hir_and_then(
157 cx,
158 UNIT_IN_BUNDLE,
159 unit_expr.hir_id,
160 unit_expr.span,
161 UNIT_IN_BUNDLE.desc,
162 |diag| {
163 diag.note("units `()` are not `Component`s and will be skipped");
164 },
165 );
166 }
167 }
168 }
169}
170
171/// Returns the arguments of a method call that are intended to be `Bundle`s.
172///
173/// `fn_id` should be the definition of the function itself, and `args` should be the arguments
174/// passed to the function.
175fn filter_bundle_args<'tcx>(
176 cx: &LateContext<'tcx>,
177 fn_id: DefId,
178 fn_args: &'tcx [Expr<'tcx>],
179 fn_arg_types: &[Ty<'tcx>],
180) -> impl Iterator<Item = &'tcx Expr<'tcx>> {
181 let bundle_bounded_generics: Vec<Ty<'_>> = bundle_bounded_generics(cx, fn_id);
182
183 // Only yield arguments whose types are generic parameters that require the `Bundle` trait.
184 fn_arg_types
185 .iter()
186 .enumerate()
187 .filter(move |(_, arg)| bundle_bounded_generics.contains(arg))
188 .map(|(i, _)| &fn_args[i])
189}
190
191/// Returns a list of types corresponding to the inputs of a function.
192///
193/// Notably, the returned types are not instantiated. Generic parameters will be preserved and not
194/// filled in with actual types.
195///
196/// # Example
197///
198/// Running this function on the [`DefId`] of `foo()` will return `[usize, bool]`, while `bar()`
199/// will return `[T, usize]`.
200///
201/// ```
202/// # use bevy::ecs::bundle::Bundle;
203/// #
204/// fn foo(a: usize, b: bool) {}
205/// fn bar<T: Bundle>(bundle: T, size: usize) {}
206/// ```
207fn fn_arg_types<'tcx>(cx: &LateContext<'tcx>, fn_id: DefId) -> &'tcx [Ty<'tcx>] {
208 cx.tcx
209 .fn_sig(fn_id)
210 .instantiate_identity()
211 .inputs()
212 .skip_binder()
213}
214
215/// Returns a list of a generic parameters of a function that must implement `Bundle`.
216///
217/// Each returned [`Ty`] is guaranteed to be a [`ty::TyKind::Param`].
218///
219/// # Example
220///
221/// If run on the following function, this function would return `A` and `C` because they both
222/// implement `Bundle`.
223///
224/// ```
225/// # use bevy::ecs::bundle::Bundle;
226/// #
227/// fn my_function<A: Bundle, B: Clone, C: Bundle + Clone>(_: A, _: B, _: C) {
228/// // ...
229/// }
230/// ```
231fn bundle_bounded_generics<'tcx>(cx: &LateContext<'tcx>, fn_id: DefId) -> Vec<Ty<'tcx>> {
232 let mut bundle_bounded_generics = Vec::new();
233
234 // Fetch the parameter environment for the function, which contains all generic trait bounds.
235 // (Such as the `T: Bundle` that we're looking for!) See
236 // <https://rustc-dev-guide.rust-lang.org/typing_parameter_envs.html> for more information.
237 let param_env = cx.tcx.param_env(fn_id);
238
239 for clause in param_env.caller_bounds() {
240 // We only want trait predicates, filtering out lifetimes and constants.
241 if let Some(trait_predicate) = clause.as_trait_clause()
242 // The `Bundle` trait doesn't require any bound vars, so we dispel the binder.
243 && let Some(trait_predicate) = trait_predicate.no_bound_vars()
244 && let ty::TraitPredicate {
245 trait_ref,
246 // Negative trait bounds, which are unstable, allow matching all types _except_
247 // those with a specific trait. We don't want that, however, so we only match
248 // positive trait bounds.
249 polarity: PredicatePolarity::Positive,
250 } = trait_predicate
251 // Only match `T: Bundle` predicates.
252 && paths::BUNDLE.matches(cx, trait_ref.def_id)
253 {
254 let self_ty = trait_ref.self_ty();
255
256 span_assert!(
257 cx.tcx.def_span(fn_id),
258 matches!(self_ty.kind(), ty::TyKind::Param(_)),
259 "type {self_ty} from trait bound {trait_ref} was expected to be a type parameter",
260 );
261
262 // At this point, we've confirmed the predicate is `T: Bundle`! Add it to the list to
263 // be returned. :)
264 bundle_bounded_generics.push(trait_ref.self_ty());
265 }
266 }
267
268 bundle_bounded_generics
269}
270
271/// Represents the path to an item within a nested tuple.
272///
273/// # Example
274///
275/// Each number within the [`TuplePath`] represents an index into the tuple. An empty path
276/// represents the root tuple, while a path of `TuplePath([0])` represents the first item within
277/// that tuple.
278///
279/// ```ignore
280/// // TuplePath([])
281/// (
282/// // TuplePath([0])
283/// Name::new("Foo"),
284/// // TuplePath([1])
285/// (
286/// // TuplePath([1, 0])
287/// (),
288/// // TuplePath([1, 1])
289/// Transform::default(),
290/// // TuplePath([1, 2])
291/// Visibility::Hidden,
292/// ),
293/// // TuplePath([2])
294/// (),
295/// )
296/// ```
297#[derive(Clone, Debug)]
298#[repr(transparent)]
299struct TuplePath(Vec<usize>);
300
301impl TuplePath {
302 /// Creates an empty [`TuplePath`].
303 fn new() -> Self {
304 Self(Vec::new())
305 }
306
307 /// Pushes an index to the end of the path.
308 fn push(&mut self, i: usize) {
309 self.0.push(i);
310 }
311
312 /// Pops the last index in the path.
313 fn pop(&mut self) -> Option<usize> {
314 self.0.pop()
315 }
316
317 /// Finds the [`Expr`] of the item represented by this path given the root tuple.
318 ///
319 /// In the event the path is invalid in some way (such as if an expected tuple is not found),
320 /// this will return the expression closest to the target.
321 fn into_expr<'tcx>(self, root_tuple: &'tcx Expr<'tcx>) -> &'tcx Expr<'tcx> {
322 let mut tuple = root_tuple;
323
324 for i in self.0 {
325 let ExprKind::Tup(items) = tuple.kind else {
326 // If the path is invalid in some way, return the expression nearest to the target.
327 // This is usually the case when the bundle is created outside of
328 // `Commands::spawn()`, such as with `commands.spawn(my_helper())` instead of the
329 // expected `commands.spawn((Foo, Bar, ()))`.
330 return tuple;
331 };
332
333 tuple = &items[i];
334 }
335
336 tuple
337 }
338}
339
340/// Returns the [`TuplePath`]s to all unit types within a tuple type.
341///
342/// # Example
343///
344/// Given a type:
345///
346/// ```ignore
347/// type MyBundle = (
348/// Name,
349/// (
350/// (),
351/// Transform,
352/// Visibility,
353/// ),
354/// (),
355/// );
356/// ```
357///
358/// This function would return:
359///
360/// ```ignore
361/// [
362/// TuplePath([1, 0]),
363/// TuplePath([2]),
364/// ]
365/// ```
366///
367/// See [`TuplePath`]'s documentation for more information.
368fn find_units_in_tuple(ty: Ty<'_>) -> Vec<TuplePath> {
369 fn inner(ty: Ty<'_>, current_path: &mut TuplePath, unit_paths: &mut Vec<TuplePath>) {
370 if let ty::TyKind::Tuple(types) = ty.kind() {
371 if types.is_empty() {
372 unit_paths.push(current_path.clone());
373 return;
374 }
375
376 for (i, ty) in types.into_iter().enumerate() {
377 current_path.push(i);
378 inner(ty, current_path, unit_paths);
379 current_path.pop();
380 }
381 }
382 }
383
384 let mut current_path = TuplePath::new();
385 let mut unit_paths = Vec::new();
386
387 inner(ty, &mut current_path, &mut unit_paths);
388
389 unit_paths
390}
391
392/// Returns [`Some`] if the method can be replaced with `spawn_empty()`.
393///
394/// The returned [`Expr`] is that of the unit `()` in the bundle argument.
395fn can_be_spawn_empty<'tcx>(
396 cx: &LateContext<'tcx>,
397 method_path: &'tcx PathSegment<'tcx>,
398 receiver: &'tcx Expr<'tcx>,
399 args: &'tcx [Expr<'tcx>],
400) -> Option<&'tcx Expr<'tcx>> {
401 // A list of all methods that can be replaced with `spawn_empty()`. The format is `(receiver
402 // type, method name, bundle arg index)`.
403 static CAN_SPAWN_EMPTY: &[(&PathLookup, Symbol, usize)] = &[
404 (&paths::COMMANDS, sym::spawn, 0),
405 (&paths::WORLD, sym::spawn, 0),
406 (&paths::RELATED_SPAWNER, sym::spawn, 0),
407 (&paths::RELATED_SPAWNER_COMMANDS, sym::spawn, 0),
408 ];
409
410 let typeck_results = cx.typeck_results();
411
412 // Find the adjusted receiver type (e.g. `World` from `Box<World>`), removing any references to
413 // find the underlying type.
414 let receiver_ty = typeck_results.expr_ty_adjusted(receiver).peel_refs();
415
416 for (path, method, index) in CAN_SPAWN_EMPTY {
417 if path.matches_ty(cx, receiver_ty)
418 && method_path.ident.name == *method
419 && let Some(bundle_expr) = args.get(*index)
420 && typeck_results.expr_ty(bundle_expr).is_unit()
421 {
422 return Some(bundle_expr);
423 }
424 }
425
426 None
427}