Module unit_in_bundle

Source
Expand description

Checks for Bundles that contain the unit () as a component.

Specifically, this lint checks for when you pass a Bundle to a function or method, such as Commands::spawn(). If the bundle contains a unit, the lint will emit a warning.

§Motivation

It is possible to create bundles with a unit () component, since unit implements Bundle. Unit is not a Component, however, and will be ignored instead of added to the entity. Often, inserting a unit is unintentional and is a sign that the author intended to do something else.

§Example

fn spawn(mut commands: Commands) {
    commands.spawn(());

    commands.spawn((
        Name::new("Decal"),
        // This is likely a mistake! `Transform::rotate_z()` returns a unit `()`, not a
        // `Transform`! As such, no `Transform` will be inserted into the entity.
        Transform::from_translation(Vec3::new(0.75, 0.0, 0.0))
            .rotate_z(PI / 4.0),
    ));
}

Use instead:

fn spawn(mut commands: Commands) {
    // `Commands::spawn_empty()` is preferred if you do not need to add any components.
    commands.spawn_empty();

    commands.spawn((
        Name::new("Decal"),
        // `Transform::with_rotation()` returns a `Transform`, which was the intended behavior.
        Transform::from_translation(Vec3::new(0.75, 0.0, 0.0))
            .with_rotation(Quat::from_rotation_z(PI / 4.0)),
    ));
}