Skip to content

Warn when unit () is inserted as a value #21103

@ChristopherBiscardi

Description

@ChristopherBiscardi

What problem does this solve or what need does it fill?

This program (note: look_at) appears to insert a Transform, but actually inserts a unit value because Transform::look_at doesn't return a Transform.
This results in the debug system never finding the non-existent Transform at best, and getting a default Transform if another Component that requires a Transform inserts a Default value (such as Mesh3d).

use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, startup)
        .add_systems(Update, debug)
        .run();
}

fn debug(entities: Query<(&Name, &Transform)>) {
    for (name, transform) in &entities {
        if ["Parent"].contains(&name.as_str()) {
            info!(?name, transform=?transform.translation);
        }
    }
}

fn startup(mut commands: Commands) {
    commands.spawn((
        Name::new("Parent"),
        Transform::from_xyz(10.0, 0.0, 0.0)
            .look_at(Vec3::ZERO, Vec3::Y),
    ));
}

What solution would you like?

  • functions that accept exclusive references and return (), like Transform::look_at, should link to versions that return Self like Transform::looking_at if applicable in the documentation
  • A warning should be logged if () is inserted as a component value, instead of just ignoring it

What alternative(s) have you considered?

The bevy linter includes a lint for this called unit_in_bundle with a very similar Transform example in the showcase. We could encourage use of the bevy linter instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-DocsAn addition or correction to our documentationC-FeatureA new feature, making something new possibleS-Needs-DesignThis issue requires design work to think about how it would best be accomplished

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions