From f5039a476d48ec19bd74c94e83e10772565c68f1 Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Sun, 23 Jan 2022 14:24:37 +0000 Subject: [PATCH] Mark .id() methods which return an `Entity` as must_use (#3750) # 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] --- crates/bevy_ecs/src/lib.rs | 10 ++-------- crates/bevy_ecs/src/system/commands/mod.rs | 1 + crates/bevy_ecs/src/system/mod.rs | 4 ++-- crates/bevy_ecs/src/world/entity_ref.rs | 2 ++ 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index 30d70ca17e..04731b45cf 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -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 diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 6de329856c..e2a2a5a222 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -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 } diff --git a/crates/bevy_ecs/src/system/mod.rs b/crates/bevy_ecs/src/system/mod.rs index a5b4c68bce..9eec231db3 100644 --- a/crates/bevy_ecs/src/system/mod.rs +++ b/crates/bevy_ecs/src/system/mod.rs @@ -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::>(); fn validate_remove( diff --git a/crates/bevy_ecs/src/world/entity_ref.rs b/crates/bevy_ecs/src/world/entity_ref.rs index b85fa2b74f..eda55333fb 100644 --- a/crates/bevy_ecs/src/world/entity_ref.rs +++ b/crates/bevy_ecs/src/world/entity_ref.rs @@ -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 } @@ -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 }