Skip to content

Commit d8974e7

Browse files
committed
small and mostly pointless refactoring (bevyengine#2934)
What is says on the tin. This has got more to do with making `clippy` slightly more *quiet* than it does with changing anything that might greatly impact readability or performance. that said, deriving `Default` for a couple of structs is a nice easy win
1 parent 803e8cd commit d8974e7

File tree

99 files changed

+329
-304
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+329
-304
lines changed

crates/bevy_app/src/app.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,10 @@ impl App {
373373
system: impl IntoSystemDescriptor<Params>,
374374
) -> &mut Self {
375375
use std::any::TypeId;
376-
if stage_label.type_id() == TypeId::of::<StartupStage>() {
377-
panic!("add systems to a startup stage using App::add_startup_system_to_stage");
378-
}
376+
assert!(
377+
stage_label.type_id() != TypeId::of::<StartupStage>(),
378+
"add systems to a startup stage using App::add_startup_system_to_stage"
379+
);
379380
self.schedule.add_system_to_stage(stage_label, system);
380381
self
381382
}
@@ -407,9 +408,10 @@ impl App {
407408
system_set: SystemSet,
408409
) -> &mut Self {
409410
use std::any::TypeId;
410-
if stage_label.type_id() == TypeId::of::<StartupStage>() {
411-
panic!("add system sets to a startup stage using App::add_startup_system_set_to_stage");
412-
}
411+
assert!(
412+
stage_label.type_id() != TypeId::of::<StartupStage>(),
413+
"add system sets to a startup stage using App::add_startup_system_set_to_stage"
414+
);
413415
self.schedule
414416
.add_system_set_to_stage(stage_label, system_set);
415417
self

crates/bevy_asset/src/asset_server.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,11 @@ impl AssetServer {
355355
});
356356

357357
// load asset dependencies and prepare asset type hashmap
358-
for (label, loaded_asset) in load_context.labeled_assets.iter_mut() {
358+
for (label, loaded_asset) in &mut load_context.labeled_assets {
359359
let label_id = LabelId::from(label.as_ref().map(|label| label.as_str()));
360360
let type_uuid = loaded_asset.value.as_ref().unwrap().type_uuid();
361361
source_info.asset_types.insert(label_id, type_uuid);
362-
for dependency in loaded_asset.dependencies.iter() {
362+
for dependency in &loaded_asset.dependencies {
363363
self.load_untracked(dependency.clone(), false);
364364
}
365365
}
@@ -484,7 +484,7 @@ impl AssetServer {
484484

485485
fn create_assets_in_load_context(&self, load_context: &mut LoadContext) {
486486
let asset_lifecycles = self.server.asset_lifecycles.read();
487-
for (label, asset) in load_context.labeled_assets.iter_mut() {
487+
for (label, asset) in &mut load_context.labeled_assets {
488488
let asset_value = asset
489489
.value
490490
.take()
@@ -674,7 +674,7 @@ mod test {
674674
extensions == vec!["v1.2.3.pong", "2.3.pong", "3.pong", "pong"],
675675
_ => false,
676676
}
677-
)
677+
);
678678
}
679679

680680
#[test]

crates/bevy_asset/src/assets.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -216,22 +216,22 @@ impl<T: Asset> Assets<T> {
216216
///
217217
/// Keeps the allocated memory for reuse.
218218
pub fn clear(&mut self) {
219-
self.assets.clear()
219+
self.assets.clear();
220220
}
221221

222222
/// Reserves capacity for at least additional more elements to be inserted into the assets.
223223
///
224224
/// The collection may reserve more space to avoid frequent reallocations.
225225
pub fn reserve(&mut self, additional: usize) {
226-
self.assets.reserve(additional)
226+
self.assets.reserve(additional);
227227
}
228228

229229
/// Shrinks the capacity of the asset map as much as possible.
230230
///
231231
/// It will drop down as much as possible while maintaining the internal rules and possibly
232232
/// leaving some space in accordance with the resize policy.
233233
pub fn shrink_to_fit(&mut self) {
234-
self.assets.shrink_to_fit()
234+
self.assets.shrink_to_fit();
235235
}
236236

237237
pub fn asset_event_system(
@@ -241,7 +241,7 @@ impl<T: Asset> Assets<T> {
241241
// Check if the events are empty before calling `drain`.
242242
// As `drain` triggers change detection.
243243
if !assets.events.is_empty() {
244-
events.send_batch(assets.events.drain())
244+
events.send_batch(assets.events.drain());
245245
}
246246
}
247247

@@ -331,6 +331,6 @@ mod tests {
331331
let handle = assets_before.add(MyAsset);
332332
app.add_asset::<MyAsset>(); // Ensure this doesn't overwrite the Asset
333333
let assets_after = app.world.get_resource_mut::<Assets<MyAsset>>().unwrap();
334-
assert!(assets_after.get(handle).is_some())
334+
assert!(assets_after.get(handle).is_some());
335335
}
336336
}

crates/bevy_asset/src/handle.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,9 @@ impl<T: Asset> Handle<T> {
179179
}
180180

181181
#[inline]
182+
#[must_use]
182183
pub fn clone_weak(&self) -> Self {
183-
Handle::weak(self.id)
184+
Self::weak(self.id)
184185
}
185186

186187
pub fn clone_untyped(&self) -> HandleUntyped {
@@ -327,8 +328,9 @@ impl HandleUntyped {
327328
}
328329
}
329330

330-
pub fn clone_weak(&self) -> HandleUntyped {
331-
HandleUntyped::weak(self.id)
331+
#[must_use]
332+
pub fn clone_weak(&self) -> Self {
333+
Self::weak(self.id)
332334
}
333335

334336
pub fn is_weak(&self) -> bool {
@@ -344,9 +346,10 @@ impl HandleUntyped {
344346
/// The new handle will maintain the Strong or Weak status of the current handle.
345347
pub fn typed<T: Asset>(mut self) -> Handle<T> {
346348
if let HandleId::Id(type_uuid, _) = self.id {
347-
if T::TYPE_UUID != type_uuid {
348-
panic!("Attempted to convert handle to invalid type.");
349-
}
349+
assert!(
350+
T::TYPE_UUID == type_uuid,
351+
"Attempted to convert handle to invalid type."
352+
);
350353
}
351354
let handle_type = match &self.handle_type {
352355
HandleType::Strong(sender) => HandleType::Strong(sender.clone()),

crates/bevy_asset/src/io/file_asset_io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pub fn filesystem_watcher_system(asset_server: Res<AssetServer>) {
155155
..
156156
} = event
157157
{
158-
for path in paths.iter() {
158+
for path in &paths {
159159
if !changed.contains(path) {
160160
let relative_path = path.strip_prefix(&asset_io.root_path).unwrap();
161161
let _ = asset_server.load_untracked(relative_path.into(), true);

crates/bevy_asset/src/loader.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ impl<T: Asset> LoadedAsset<T> {
4747
self.dependencies.push(asset_path.to_owned());
4848
}
4949

50+
#[must_use]
5051
pub fn with_dependency(mut self, asset_path: AssetPath) -> Self {
5152
self.add_dependency(asset_path);
5253
self
5354
}
5455

56+
#[must_use]
5557
pub fn with_dependencies(mut self, mut asset_paths: Vec<AssetPath<'static>>) -> Self {
5658
for asset_path in asset_paths.drain(..) {
5759
self.add_dependency(asset_path);
@@ -132,7 +134,7 @@ impl<'a> LoadContext<'a> {
132134

133135
pub fn get_asset_metas(&self) -> Vec<AssetMeta> {
134136
let mut asset_metas = Vec::new();
135-
for (label, asset) in self.labeled_assets.iter() {
137+
for (label, asset) in &self.labeled_assets {
136138
asset_metas.push(AssetMeta {
137139
dependencies: asset.dependencies.clone(),
138140
label: label.clone(),
@@ -182,7 +184,7 @@ impl<T: AssetDynamic> AssetLifecycle for AssetLifecycleChannel<T> {
182184
id,
183185
version,
184186
}))
185-
.unwrap()
187+
.unwrap();
186188
} else {
187189
panic!(
188190
"Failed to downcast asset to {}.",

crates/bevy_core/src/float_ord.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ impl Hash for FloatOrd {
4141
fn hash<H: Hasher>(&self, state: &mut H) {
4242
if self.0.is_nan() {
4343
// Ensure all NaN representations hash to the same value
44-
state.write(bytemuck::bytes_of(&f32::NAN))
44+
state.write(bytemuck::bytes_of(&f32::NAN));
4545
} else if self.0 == 0.0 {
4646
// Ensure both zeroes hash to the same value
47-
state.write(bytemuck::bytes_of(&0.0f32))
47+
state.write(bytemuck::bytes_of(&0.0f32));
4848
} else {
4949
state.write(bytemuck::bytes_of(&self.0));
5050
}

crates/bevy_core/src/time/fixed_timestep.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ impl FixedTimestep {
109109

110110
/// Sets the label for the timestep. Setting a label allows a timestep
111111
/// to be observed by the global [`FixedTimesteps`] resource.
112+
#[must_use]
112113
pub fn with_label(mut self, label: &str) -> Self {
113114
self.state.label = Some(label.to_string());
114115
self
@@ -197,7 +198,7 @@ impl System for FixedTimestep {
197198
}
198199

199200
fn apply_buffers(&mut self, world: &mut World) {
200-
self.internal_system.apply_buffers(world)
201+
self.internal_system.apply_buffers(world);
201202
}
202203

203204
fn initialize(&mut self, world: &mut World) {

crates/bevy_core/src/time/timer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl Timer {
180180
self.stopwatch.reset();
181181
self.finished = self.just_finished();
182182
}
183-
self.repeating = repeating
183+
self.repeating = repeating;
184184
}
185185

186186
/// Advance the timer by `delta` seconds.

crates/bevy_core_pipeline/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ pub fn extract_clear_color(clear_color: Res<ClearColor>, mut render_world: ResMu
334334
// If the clear color has changed
335335
if clear_color.is_changed() {
336336
// Update the clear color resource in the render world
337-
render_world.insert_resource(clear_color.clone())
337+
render_world.insert_resource(clear_color.clone());
338338
}
339339
}
340340

crates/bevy_core_pipeline/src/main_pass_2d.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl Node for MainPass2dNode {
6363

6464
let mut draw_functions = draw_functions.write();
6565
let mut tracked_pass = TrackedRenderPass::new(render_pass);
66-
for item in transparent_phase.items.iter() {
66+
for item in &transparent_phase.items {
6767
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
6868
draw_function.draw(world, &mut tracked_pass, view_entity, item);
6969
}

crates/bevy_core_pipeline/src/main_pass_3d.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl Node for MainPass3dNode {
8282
.begin_render_pass(&pass_descriptor);
8383
let mut draw_functions = draw_functions.write();
8484
let mut tracked_pass = TrackedRenderPass::new(render_pass);
85-
for item in opaque_phase.items.iter() {
85+
for item in &opaque_phase.items {
8686
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
8787
draw_function.draw(world, &mut tracked_pass, view_entity, item);
8888
}
@@ -116,7 +116,7 @@ impl Node for MainPass3dNode {
116116
.begin_render_pass(&pass_descriptor);
117117
let mut draw_functions = draw_functions.write();
118118
let mut tracked_pass = TrackedRenderPass::new(render_pass);
119-
for item in alpha_mask_phase.items.iter() {
119+
for item in &alpha_mask_phase.items {
120120
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
121121
draw_function.draw(world, &mut tracked_pass, view_entity, item);
122122
}
@@ -154,7 +154,7 @@ impl Node for MainPass3dNode {
154154
.begin_render_pass(&pass_descriptor);
155155
let mut draw_functions = draw_functions.write();
156156
let mut tracked_pass = TrackedRenderPass::new(render_pass);
157-
for item in transparent_phase.items.iter() {
157+
for item in &transparent_phase.items {
158158
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
159159
draw_function.draw(world, &mut tracked_pass, view_entity, item);
160160
}

crates/bevy_derive/src/bevy_main.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use syn::{parse_macro_input, ItemFn};
44

55
pub fn bevy_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
66
let input = parse_macro_input!(item as ItemFn);
7-
if input.sig.ident != "main" {
8-
panic!("`bevy_main` can only be used on a function called 'main'.")
9-
}
7+
assert!(
8+
input.sig.ident == "main",
9+
"`bevy_main` can only be used on a function called 'main'.",
10+
);
1011

1112
TokenStream::from(quote! {
1213
#[no_mangle]

crates/bevy_derive/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ pub fn derive_app_label(input: TokenStream) -> TokenStream {
3030
let input = syn::parse_macro_input!(input as syn::DeriveInput);
3131
let mut trait_path = BevyManifest::default().get_path("bevy_app");
3232
trait_path.segments.push(format_ident!("AppLabel").into());
33-
derive_label(input, trait_path)
33+
derive_label(input, &trait_path)
3434
}

crates/bevy_diagnostic/src/diagnostic.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl Diagnostic {
6565
"Diagnostic {:?} has name longer than {} characters, and so might overflow in the LogDiagnosticsPlugin\
6666
Consider using a shorter name.",
6767
name, MAX_DIAGNOSTIC_NAME_WIDTH
68-
)
68+
);
6969
}
7070
Diagnostic {
7171
id,
@@ -77,6 +77,7 @@ impl Diagnostic {
7777
}
7878
}
7979

80+
#[must_use]
8081
pub fn with_suffix(mut self, suffix: impl Into<Cow<'static, str>>) -> Self {
8182
self.suffix = suffix.into();
8283
self

crates/bevy_ecs/macros/src/component.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn derive_component(input: TokenStream) -> TokenStream {
1313
Err(e) => return e.into_compile_error().into(),
1414
};
1515

16-
let storage = storage_path(bevy_ecs_path.clone(), attrs.storage);
16+
let storage = storage_path(&bevy_ecs_path, attrs.storage);
1717

1818
ast.generics
1919
.make_where_clause()
@@ -96,7 +96,7 @@ fn parse_component_attr(ast: &DeriveInput) -> Result<Attrs> {
9696
Ok(attrs)
9797
}
9898

99-
fn storage_path(bevy_ecs_path: Path, ty: StorageTy) -> TokenStream2 {
99+
fn storage_path(bevy_ecs_path: &Path, ty: StorageTy) -> TokenStream2 {
100100
let typename = match ty {
101101
StorageTy::Table => Ident::new("TableStorage", Span::call_site()),
102102
StorageTy::SparseSet => Ident::new("SparseStorage", Span::call_site()),

crates/bevy_ecs/macros/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ pub fn derive_system_label(input: TokenStream) -> TokenStream {
433433
trait_path
434434
.segments
435435
.push(format_ident!("SystemLabel").into());
436-
derive_label(input, trait_path)
436+
derive_label(input, &trait_path)
437437
}
438438

439439
#[proc_macro_derive(StageLabel)]
@@ -442,7 +442,7 @@ pub fn derive_stage_label(input: TokenStream) -> TokenStream {
442442
let mut trait_path = bevy_ecs_path();
443443
trait_path.segments.push(format_ident!("schedule").into());
444444
trait_path.segments.push(format_ident!("StageLabel").into());
445-
derive_label(input, trait_path)
445+
derive_label(input, &trait_path)
446446
}
447447

448448
#[proc_macro_derive(AmbiguitySetLabel)]
@@ -453,7 +453,7 @@ pub fn derive_ambiguity_set_label(input: TokenStream) -> TokenStream {
453453
trait_path
454454
.segments
455455
.push(format_ident!("AmbiguitySetLabel").into());
456-
derive_label(input, trait_path)
456+
derive_label(input, &trait_path)
457457
}
458458

459459
#[proc_macro_derive(RunCriteriaLabel)]
@@ -464,7 +464,7 @@ pub fn derive_run_criteria_label(input: TokenStream) -> TokenStream {
464464
trait_path
465465
.segments
466466
.push(format_ident!("RunCriteriaLabel").into());
467-
derive_label(input, trait_path)
467+
derive_label(input, &trait_path)
468468
}
469469

470470
pub(crate) fn bevy_ecs_path() -> syn::Path {

crates/bevy_ecs/src/archetype.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ impl Archetypes {
520520
}
521521

522522
pub fn clear_entities(&mut self) {
523-
for archetype in self.archetypes.iter_mut() {
523+
for archetype in &mut self.archetypes {
524524
archetype.clear_entities();
525525
}
526526
}

crates/bevy_ecs/src/bundle.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ impl<'a, 'b> BundleInserter<'a, 'b> {
489489
{
490490
&mut *self.archetype
491491
} else if new_archetype.id() == swapped_location.archetype_id {
492-
&mut *new_archetype
492+
new_archetype
493493
} else {
494494
// SAFE: the only two borrowed archetypes are above and we just did collision checks
495495
&mut *self
@@ -630,9 +630,11 @@ unsafe fn initialize_bundle(
630630
let mut deduped = component_ids.clone();
631631
deduped.sort();
632632
deduped.dedup();
633-
if deduped.len() != component_ids.len() {
634-
panic!("Bundle {} has duplicate components", bundle_type_name);
635-
}
633+
assert!(
634+
deduped.len() == component_ids.len(),
635+
"Bundle {} has duplicate components",
636+
bundle_type_name
637+
);
636638

637639
BundleInfo {
638640
id,

crates/bevy_ecs/src/component.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub struct ComponentDescriptor {
179179
impl ComponentDescriptor {
180180
// SAFETY: The pointer points to a valid value of type `T` and it is safe to drop this value.
181181
unsafe fn drop_ptr<T>(x: *mut u8) {
182-
x.cast::<T>().drop_in_place()
182+
x.cast::<T>().drop_in_place();
183183
}
184184

185185
pub fn new<T: Component>() -> Self {

0 commit comments

Comments
 (0)