Skip to content

Commit

Permalink
Mark .id() methods which return an Entity as must_use (#3750)
Browse files Browse the repository at this point in the history
# Objective

- Calling .id() has no purpose unless you use the Entity returned
- This is an easy source of confusion for beginners.
- This is easily missed during refactors.

## Solution

- Mark the appropriate methods as #[must_use]
  • Loading branch information
alice-i-cecile committed Jan 23, 2022
1 parent f3de12b commit f5039a4
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 10 deletions.
10 changes: 2 additions & 8 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,18 +1130,12 @@ mod tests {
#[test]
fn remove_bundle() {
let mut world = World::default();
world
.spawn()
.insert_bundle((A(1), B(1), TableStored("1")))
.id();
world.spawn().insert_bundle((A(1), B(1), TableStored("1")));
let e2 = world
.spawn()
.insert_bundle((A(2), B(2), TableStored("2")))
.id();
world
.spawn()
.insert_bundle((A(3), B(3), TableStored("3")))
.id();
world.spawn().insert_bundle((A(3), B(3), TableStored("3")));

let mut query = world.query::<(&B, &TableStored)>();
let results = query
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ecs/src/system/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ impl<'w, 's, 'a> EntityCommands<'w, 's, 'a> {
/// # my_system.system();
/// ```
#[inline]
#[must_use = "Omit the .id() call if you do not need to store the `Entity` identifier."]
pub fn id(&self) -> Entity {
self.entity
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,8 @@ mod tests {
world.clear_trackers();

// Then, try removing a component
world.spawn().insert(W(3)).id();
world.spawn().insert(W(4)).id();
world.spawn().insert(W(3));
world.spawn().insert(W(4));
world.entity_mut(entity_to_remove_w_from).remove::<W<i32>>();

fn validate_remove(
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_ecs/src/world/entity_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl<'w> EntityRef<'w> {
}

#[inline]
#[must_use = "Omit the .id() call if you do not need to store the `Entity` identifier."]
pub fn id(&self) -> Entity {
self.entity
}
Expand Down Expand Up @@ -113,6 +114,7 @@ impl<'w> EntityMut<'w> {
}

#[inline]
#[must_use = "Omit the .id() call if you do not need to store the `Entity` identifier."]
pub fn id(&self) -> Entity {
self.entity
}
Expand Down

0 comments on commit f5039a4

Please sign in to comment.