Skip to content

Commit

Permalink
chore: change lint message, feat: add short documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
DaAlbrecht committed Jan 13, 2025
1 parent 6fabdc9 commit 578fd5b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
39 changes: 37 additions & 2 deletions bevy_lint/src/lints/unit_component_insertion.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
//! Checks for method or function calls inside of `commands.spawn` that return unit `()`
//!
//! # Motivation
//!
//! In Bevy, the `commands.spawn` method is used to create entities in the ECS with the given
//! `Bundle`. A `Bundle` can be a tuple of `Component` that should be added to this entity. If a
//! Value of type `()` is mistakenly passed, it results in an empty component being added.
//!
//! # Example
//!
//! ```rust
//! # use bevy::prelude::*;
//! # use std::f32::consts::PI;
//! #
//! fn main() {
//! App::new().add_systems(Startup, test);
//! }
//!
//! fn test(mut commands: Commands) {
//! commands.spawn((
//! Name::new("Decal"),
//! Transform::from_translation(Vec3::new(0.75, 0.0, 0.0)).rotate_z(PI / 4.0),
//! ));
//! }
//! ```
//!
//! ```text
//! warning: Expression returns `unit` and results in an empty component insertion
//! --> src/main.rs:15:64
//! |
//! 15 | ...Vec3::new(0.75, 0.0, 0.0)).rotate_z(PI / 4.0),
//! | ^^^^^^^^
//! |
//! = note: `#[warn(bevy::unit_component_insertion)]` on by default
//! ```
use std::ops::ControlFlow;

use clippy_utils::{diagnostics::span_lint, sym, ty::match_type, visitors::for_each_expr};
Expand All @@ -11,7 +46,7 @@ use crate::declare_bevy_lint;
declare_bevy_lint! {
pub UNIT_COMPONENT_INSERTION,
SUSPICIOUS,
"method returns `()` and will spawn an empty bundle",
"method returns `unit` and will be inserted as a component",
}

impl_lint_pass! {
Expand Down Expand Up @@ -74,7 +109,7 @@ impl<'tcx> LateLintPass<'tcx> for UnitComponentInsertion {
cx,
UNIT_COMPONENT_INSERTION.lint,
span,
"Expression returns `()` and results in an empty bundle being inserted",
"Expression returns `unit` and results in an empty component insertion",
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion bevy_lint/tests/ui/unit_component_insertion/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ fn test(mut commands: Commands) {
commands.spawn((
Name::new("Decal"),
Transform::from_translation(Vec3::new(0.75, 0.0, 0.0)).rotate_z(PI / 4.0),
//~^ ERROR: Expression returns `()` and results in an empty bundle being inserted
//~^ ERROR: Expression returns `unit` and results in an empty component insertion
));
}
2 changes: 1 addition & 1 deletion bevy_lint/tests/ui/unit_component_insertion/main.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: Expression returns `()` and results in an empty bundle being inserted
error: Expression returns `unit` and results in an empty component insertion
--> tests/ui/unit_component_insertion/main.rs:15:64
|
15 | Transform::from_translation(Vec3::new(0.75, 0.0, 0.0)).rotate_z(PI / 4.0),
Expand Down

0 comments on commit 578fd5b

Please sign in to comment.