diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index cceafa0dbc..2505041f89 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -26,7 +26,7 @@ jobs: - uses: actions-rs/toolchain@v1 with: toolchain: nightly - components: rustfmt + components: rustfmt, clippy - name: Install alsa run: sudo apt-get install libasound2-dev @@ -37,6 +37,15 @@ jobs: - name: Check the format run: cargo +nightly fmt --all -- --check + # type complexity must be ignored because we use huge templates for queries + - name: Run clippy + run: > + cargo +nightly clippy + --all-targets + --all-features + -- + -D warnings + -A clippy::type_complexity + - name: Run tests run: cargo test --workspace - diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index cff6dcb93b..d5d1cfbc3c 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -1,6 +1,7 @@ use crate::app_builder::AppBuilder; use bevy_ecs::{ParallelExecutor, Resources, Schedule, World}; +#[allow(clippy::needless_doctest_main)] /// Containers of app logic and data /// /// App store the ECS World, Resources, Schedule, and Executor. They also store the "run" function of the App, which diff --git a/crates/bevy_app/src/app_builder.rs b/crates/bevy_app/src/app_builder.rs index 9873f9921d..f83e5f8bee 100644 --- a/crates/bevy_app/src/app_builder.rs +++ b/crates/bevy_app/src/app_builder.rs @@ -39,7 +39,7 @@ impl AppBuilder { } pub fn run(&mut self) { - let app = std::mem::replace(&mut self.app, App::default()); + let app = std::mem::take(&mut self.app); app.run(); } @@ -209,7 +209,7 @@ impl AppBuilder { where R: FromResources + Send + Sync + 'static, { - let resource = R::from_resources(&mut self.app.resources); + let resource = R::from_resources(&self.app.resources); self.app.resources.insert(resource); self diff --git a/crates/bevy_app/src/event.rs b/crates/bevy_app/src/event.rs index 8a907cc38e..f9d144a130 100644 --- a/crates/bevy_app/src/event.rs +++ b/crates/bevy_app/src/event.rs @@ -159,7 +159,7 @@ impl EventReader { events: &'a Events, predicate: impl FnMut(&&T) -> bool, ) -> Option<&'a T> { - self.iter(events).rev().filter(predicate).next() + self.iter(events).rev().find(predicate) } /// Retrieves the earliest event in `events` that this reader hasn't seen yet. This updates the EventReader's diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index 1825a1c1a8..01aea3b966 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -172,7 +172,7 @@ impl AssetServer { pub fn watch_for_changes(&self) -> Result<(), AssetServerError> { let mut filesystem_watcher = self.filesystem_watcher.write().unwrap(); - let _ = filesystem_watcher.get_or_insert_with(|| FilesystemWatcher::default()); + let _ = filesystem_watcher.get_or_insert_with(FilesystemWatcher::default); // watch current files let asset_info_paths = self.asset_info_paths.read().unwrap(); for asset_path in asset_info_paths.keys() { @@ -186,44 +186,37 @@ impl AssetServer { pub fn filesystem_watcher_system(asset_server: Res) { use notify::event::{Event, EventKind, ModifyKind}; let mut changed = HashSet::new(); - loop { - let result = if let Some(filesystem_watcher) = - asset_server.filesystem_watcher.read().unwrap().as_ref() - { - match filesystem_watcher.receiver.try_recv() { - Ok(result) => result, - Err(TryRecvError::Empty) => { - break; - } - Err(TryRecvError::Disconnected) => panic!("FilesystemWatcher disconnected"), + + while let Some(filesystem_watcher) = + asset_server.filesystem_watcher.read().unwrap().as_ref() + { + let result = match filesystem_watcher.receiver.try_recv() { + Ok(result) => result, + Err(TryRecvError::Empty) => { + break; } - } else { - break; + Err(TryRecvError::Disconnected) => panic!("FilesystemWatcher disconnected"), }; let event = result.unwrap(); - match event { - Event { - kind: EventKind::Modify(ModifyKind::Data(_)), - paths, - .. - } => { - for path in paths.iter() { - if !changed.contains(path) { - let root_path = asset_server.get_root_path().unwrap(); - let relative_path = path.strip_prefix(root_path).unwrap(); - match asset_server.load_untyped(relative_path) { - Ok(_) => {} - Err(AssetServerError::AssetLoadError(error)) => { - panic!("{:?}", error) - } - Err(_) => {} - } + if let Event { + kind: EventKind::Modify(ModifyKind::Data(_)), + paths, + .. + } = event + { + for path in paths.iter() { + if !changed.contains(path) { + let root_path = asset_server.get_root_path().unwrap(); + let relative_path = path.strip_prefix(root_path).unwrap(); + match asset_server.load_untyped(relative_path) { + Ok(_) => {} + Err(AssetServerError::AssetLoadError(error)) => panic!("{:?}", error), + Err(_) => {} } } - changed.extend(paths); } - _ => {} + changed.extend(paths); } } } @@ -244,8 +237,7 @@ impl AssetServer { // TODO: add type checking here. people shouldn't be able to request a Handle for a Mesh asset pub fn load>(&self, path: P) -> Result, AssetServerError> { - self.load_untyped(path) - .map(|handle_id| Handle::from(handle_id)) + self.load_untyped(path).map(Handle::from) } pub fn load_sync>( @@ -338,15 +330,11 @@ impl AssetServer { } pub fn set_load_state(&self, handle_id: HandleId, load_state: LoadState) { - self.asset_info - .write() - .unwrap() - .get_mut(&handle_id) - .map(|asset_info| { - if load_state.get_version() >= asset_info.load_state.get_version() { - asset_info.load_state = load_state; - } - }); + if let Some(asset_info) = self.asset_info.write().unwrap().get_mut(&handle_id) { + if load_state.get_version() >= asset_info.load_state.get_version() { + asset_info.load_state = load_state; + } + } } pub fn get_load_state_untyped(&self, handle_id: HandleId) -> Option { @@ -453,7 +441,7 @@ impl AssetServer { ) { Ok(handle) => handle, Err(AssetServerError::MissingAssetHandler) => continue, - Err(err) => Err(err)?, + Err(err) => return Err(err), }; handle_ids.push(handle); diff --git a/crates/bevy_asset/src/handle.rs b/crates/bevy_asset/src/handle.rs index 01b43df487..c51bc60f15 100644 --- a/crates/bevy_asset/src/handle.rs +++ b/crates/bevy_asset/src/handle.rs @@ -17,6 +17,7 @@ pub(crate) const DEFAULT_HANDLE_ID: HandleId = pub struct HandleId(pub Uuid); impl HandleId { + #[allow(clippy::new_without_default)] pub fn new() -> HandleId { HandleId(Uuid::new_v4()) } @@ -151,7 +152,7 @@ impl Default for Handle { impl Clone for Handle { fn clone(&self) -> Self { Handle { - id: self.id.clone(), + id: self.id, marker: PhantomData, } } diff --git a/crates/bevy_asset/src/loader.rs b/crates/bevy_asset/src/loader.rs index e0dda22fbe..db398a17d3 100644 --- a/crates/bevy_asset/src/loader.rs +++ b/crates/bevy_asset/src/loader.rs @@ -47,6 +47,7 @@ pub struct AssetChannel { } impl AssetChannel { + #[allow(clippy::new_without_default)] pub fn new() -> Self { let (sender, receiver) = crossbeam_channel::unbounded(); AssetChannel { sender, receiver } diff --git a/crates/bevy_core/src/bytes.rs b/crates/bevy_core/src/bytes.rs index b10132d23e..efcbdf26fb 100644 --- a/crates/bevy_core/src/bytes.rs +++ b/crates/bevy_core/src/bytes.rs @@ -144,7 +144,7 @@ where T: FromBytes, { fn from_bytes(bytes: &[u8]) -> Self { - if bytes.len() == 0 { + if bytes.is_empty() { None } else { Some(T::from_bytes(bytes)) diff --git a/crates/bevy_core/src/float_ord.rs b/crates/bevy_core/src/float_ord.rs index 29d1545cdb..74496b7756 100644 --- a/crates/bevy_core/src/float_ord.rs +++ b/crates/bevy_core/src/float_ord.rs @@ -11,6 +11,7 @@ use std::{ #[derive(Debug, Copy, Clone, PartialOrd)] pub struct FloatOrd(pub f32); +#[allow(clippy::derive_ord_xor_partial_ord)] impl Ord for FloatOrd { fn cmp(&self, other: &Self) -> Ordering { self.0.partial_cmp(&other.0).unwrap_or_else(|| { diff --git a/crates/bevy_core/src/label.rs b/crates/bevy_core/src/label.rs index 9f37cb4ada..262d9ca5a3 100644 --- a/crates/bevy_core/src/label.rs +++ b/crates/bevy_core/src/label.rs @@ -77,7 +77,7 @@ pub(crate) fn entity_labels_system( let current_labels = entity_labels .entity_labels .entry(entity) - .or_insert_with(|| HashSet::default()); + .or_insert_with(HashSet::default); for removed_label in current_labels.difference(&labels.labels) { if let Some(entities) = entity_labels.label_entities.get_mut(removed_label) { entities.retain(|e| *e != entity); diff --git a/crates/bevy_core/src/time/mod.rs b/crates/bevy_core/src/time/mod.rs index 380aa67e6e..ce3863df40 100644 --- a/crates/bevy_core/src/time/mod.rs +++ b/crates/bevy_core/src/time/mod.rs @@ -1,3 +1,4 @@ +#[allow(clippy::module_inception)] mod time; mod timer; diff --git a/crates/bevy_derive/src/as_vertex_buffer_descriptor.rs b/crates/bevy_derive/src/as_vertex_buffer_descriptor.rs index a04e6126cf..fbe42bd422 100644 --- a/crates/bevy_derive/src/as_vertex_buffer_descriptor.rs +++ b/crates/bevy_derive/src/as_vertex_buffer_descriptor.rs @@ -12,7 +12,7 @@ struct VertexAttributes { pub instance: bool, } -static VERTEX_ATTRIBUTE_NAME: &'static str = "vertex"; +static VERTEX_ATTRIBUTE_NAME: &str = "vertex"; pub fn derive_as_vertex_buffer_descriptor(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); @@ -34,26 +34,21 @@ pub fn derive_as_vertex_buffer_descriptor(input: TokenStream) -> TokenStream { field .attrs .iter() - .find(|a| { - a.path.get_ident().as_ref().unwrap().to_string() == VERTEX_ATTRIBUTE_NAME - }) - .map_or_else( - || VertexAttributes::default(), - |a| { - syn::custom_keyword!(ignore); - let mut vertex_attributes = VertexAttributes::default(); - a.parse_args_with(|input: ParseStream| { - if let Some(_) = input.parse::>()? { - vertex_attributes.ignore = true; - return Ok(()); - } - Ok(()) - }) - .expect("invalid 'vertex' attribute format"); + .find(|a| *a.path.get_ident().as_ref().unwrap() == VERTEX_ATTRIBUTE_NAME) + .map_or_else(VertexAttributes::default, |a| { + syn::custom_keyword!(ignore); + let mut vertex_attributes = VertexAttributes::default(); + a.parse_args_with(|input: ParseStream| { + if input.parse::>()?.is_some() { + vertex_attributes.ignore = true; + return Ok(()); + } + Ok(()) + }) + .expect("invalid 'vertex' attribute format"); - vertex_attributes - }, - ), + vertex_attributes + }), ) }) .collect::>(); diff --git a/crates/bevy_derive/src/modules.rs b/crates/bevy_derive/src/modules.rs index 92d032d456..75a7b9c31f 100644 --- a/crates/bevy_derive/src/modules.rs +++ b/crates/bevy_derive/src/modules.rs @@ -44,9 +44,9 @@ pub fn get_modules(attributes: &[Attribute]) -> Modules { }; for attribute in attributes.iter() { - if attribute.path.get_ident().as_ref().unwrap().to_string() == AS_CRATE_ATTRIBUTE_NAME { + if *attribute.path.get_ident().as_ref().unwrap() == AS_CRATE_ATTRIBUTE_NAME { let value = attribute.tokens.to_string(); - if &value[1..value.len() - 1] == modules.bevy_render { + if value[1..value.len() - 1] == modules.bevy_render { modules.bevy_render = "crate".to_string(); } } diff --git a/crates/bevy_derive/src/render_resources.rs b/crates/bevy_derive/src/render_resources.rs index 5184dc484d..449d7216c7 100644 --- a/crates/bevy_derive/src/render_resources.rs +++ b/crates/bevy_derive/src/render_resources.rs @@ -16,7 +16,7 @@ struct RenderResourceAttributes { pub from_self: bool, } -static RENDER_RESOURCE_ATTRIBUTE_NAME: &'static str = "render_resources"; +static RENDER_RESOURCE_ATTRIBUTE_NAME: &str = "render_resources"; pub fn derive_render_resources(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); @@ -26,25 +26,20 @@ pub fn derive_render_resources(input: TokenStream) -> TokenStream { let attributes = ast .attrs .iter() - .find(|a| { - a.path.get_ident().as_ref().unwrap().to_string() == RENDER_RESOURCE_ATTRIBUTE_NAME - }) - .map_or_else( - || RenderResourceAttributes::default(), - |a| { - syn::custom_keyword!(from_self); - let mut attributes = RenderResourceAttributes::default(); - a.parse_args_with(|input: ParseStream| { - if let Some(_) = input.parse::>()? { - attributes.from_self = true; - } - Ok(()) - }) - .expect("invalid 'render_resources' attribute format"); + .find(|a| *a.path.get_ident().as_ref().unwrap() == RENDER_RESOURCE_ATTRIBUTE_NAME) + .map_or_else(RenderResourceAttributes::default, |a| { + syn::custom_keyword!(from_self); + let mut attributes = RenderResourceAttributes::default(); + a.parse_args_with(|input: ParseStream| { + if input.parse::>()?.is_some() { + attributes.from_self = true; + } + Ok(()) + }) + .expect("invalid 'render_resources' attribute format"); - attributes - }, - ); + attributes + }); let struct_name = &ast.ident; let struct_name_string = struct_name.to_string(); @@ -93,28 +88,24 @@ pub fn derive_render_resources(input: TokenStream) -> TokenStream { .attrs .iter() .find(|a| { - a.path.get_ident().as_ref().unwrap().to_string() - == RENDER_RESOURCE_ATTRIBUTE_NAME + *a.path.get_ident().as_ref().unwrap() == RENDER_RESOURCE_ATTRIBUTE_NAME }) - .map_or_else( - || RenderResourceFieldAttributes::default(), - |a| { - syn::custom_keyword!(ignore); - syn::custom_keyword!(buffer); - let mut attributes = RenderResourceFieldAttributes::default(); - a.parse_args_with(|input: ParseStream| { - if let Some(_) = input.parse::>()? { - attributes.ignore = true; - } else if let Some(_) = input.parse::>()? { - attributes.buffer = true; - } - Ok(()) - }) - .expect("invalid 'render_resources' attribute format"); + .map_or_else(RenderResourceFieldAttributes::default, |a| { + syn::custom_keyword!(ignore); + syn::custom_keyword!(buffer); + let mut attributes = RenderResourceFieldAttributes::default(); + a.parse_args_with(|input: ParseStream| { + if input.parse::>()?.is_some() { + attributes.ignore = true; + } else if input.parse::>()?.is_some() { + attributes.buffer = true; + } + Ok(()) + }) + .expect("invalid 'render_resources' attribute format"); - attributes - }, - ), + attributes + }), ) }) .collect::>(); diff --git a/crates/bevy_derive/src/shader_defs.rs b/crates/bevy_derive/src/shader_defs.rs index 6ce1b6def4..055b76cf80 100644 --- a/crates/bevy_derive/src/shader_defs.rs +++ b/crates/bevy_derive/src/shader_defs.rs @@ -5,7 +5,7 @@ use proc_macro2::Ident; use quote::quote; use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Fields, Path}; -static SHADER_DEF_ATTRIBUTE_NAME: &'static str = "shader_def"; +static SHADER_DEF_ATTRIBUTE_NAME: &str = "shader_def"; pub fn derive_shader_defs(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); @@ -25,10 +25,7 @@ pub fn derive_shader_defs(input: TokenStream) -> TokenStream { .filter(|f| { f.attrs .iter() - .find(|a| { - a.path.get_ident().as_ref().unwrap().to_string() == SHADER_DEF_ATTRIBUTE_NAME - }) - .is_some() + .any(|a| *a.path.get_ident().as_ref().unwrap() == SHADER_DEF_ATTRIBUTE_NAME) }) .map(|f| f.ident.as_ref().unwrap()) .collect::>(); diff --git a/crates/bevy_diagnostic/src/diagnostic.rs b/crates/bevy_diagnostic/src/diagnostic.rs index 9b3e1a540d..886f17f6be 100644 --- a/crates/bevy_diagnostic/src/diagnostic.rs +++ b/crates/bevy_diagnostic/src/diagnostic.rs @@ -71,7 +71,7 @@ impl Diagnostic { } pub fn average(&self) -> Option { - if self.history.len() > 0 { + if !self.history.is_empty() { Some(self.sum / self.history.len() as f64) } else { None @@ -93,7 +93,7 @@ impl Diagnostic { } } - return None; + None } pub fn get_max_history_length(&self) -> usize { diff --git a/crates/bevy_diagnostic/src/system_profiler.rs b/crates/bevy_diagnostic/src/system_profiler.rs index f10d72a5a1..ee47376df9 100644 --- a/crates/bevy_diagnostic/src/system_profiler.rs +++ b/crates/bevy_diagnostic/src/system_profiler.rs @@ -31,7 +31,7 @@ impl Profiler for SystemProfiler { let mut system_profiles = self.system_profiles.write().unwrap(); let profiles = system_profiles .entry(scope.clone()) - .or_insert_with(|| SystemProfiles::default()); + .or_insert_with(SystemProfiles::default); profiles.current_start = Some(Instant::now()); } diff --git a/crates/bevy_ecs/hecs/macros/src/lib.rs b/crates/bevy_ecs/hecs/macros/src/lib.rs index d430e01704..e21847d6e6 100644 --- a/crates/bevy_ecs/hecs/macros/src/lib.rs +++ b/crates/bevy_ecs/hecs/macros/src/lib.rs @@ -69,6 +69,7 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream { unsafe fn put(mut self, mut f: impl FnMut(*mut u8, std::any::TypeId, usize) -> bool) { #( if f((&mut self.#fields as *mut #tys).cast::(), std::any::TypeId::of::<#tys>(), std::mem::size_of::<#tys>()) { + #[allow(clippy::forget_copy)] std::mem::forget(self.#fields); } )* diff --git a/crates/bevy_ecs/hecs/src/archetype.rs b/crates/bevy_ecs/hecs/src/archetype.rs index 6874debf3e..6e94992db5 100644 --- a/crates/bevy_ecs/hecs/src/archetype.rs +++ b/crates/bevy_ecs/hecs/src/archetype.rs @@ -220,6 +220,12 @@ impl Archetype { self.len } + #[allow(missing_docs)] + #[inline] + pub fn is_empty(&self) -> bool { + self.len == 0 + } + #[allow(missing_docs)] pub fn iter_entities(&self) -> impl Iterator { self.entities.iter().take(self.len as usize) @@ -239,6 +245,7 @@ impl Archetype { &self.types } + /// # Safety /// `index` must be in-bounds pub(crate) unsafe fn get_dynamic( &self, @@ -255,6 +262,7 @@ impl Archetype { )) } + /// # Safety /// Every type must be written immediately after this call pub unsafe fn allocate(&mut self, id: u32) -> u32 { if self.len as usize == self.entities.len() { @@ -408,7 +416,13 @@ impl Archetype { } } - #[allow(missing_docs)] + /// # Safety + /// + /// - `component` must point to valid memory + /// - the component `ty`pe must be registered + /// - `index` must be in-bound + /// - `size` must be the size of the component + /// - the storage array must be big enough pub unsafe fn put_dynamic( &mut self, component: *mut u8, diff --git a/crates/bevy_ecs/hecs/src/borrow.rs b/crates/bevy_ecs/hecs/src/borrow.rs index 41b1cc4966..fdd1df86ab 100644 --- a/crates/bevy_ecs/hecs/src/borrow.rs +++ b/crates/bevy_ecs/hecs/src/borrow.rs @@ -72,7 +72,11 @@ pub struct Ref<'a, T: Component> { } impl<'a, T: Component> Ref<'a, T> { - #[allow(missing_docs)] + /// Creates a new entity component borrow + /// + /// # Safety + /// + /// - the index of the component must be valid pub unsafe fn new(archetype: &'a Archetype, index: u32) -> Result { let target = NonNull::new_unchecked( archetype @@ -120,7 +124,11 @@ pub struct RefMut<'a, T: Component> { } impl<'a, T: Component> RefMut<'a, T> { - #[allow(missing_docs)] + /// Creates a new entity component mutable borrow + /// + /// # Safety + /// + /// - the index of the component must be valid pub unsafe fn new(archetype: &'a Archetype, index: u32) -> Result { let target = NonNull::new_unchecked( archetype diff --git a/crates/bevy_ecs/hecs/src/entities.rs b/crates/bevy_ecs/hecs/src/entities.rs index 5d5b0ea34e..513c507261 100644 --- a/crates/bevy_ecs/hecs/src/entities.rs +++ b/crates/bevy_ecs/hecs/src/entities.rs @@ -11,6 +11,7 @@ use std::error::Error; #[derive(Debug, Clone, Copy, Hash, Eq, Ord, PartialEq, PartialOrd)] pub struct Entity(u32); +#[allow(clippy::new_without_default)] impl Entity { #[allow(missing_docs)] pub fn new() -> Self { diff --git a/crates/bevy_ecs/hecs/src/query.rs b/crates/bevy_ecs/hecs/src/query.rs index 5c11297af3..9374c9eb56 100644 --- a/crates/bevy_ecs/hecs/src/query.rs +++ b/crates/bevy_ecs/hecs/src/query.rs @@ -47,6 +47,9 @@ pub trait Fetch<'a>: Sized { fn release(archetype: &Archetype); /// if this returns true, the current item will be skipped during iteration + /// + /// # Safety + /// shouldn't be called if there is no current item unsafe fn should_skip(&self) -> bool { false } @@ -793,7 +796,7 @@ struct ChunkIter { } impl ChunkIter { - unsafe fn next<'a, 'w>(&mut self) -> Option<>::Item> { + unsafe fn next<'a>(&mut self) -> Option<>::Item> { loop { if self.len == 0 { return None; diff --git a/crates/bevy_ecs/hecs/src/world.rs b/crates/bevy_ecs/hecs/src/world.rs index 748f1e9d52..7dc63c52cc 100644 --- a/crates/bevy_ecs/hecs/src/world.rs +++ b/crates/bevy_ecs/hecs/src/world.rs @@ -165,7 +165,7 @@ impl World { let removed_entities = self .removed_components .entry(ty.id()) - .or_insert_with(|| Vec::new()); + .or_insert_with(Vec::new); removed_entities.push(entity); } Ok(()) @@ -202,7 +202,7 @@ impl World { let removed_entities = self .removed_components .entry(ty.id()) - .or_insert_with(|| Vec::new()); + .or_insert_with(Vec::new); removed_entities.extend(archetype.iter_entities().map(|id| Entity::from_id(*id))); } archetype.clear(); @@ -505,7 +505,7 @@ impl World { state.mutated_entities[target_index as usize] = is_mutated; } else { let removed_entities = - removed_components.entry(ty).or_insert_with(|| Vec::new()); + removed_components.entry(ty).or_insert_with(Vec::new); removed_entities.push(entity); } }) diff --git a/crates/bevy_ecs/src/resource/resource_query.rs b/crates/bevy_ecs/src/resource/resource_query.rs index bae6498ce8..43c2ae14d5 100644 --- a/crates/bevy_ecs/src/resource/resource_query.rs +++ b/crates/bevy_ecs/src/resource/resource_query.rs @@ -17,6 +17,10 @@ pub struct Res<'a, T: Resource> { } impl<'a, T: Resource> Res<'a, T> { + /// Creates a reference cell to a Resource from a pointer + /// + /// # Safety + /// The pointer must have correct lifetime / storage pub unsafe fn new(value: NonNull) -> Self { Self { value: &*value.as_ptr(), @@ -26,6 +30,7 @@ impl<'a, T: Resource> Res<'a, T> { /// A clone that is unsafe to perform. You probably shouldn't use this. pub trait UnsafeClone { + #[allow(clippy::missing_safety_doc)] unsafe fn unsafe_clone(&self) -> Self; } @@ -53,6 +58,10 @@ pub struct ResMut<'a, T: Resource> { } impl<'a, T: Resource> ResMut<'a, T> { + /// Creates a mutable reference cell to a Resource from a pointer + /// + /// # Safety + /// The pointer must have correct lifetime / storage / ownership pub unsafe fn new(value: NonNull) -> Self { Self { value: value.as_ptr(), @@ -133,6 +142,7 @@ pub trait FetchResource<'a>: Sized { fn borrow(resources: &Resources); fn release(resources: &Resources); + #[allow(clippy::missing_safety_doc)] unsafe fn get(resources: &'a Resources, system_id: Option) -> Self::Item; } diff --git a/crates/bevy_ecs/src/resource/resources.rs b/crates/bevy_ecs/src/resource/resources.rs index f0561ef997..f92a716c60 100644 --- a/crates/bevy_ecs/src/resource/resources.rs +++ b/crates/bevy_ecs/src/resource/resources.rs @@ -42,10 +42,12 @@ impl Resources { self.get_resource_mut(ResourceIndex::Global) } + #[allow(clippy::needless_lifetimes)] pub fn get_local<'a, T: Resource>(&'a self, id: SystemId) -> Option> { self.get_resource(ResourceIndex::System(id)) } + #[allow(clippy::needless_lifetimes)] pub fn get_local_mut<'a, T: Resource>(&'a self, id: SystemId) -> Option> { self.get_resource_mut(ResourceIndex::System(id)) } @@ -82,10 +84,13 @@ impl Resources { }), }; - if index == archetype.len() { - unsafe { archetype.allocate(index) }; - } else if index > archetype.len() { - panic!("attempted to access index beyond 'current_capacity + 1'") + use std::cmp::Ordering; + match index.cmp(&archetype.len()) { + Ordering::Equal => { + unsafe { archetype.allocate(index) }; + } + Ordering::Greater => panic!("attempted to access index beyond 'current_capacity + 1'"), + Ordering::Less => (), } unsafe { @@ -140,6 +145,7 @@ impl Resources { } #[inline] + #[allow(clippy::missing_safety_doc)] pub unsafe fn get_unsafe_ref(&self, resource_index: ResourceIndex) -> NonNull { self.resource_data .get(&TypeId::of::()) diff --git a/crates/bevy_ecs/src/schedule/mod.rs b/crates/bevy_ecs/src/schedule/mod.rs index 8e617a17a5..be45138cb8 100644 --- a/crates/bevy_ecs/src/schedule/mod.rs +++ b/crates/bevy_ecs/src/schedule/mod.rs @@ -1,4 +1,5 @@ mod parallel_executor; +#[allow(clippy::module_inception)] mod schedule; pub use parallel_executor::*; diff --git a/crates/bevy_ecs/src/schedule/parallel_executor.rs b/crates/bevy_ecs/src/schedule/parallel_executor.rs index db51e29333..0366f08751 100644 --- a/crates/bevy_ecs/src/schedule/parallel_executor.rs +++ b/crates/bevy_ecs/src/schedule/parallel_executor.rs @@ -51,7 +51,7 @@ impl ParallelExecutor { if schedule_changed { self.stages.clear(); self.stages - .resize_with(schedule.stage_order.len(), || ExecutorStage::default()); + .resize_with(schedule.stage_order.len(), ExecutorStage::default); } for (stage_name, executor_stage) in schedule.stage_order.iter().zip(self.stages.iter_mut()) { @@ -208,9 +208,10 @@ impl ExecutorStage { ThreadLocalExecution::NextFlush => { let resource_access = system.resource_access(); // if any system before this one conflicts, check all systems that came before for compatibility - if current_archetype_access.is_compatible(archetype_access) == false - || current_resource_access.is_compatible(resource_access) == false + if !current_archetype_access.is_compatible(archetype_access) + || !current_resource_access.is_compatible(resource_access) { + #[allow(clippy::needless_range_loop)] for earlier_system_index in prepare_system_index_range.start..system_index { @@ -223,14 +224,12 @@ impl ExecutorStage { ); // if earlier system is incompatible, make the current system dependent - if earlier_system + if !earlier_system .archetype_access() .is_compatible(archetype_access) - == false - || earlier_system + || !earlier_system .resource_access() .is_compatible(resource_access) - == false { self.system_dependents[earlier_system_index].push(system_index); self.system_dependencies[system_index] diff --git a/crates/bevy_ecs/src/schedule/schedule.rs b/crates/bevy_ecs/src/schedule/schedule.rs index 77a72237dc..a50694f643 100644 --- a/crates/bevy_ecs/src/schedule/schedule.rs +++ b/crates/bevy_ecs/src/schedule/schedule.rs @@ -25,7 +25,7 @@ pub struct Schedule { impl Schedule { pub fn add_stage(&mut self, stage: impl Into>) { let stage: Cow = stage.into(); - if let Some(_) = self.stages.get(&stage) { + if self.stages.get(&stage).is_some() { panic!("Stage already exists: {}", stage); } else { self.stages.insert(stage.clone(), Vec::new()); @@ -40,7 +40,7 @@ impl Schedule { ) { let target: Cow = target.into(); let stage: Cow = stage.into(); - if let Some(_) = self.stages.get(&stage) { + if self.stages.get(&stage).is_some() { panic!("Stage already exists: {}", stage); } @@ -63,7 +63,7 @@ impl Schedule { ) { let target: Cow = target.into(); let stage: Cow = stage.into(); - if let Some(_) = self.stages.get(&stage) { + if self.stages.get(&stage).is_some() { panic!("Stage already exists: {}", stage); } @@ -173,7 +173,7 @@ impl Schedule { let thread_pool_builder = resources .get::() .map(|options| (*options).clone()) - .unwrap_or_else(|| ParallelExecutorOptions::default()) + .unwrap_or_else(ParallelExecutorOptions::default) .create_builder(); // For now, bevy_ecs only uses the global thread pool so it is sufficient to configure it once here. // Dont call .unwrap() as the function is called twice.. diff --git a/crates/bevy_ecs/src/system/mod.rs b/crates/bevy_ecs/src/system/mod.rs index e9441684f0..ef5e9f2043 100644 --- a/crates/bevy_ecs/src/system/mod.rs +++ b/crates/bevy_ecs/src/system/mod.rs @@ -3,6 +3,7 @@ mod into_system; #[cfg(feature = "profiler")] mod profiler; mod query; +#[allow(clippy::module_inception)] mod system; pub use commands::*; diff --git a/crates/bevy_ecs/src/system/query.rs b/crates/bevy_ecs/src/system/query.rs index 95e4e06c35..0d27c4530b 100644 --- a/crates/bevy_ecs/src/system/query.rs +++ b/crates/bevy_ecs/src/system/query.rs @@ -49,9 +49,7 @@ impl<'a, Q: HecsQuery> Query<'a, Q> { .mutable .contains(location.archetype as usize) { - self.world - .get(entity) - .map_err(|err| QueryError::ComponentError(err)) + self.world.get(entity).map_err(QueryError::ComponentError) } else { Err(QueryError::CannotReadArchetype) } @@ -83,20 +81,21 @@ impl<'a, Q: HecsQuery> Query<'a, Q> { /// Gets a mutable reference to the entity's component of the given type. This will fail if the entity does not have /// the given component type or if the given component type does not match this query. pub fn get_mut(&self, entity: Entity) -> Result, QueryError> { - if let Some(location) = self.world.get_entity_location(entity) { - if self - .archetype_access - .mutable - .contains(location.archetype as usize) - { - self.world - .get_mut(entity) - .map_err(|err| QueryError::ComponentError(err)) - } else { - Err(QueryError::CannotWriteArchetype) - } + let location = match self.world.get_entity_location(entity) { + None => return Err(QueryError::ComponentError(ComponentError::NoSuchEntity)), + Some(location) => location, + }; + + if self + .archetype_access + .mutable + .contains(location.archetype as usize) + { + self.world + .get_mut(entity) + .map_err(QueryError::ComponentError) } else { - Err(QueryError::ComponentError(ComponentError::NoSuchEntity)) + Err(QueryError::CannotWriteArchetype) } } @@ -241,7 +240,7 @@ struct ChunkIter { impl ChunkIter { #[inline] - unsafe fn next<'a, 'w>(&mut self) -> Option<>::Item> { + unsafe fn next<'a>(&mut self) -> Option<>::Item> { loop { if self.len == 0 { return None; diff --git a/crates/bevy_ecs/src/system/system.rs b/crates/bevy_ecs/src/system/system.rs index 7eee2f257a..d5dac74c8f 100644 --- a/crates/bevy_ecs/src/system/system.rs +++ b/crates/bevy_ecs/src/system/system.rs @@ -14,6 +14,7 @@ pub enum ThreadLocalExecution { pub struct SystemId(pub u32); impl SystemId { + #[allow(clippy::new_without_default)] pub fn new() -> Self { SystemId(rand::random::()) } diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index f2827b5b79..eacf4559a5 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -51,7 +51,7 @@ fn get_primitive_topology(mode: Mode) -> Result { Mode::LineStrip => Ok(PrimitiveTopology::LineStrip), Mode::Triangles => Ok(PrimitiveTopology::TriangleList), Mode::TriangleStrip => Ok(PrimitiveTopology::TriangleStrip), - mode @ _ => Err(GltfError::UnsupportedPrimitive { mode }), + mode => Err(GltfError::UnsupportedPrimitive { mode }), } } @@ -60,7 +60,7 @@ pub fn load_gltf(asset_path: &Path, bytes: Vec) -> Result { let gltf = gltf::Gltf::from_slice(&bytes)?; let buffer_data = load_buffers(gltf.buffers(), asset_path)?; for scene in gltf.scenes() { - for node in scene.nodes() { + if let Some(node) = scene.nodes().next() { return Ok(load_node(&buffer_data, &node, 1)?); } } @@ -71,43 +71,41 @@ pub fn load_gltf(asset_path: &Path, bytes: Vec) -> Result { fn load_node(buffer_data: &[Vec], node: &gltf::Node, depth: i32) -> Result { if let Some(mesh) = node.mesh() { - for primitive in mesh.primitives() { + if let Some(primitive) = mesh.primitives().next() { let reader = primitive.reader(|buffer| Some(&buffer_data[buffer.index()])); let primitive_topology = get_primitive_topology(primitive.mode())?; let mut mesh = Mesh::new(primitive_topology); - reader - .read_positions() - .map(|v| VertexAttribute { - name: "Vertex_Position".into(), - values: VertexAttributeValues::Float3(v.collect()), - }) - .map(|vertex_attribute| mesh.attributes.push(vertex_attribute)); - reader - .read_normals() - .map(|v| VertexAttribute { - name: "Vertex_Normal".into(), - values: VertexAttributeValues::Float3(v.collect()), - }) - .map(|vertex_attribute| mesh.attributes.push(vertex_attribute)); + if let Some(vertex_attribute) = reader.read_positions().map(|v| VertexAttribute { + name: "Vertex_Position".into(), + values: VertexAttributeValues::Float3(v.collect()), + }) { + mesh.attributes.push(vertex_attribute); + } - reader - .read_tex_coords(0) - .map(|v| VertexAttribute { - name: "Vertex_Uv".into(), - values: VertexAttributeValues::Float2(v.into_f32().collect()), - }) - .map(|vertex_attribute| mesh.attributes.push(vertex_attribute)); + if let Some(vertex_attribute) = reader.read_normals().map(|v| VertexAttribute { + name: "Vertex_Normal".into(), + values: VertexAttributeValues::Float3(v.collect()), + }) { + mesh.attributes.push(vertex_attribute); + } - reader.read_indices().map(|indices| { + if let Some(vertex_attribute) = reader.read_tex_coords(0).map(|v| VertexAttribute { + name: "Vertex_Uv".into(), + values: VertexAttributeValues::Float2(v.into_f32().collect()), + }) { + mesh.attributes.push(vertex_attribute); + } + + if let Some(indices) = reader.read_indices() { mesh.indices = Some(indices.into_u32().collect::>()); - }); + }; return Ok(mesh); } } - for child in node.children() { + if let Some(child) = node.children().next() { return Ok(load_node(buffer_data, &child, depth + 1)?); } diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index 0763c5d3b8..bfd3069e34 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -19,10 +19,7 @@ pub enum ElementState { impl ElementState { pub fn is_pressed(&self) -> bool { - match self { - ElementState::Pressed => true, - _ => false, - } + matches!(self, ElementState::Pressed) } } diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index 378d4dcbd9..c5329b9450 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -3,6 +3,7 @@ use bevy_render::{color::Color, renderer::RenderResources, shader::ShaderDefs, t /// A material with "standard" properties used in PBR lighting #[derive(RenderResources, ShaderDefs)] +#[allow(clippy::manual_non_exhaustive)] pub struct StandardMaterial { pub albedo: Color, #[shader_def] @@ -10,6 +11,12 @@ pub struct StandardMaterial { #[render_resources(ignore)] #[shader_def] pub shaded: bool, + + // this is a manual implementation of the non exhaustive pattern, + // especially made to allow ..Default::default() + #[render_resources(ignore)] + #[doc(hidden)] + pub __non_exhaustive: (), } impl Default for StandardMaterial { @@ -18,6 +25,7 @@ impl Default for StandardMaterial { albedo: Color::rgb(1.0, 1.0, 1.0), albedo_texture: None, shaded: true, + __non_exhaustive: (), } } } diff --git a/crates/bevy_property/bevy_property_derive/src/lib.rs b/crates/bevy_property/bevy_property_derive/src/lib.rs index aa2691e973..bbdd211410 100644 --- a/crates/bevy_property/bevy_property_derive/src/lib.rs +++ b/crates/bevy_property/bevy_property_derive/src/lib.rs @@ -48,14 +48,12 @@ pub fn derive_properties(input: TokenStream) -> TokenStream { f, f.attrs .iter() - .find(|a| { - a.path.get_ident().as_ref().unwrap().to_string() == PROP_ATTRIBUTE_NAME - }) + .find(|a| *a.path.get_ident().as_ref().unwrap() == PROP_ATTRIBUTE_NAME) .map(|a| { syn::custom_keyword!(ignore); let mut attribute_args = PropAttributeArgs { ignore: None }; a.parse_args_with(|input: ParseStream| { - if let Some(_) = input.parse::>()? { + if input.parse::>()?.is_some() { attribute_args.ignore = Some(true); return Ok(()); } diff --git a/crates/bevy_property/src/dynamic_properties.rs b/crates/bevy_property/src/dynamic_properties.rs index 52fdd89164..5c343bd339 100644 --- a/crates/bevy_property/src/dynamic_properties.rs +++ b/crates/bevy_property/src/dynamic_properties.rs @@ -158,12 +158,16 @@ impl Property for DynamicProperties { PropertyType::Map => { for (i, prop) in properties.iter_props().enumerate() { let name = properties.prop_name(i).unwrap(); - self.prop_mut(name).map(|p| p.apply(prop)); + if let Some(p) = self.prop_mut(name) { + p.apply(prop); + } } } PropertyType::Seq => { for (i, prop) in properties.iter_props().enumerate() { - self.prop_with_index_mut(i).map(|p| p.apply(prop)); + if let Some(p) = self.prop_with_index_mut(i) { + p.apply(prop); + } } } _ => panic!("DynamicProperties cannot be Value types"), diff --git a/crates/bevy_property/src/impl_property/impl_property_std.rs b/crates/bevy_property/src/impl_property/impl_property_std.rs index fe3dfc40dd..7c5f9f32e9 100644 --- a/crates/bevy_property/src/impl_property/impl_property_std.rs +++ b/crates/bevy_property/src/impl_property/impl_property_std.rs @@ -67,7 +67,7 @@ where fn set(&mut self, value: &dyn Property) { if let Some(properties) = value.as_properties() { let len = properties.prop_len(); - self.resize_with(len, || T::default()); + self.resize_with(len, T::default); if properties.property_type() != self.property_type() { panic!( @@ -77,7 +77,9 @@ where ); } for (i, prop) in properties.iter_props().enumerate() { - self.prop_with_index_mut(i).map(|p| p.apply(prop)); + if let Some(p) = self.prop_with_index_mut(i) { + p.apply(prop) + } } } else { panic!("attempted to apply non-Properties type to Properties type"); @@ -171,7 +173,7 @@ impl Property for bool { #[inline] fn clone_prop(&self) -> Box { - Box::new(self.clone()) + Box::new(*self) } #[inline] @@ -211,7 +213,7 @@ impl Property for usize { #[inline] fn clone_prop(&self) -> Box { - Box::new(self.clone()) + Box::new(*self) } #[inline] @@ -269,7 +271,7 @@ impl Property for u64 { #[inline] fn clone_prop(&self) -> Box { - Box::new(self.clone()) + Box::new(*self) } #[inline] @@ -327,7 +329,7 @@ impl Property for u32 { #[inline] fn clone_prop(&self) -> Box { - Box::new(self.clone()) + Box::new(*self) } #[inline] @@ -385,7 +387,7 @@ impl Property for u16 { #[inline] fn clone_prop(&self) -> Box { - Box::new(self.clone()) + Box::new(*self) } #[inline] @@ -443,7 +445,7 @@ impl Property for u8 { #[inline] fn clone_prop(&self) -> Box { - Box::new(self.clone()) + Box::new(*self) } #[inline] @@ -501,7 +503,7 @@ impl Property for isize { #[inline] fn clone_prop(&self) -> Box { - Box::new(self.clone()) + Box::new(*self) } #[inline] @@ -559,7 +561,7 @@ impl Property for i64 { #[inline] fn clone_prop(&self) -> Box { - Box::new(self.clone()) + Box::new(*self) } #[inline] @@ -617,7 +619,7 @@ impl Property for i32 { #[inline] fn clone_prop(&self) -> Box { - Box::new(self.clone()) + Box::new(*self) } #[inline] @@ -675,7 +677,7 @@ impl Property for i16 { #[inline] fn clone_prop(&self) -> Box { - Box::new(self.clone()) + Box::new(*self) } #[inline] @@ -733,7 +735,7 @@ impl Property for i8 { #[inline] fn clone_prop(&self) -> Box { - Box::new(self.clone()) + Box::new(*self) } #[inline] @@ -791,7 +793,7 @@ impl Property for f32 { #[inline] fn clone_prop(&self) -> Box { - Box::new(self.clone()) + Box::new(*self) } #[inline] @@ -833,7 +835,7 @@ impl Property for f64 { #[inline] fn clone_prop(&self) -> Box { - Box::new(self.clone()) + Box::new(*self) } #[inline] diff --git a/crates/bevy_property/src/property_serde.rs b/crates/bevy_property/src/property_serde.rs index c78340d301..f66bf32bbe 100644 --- a/crates/bevy_property/src/property_serde.rs +++ b/crates/bevy_property/src/property_serde.rs @@ -17,6 +17,7 @@ pub enum Serializable<'a> { } impl<'a> Serializable<'a> { + #[allow(clippy::should_implement_trait)] pub fn borrow(&self) -> &dyn erased_serde::Serialize { match self { Serializable::Borrowed(serialize) => serialize, @@ -88,11 +89,9 @@ impl<'a> Serialize for DynamicPropertiesSerializer<'a> { PropertyType::Seq => { SeqSerializer::new(self.dynamic_properties, self.registry).serialize(serializer) } - _ => { - return Err(serde::ser::Error::custom( - "DynamicProperties cannot be Value type", - )) - } + _ => Err(serde::ser::Error::custom( + "DynamicProperties cannot be Value type", + )), } } } @@ -511,7 +510,7 @@ where .ok_or_else(|| de::Error::missing_field(TYPE_FIELD))?; let mut dynamic_properties = map.next_value_seed(MapPropertyDeserializer { registry })?; - dynamic_properties.type_name = type_name.to_string(); + dynamic_properties.type_name = type_name; return Ok(DynamicPropertiesOrProperty::DynamicProperties( dynamic_properties, )); diff --git a/crates/bevy_property/src/type_registry.rs b/crates/bevy_property/src/type_registry.rs index 9b76d4aece..9a8341d94a 100644 --- a/crates/bevy_property/src/type_registry.rs +++ b/crates/bevy_property/src/type_registry.rs @@ -91,7 +91,7 @@ impl PropertyTypeRegistration { } pub fn get_short_name(full_name: &str) -> String { - let mut split = full_name.splitn(2, "<"); + let mut split = full_name.splitn(2, '<'); // main type let mut short_name = split @@ -104,7 +104,7 @@ impl PropertyTypeRegistration { // process generics if they exist if let Some(generics) = split.next() { - if !generics.ends_with(">") { + if !generics.ends_with('>') { panic!("should end with closing carrot") } diff --git a/crates/bevy_render/src/batch/batcher.rs b/crates/bevy_render/src/batch/batcher.rs index 72a0b5a7a0..266528842d 100644 --- a/crates/bevy_render/src/batch/batcher.rs +++ b/crates/bevy_render/src/batch/batcher.rs @@ -123,7 +123,7 @@ where .find(|(_i, is_index)| is_index(&key)) .map(|(i, _)| i); if let Some(key_index) = key_index { - key_state.set(key_index, key.clone()); + key_state.set(key_index, key); key_state.finish() } else { return false; @@ -139,7 +139,7 @@ where batch.add(value); } - return true; + true } pub fn iter(&self) -> impl Iterator> { diff --git a/crates/bevy_render/src/batch/mod.rs b/crates/bevy_render/src/batch/mod.rs index 766b24a360..2e1d1740ee 100644 --- a/crates/bevy_render/src/batch/mod.rs +++ b/crates/bevy_render/src/batch/mod.rs @@ -1,5 +1,6 @@ // mod asset_batcher; // mod asset_batcher2; +#[allow(clippy::module_inception)] mod batch; mod batcher; diff --git a/crates/bevy_render/src/camera/active_cameras.rs b/crates/bevy_render/src/camera/active_cameras.rs index 60115a7eb9..a3e39b848a 100644 --- a/crates/bevy_render/src/camera/active_cameras.rs +++ b/crates/bevy_render/src/camera/active_cameras.rs @@ -17,7 +17,7 @@ impl ActiveCameras { } pub fn get(&self, name: &str) -> Option { - self.cameras.get(name).and_then(|e| e.clone()) + self.cameras.get(name).and_then(|e| *e) } } @@ -26,7 +26,7 @@ pub fn active_cameras_system( mut query: Query<(Entity, &Camera)>, ) { for (name, active_camera) in active_cameras.cameras.iter_mut() { - if let None = active_camera { + if active_camera.is_none() { for (camera_entity, camera) in &mut query.iter() { if let Some(ref current_name) = camera.name { if current_name == name { diff --git a/crates/bevy_render/src/camera/mod.rs b/crates/bevy_render/src/camera/mod.rs index b9837db8ee..28bdfe5ec1 100644 --- a/crates/bevy_render/src/camera/mod.rs +++ b/crates/bevy_render/src/camera/mod.rs @@ -1,4 +1,5 @@ mod active_cameras; +#[allow(clippy::module_inception)] mod camera; mod projection; mod visible_entities; diff --git a/crates/bevy_render/src/draw.rs b/crates/bevy_render/src/draw.rs index e2e7227764..07431c3c8a 100644 --- a/crates/bevy_render/src/draw.rs +++ b/crates/bevy_render/src/draw.rs @@ -138,7 +138,7 @@ impl<'a> UnsafeClone for DrawContext<'a> { render_resource_context: self.render_resource_context.unsafe_clone(), vertex_buffer_descriptors: self.vertex_buffer_descriptors.unsafe_clone(), shared_buffers: self.shared_buffers.unsafe_clone(), - current_pipeline: self.current_pipeline.clone(), + current_pipeline: self.current_pipeline, } } } diff --git a/crates/bevy_render/src/mesh/mesh.rs b/crates/bevy_render/src/mesh/mesh.rs index 439468b367..585b0e9e01 100644 --- a/crates/bevy_render/src/mesh/mesh.rs +++ b/crates/bevy_render/src/mesh/mesh.rs @@ -34,6 +34,10 @@ impl VertexAttributeValues { } } + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + // TODO: add vertex format as parameter here and perform type conversions pub fn get_bytes(&self) -> &[u8] { match self { @@ -224,9 +228,9 @@ pub mod shape { let mut normals = Vec::new(); let mut uvs = Vec::new(); for (position, normal, uv) in vertices.iter() { - positions.push(position.clone()); - normals.push(normal.clone()); - uvs.push(uv.clone()); + positions.push(*position); + normals.push(*normal); + uvs.push(*uv); } let indices = vec![ @@ -331,9 +335,9 @@ pub mod shape { let mut normals = Vec::new(); let mut uvs = Vec::new(); for (position, normal, uv) in vertices.iter() { - positions.push(position.clone()); - normals.push(normal.clone()); - uvs.push(uv.clone()); + positions.push(*position); + normals.push(*normal); + uvs.push(*uv); } Mesh { @@ -371,9 +375,9 @@ pub mod shape { let mut normals = Vec::new(); let mut uvs = Vec::new(); for (position, normal, uv) in vertices.iter() { - positions.push(position.clone()); - normals.push(normal.clone()); - uvs.push(uv.clone()); + positions.push(*position); + normals.push(*normal); + uvs.push(*uv); } Mesh { diff --git a/crates/bevy_render/src/mesh/mod.rs b/crates/bevy_render/src/mesh/mod.rs index 94e5170594..f9d33b4891 100644 --- a/crates/bevy_render/src/mesh/mod.rs +++ b/crates/bevy_render/src/mesh/mod.rs @@ -1,3 +1,4 @@ +#[allow(clippy::module_inception)] mod mesh; mod vertex; diff --git a/crates/bevy_render/src/pass/mod.rs b/crates/bevy_render/src/pass/mod.rs index bb5d012fd5..26342576ec 100644 --- a/crates/bevy_render/src/pass/mod.rs +++ b/crates/bevy_render/src/pass/mod.rs @@ -1,4 +1,5 @@ mod ops; +#[allow(clippy::module_inception)] mod pass; mod render_pass; diff --git a/crates/bevy_render/src/pipeline/mod.rs b/crates/bevy_render/src/pipeline/mod.rs index 74899a36de..d25bbfea64 100644 --- a/crates/bevy_render/src/pipeline/mod.rs +++ b/crates/bevy_render/src/pipeline/mod.rs @@ -1,5 +1,6 @@ mod bind_group; mod binding; +#[allow(clippy::module_inception)] mod pipeline; mod pipeline_compiler; mod pipeline_layout; diff --git a/crates/bevy_render/src/pipeline/pipeline_compiler.rs b/crates/bevy_render/src/pipeline/pipeline_compiler.rs index 6f636d6329..e792c76f36 100644 --- a/crates/bevy_render/src/pipeline/pipeline_compiler.rs +++ b/crates/bevy_render/src/pipeline/pipeline_compiler.rs @@ -29,8 +29,7 @@ impl Default for PipelineSpecialization { impl PipelineSpecialization { pub fn empty() -> &'static PipelineSpecialization { - pub static EMPTY: Lazy = - Lazy::new(|| PipelineSpecialization::default()); + pub static EMPTY: Lazy = Lazy::new(PipelineSpecialization::default); &EMPTY } } @@ -72,7 +71,7 @@ impl PipelineCompiler { let specialized_shaders = self .specialized_shaders .entry(*shader_handle) - .or_insert_with(|| Vec::new()); + .or_insert_with(Vec::new); let shader = shaders.get(shader_handle).unwrap(); @@ -172,7 +171,7 @@ impl PipelineCompiler { let specialized_pipelines = self .specialized_pipelines .entry(source_pipeline) - .or_insert_with(|| Vec::new()); + .or_insert_with(Vec::new); specialized_pipelines.push(SpecializedPipeline { pipeline: specialized_pipeline_handle, specialization: pipeline_specialization.clone(), diff --git a/crates/bevy_render/src/pipeline/pipeline_layout.rs b/crates/bevy_render/src/pipeline/pipeline_layout.rs index d372787fe9..80528a0e62 100644 --- a/crates/bevy_render/src/pipeline/pipeline_layout.rs +++ b/crates/bevy_render/src/pipeline/pipeline_layout.rs @@ -113,10 +113,7 @@ impl UniformProperty { UniformProperty::Vec4 => 4 * 4, UniformProperty::Mat3 => 4 * 4 * 3, UniformProperty::Mat4 => 4 * 4 * 4, - UniformProperty::Struct(properties) => properties - .iter() - .map(|p| p.get_size()) - .fold(0, |total, size| total + size), + UniformProperty::Struct(properties) => properties.iter().map(|p| p.get_size()).sum(), UniformProperty::Array(property, length) => property.get_size() * *length as u64, } } diff --git a/crates/bevy_render/src/pipeline/render_pipelines.rs b/crates/bevy_render/src/pipeline/render_pipelines.rs index 9f26bd6fbd..b3649cd9b6 100644 --- a/crates/bevy_render/src/pipeline/render_pipelines.rs +++ b/crates/bevy_render/src/pipeline/render_pipelines.rs @@ -7,7 +7,9 @@ use crate::{ use bevy_asset::Handle; use bevy_ecs::{Query, Res, ResMut}; use bevy_property::Properties; + #[derive(Properties, Default, Clone)] +#[non_exhaustive] pub struct RenderPipeline { pub pipeline: Handle, pub specialization: PipelineSpecialization, @@ -28,7 +30,6 @@ impl RenderPipeline { RenderPipeline { pipeline, specialization, - ..Default::default() } } } diff --git a/crates/bevy_render/src/render_graph/command.rs b/crates/bevy_render/src/render_graph/command.rs index b0621707f9..ab1f7725d2 100644 --- a/crates/bevy_render/src/render_graph/command.rs +++ b/crates/bevy_render/src/render_graph/command.rs @@ -54,6 +54,7 @@ impl CommandQueue { }); } + #[allow(clippy::too_many_arguments)] pub fn copy_buffer_to_texture( &mut self, source_buffer: BufferId, diff --git a/crates/bevy_render/src/render_graph/graph.rs b/crates/bevy_render/src/render_graph/graph.rs index 2dfc07b7d8..f33992c880 100644 --- a/crates/bevy_render/src/render_graph/graph.rs +++ b/crates/bevy_render/src/render_graph/graph.rs @@ -276,7 +276,7 @@ impl RenderGraph { } pub fn take_commands(&mut self) -> Commands { - std::mem::replace(&mut self.commands, Commands::default()) + std::mem::take(&mut self.commands) } } diff --git a/crates/bevy_render/src/render_graph/node.rs b/crates/bevy_render/src/render_graph/node.rs index 98e4be422c..b7abe4eb91 100644 --- a/crates/bevy_render/src/render_graph/node.rs +++ b/crates/bevy_render/src/render_graph/node.rs @@ -9,6 +9,7 @@ use uuid::Uuid; pub struct NodeId(Uuid); impl NodeId { + #[allow(clippy::new_without_default)] pub fn new() -> Self { NodeId(Uuid::new_v4()) } @@ -48,7 +49,7 @@ pub struct Edges { impl Edges { pub(crate) fn add_input_edge(&mut self, edge: Edge) -> Result<(), RenderGraphError> { if self.has_input_edge(&edge) { - return Err(RenderGraphError::EdgeAlreadyExists(edge.clone())); + return Err(RenderGraphError::EdgeAlreadyExists(edge)); } self.input_edges.push(edge); Ok(()) @@ -56,7 +57,7 @@ impl Edges { pub(crate) fn add_output_edge(&mut self, edge: Edge) -> Result<(), RenderGraphError> { if self.has_output_edge(&edge) { - return Err(RenderGraphError::EdgeAlreadyExists(edge.clone())); + return Err(RenderGraphError::EdgeAlreadyExists(edge)); } self.output_edges.push(edge); Ok(()) diff --git a/crates/bevy_render/src/render_graph/node_slot.rs b/crates/bevy_render/src/render_graph/node_slot.rs index b0629c50f5..9ad6ca3d61 100644 --- a/crates/bevy_render/src/render_graph/node_slot.rs +++ b/crates/bevy_render/src/render_graph/node_slot.rs @@ -98,6 +98,10 @@ impl ResourceSlots { pub fn len(&self) -> usize { self.slots.len() } + + pub fn is_empty(&self) -> bool { + self.slots.is_empty() + } } impl From<&ResourceSlotInfo> for ResourceSlot { diff --git a/crates/bevy_render/src/render_graph/nodes/texture_copy_node.rs b/crates/bevy_render/src/render_graph/nodes/texture_copy_node.rs index 5e59b37e07..6b642d39c7 100644 --- a/crates/bevy_render/src/render_graph/nodes/texture_copy_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/texture_copy_node.rs @@ -67,7 +67,7 @@ impl Node for TextureCopyNode { texture_resource.get_texture().unwrap(), [0, 0, 0], 0, - texture_descriptor.size.clone(), + texture_descriptor.size, ); render_context.resources().remove_buffer(texture_buffer); } diff --git a/crates/bevy_render/src/render_graph/schedule.rs b/crates/bevy_render/src/render_graph/schedule.rs index f5bb4cdbed..363ac5b0b9 100644 --- a/crates/bevy_render/src/render_graph/schedule.rs +++ b/crates/bevy_render/src/render_graph/schedule.rs @@ -75,7 +75,7 @@ impl Stages { node_borrows.push((node, indices)); } - node_borrows.sort_by_key(|(_node, indices)| indices.clone()); + node_borrows.sort_by_key(|(_node, indices)| <&NodeIndices>::clone(indices)); let mut last_stage = usize::MAX; let mut last_job = usize::MAX; for (node, indices) in node_borrows.drain(..) { @@ -159,7 +159,7 @@ impl RenderGraphStager for DependentNodeStager { // get all nodes without input. this intentionally includes nodes with no outputs let output_only_nodes = render_graph .iter_nodes() - .filter(|node| node.input_slots.len() == 0); + .filter(|node| node.input_slots.is_empty()); let mut stages = vec![Stage::default()]; let mut node_stages = HashMap::new(); for output_only_node in output_only_nodes { @@ -190,8 +190,7 @@ fn stage_node( .edges .input_edges .iter() - .find(|e| !node_stages_and_jobs.contains_key(&e.get_output_node())) - .is_some() + .any(|e| !node_stages_and_jobs.contains_key(&e.get_output_node())) { return; } diff --git a/crates/bevy_render/src/renderer/render_context.rs b/crates/bevy_render/src/renderer/render_context.rs index 0fb2597f62..2da15a49a6 100644 --- a/crates/bevy_render/src/renderer/render_context.rs +++ b/crates/bevy_render/src/renderer/render_context.rs @@ -16,6 +16,7 @@ pub trait RenderContext { destination_offset: u64, size: u64, ); + #[allow(clippy::too_many_arguments)] fn copy_buffer_to_texture( &mut self, source_buffer: BufferId, diff --git a/crates/bevy_render/src/renderer/render_resource/buffer.rs b/crates/bevy_render/src/renderer/render_resource/buffer.rs index 04eb228087..e5104e140d 100644 --- a/crates/bevy_render/src/renderer/render_resource/buffer.rs +++ b/crates/bevy_render/src/renderer/render_resource/buffer.rs @@ -4,6 +4,7 @@ use uuid::Uuid; pub struct BufferId(Uuid); impl BufferId { + #[allow(clippy::new_without_default)] pub fn new() -> Self { BufferId(Uuid::new_v4()) } diff --git a/crates/bevy_render/src/renderer/render_resource/mod.rs b/crates/bevy_render/src/renderer/render_resource/mod.rs index 7810a87147..aa81167e63 100644 --- a/crates/bevy_render/src/renderer/render_resource/mod.rs +++ b/crates/bevy_render/src/renderer/render_resource/mod.rs @@ -1,5 +1,6 @@ mod bind_group; mod buffer; +#[allow(clippy::module_inception)] mod render_resource; mod render_resource_bindings; mod shared_buffers; diff --git a/crates/bevy_render/src/renderer/render_resource/render_resource_bindings.rs b/crates/bevy_render/src/renderer/render_resource/render_resource_bindings.rs index 1536531107..52423fa806 100644 --- a/crates/bevy_render/src/renderer/render_resource/render_resource_bindings.rs +++ b/crates/bevy_render/src/renderer/render_resource/render_resource_bindings.rs @@ -117,7 +117,7 @@ impl RenderResourceBindings { for (name, (vertex_buffer, index_buffer)) in render_resource_bindings.vertex_buffers.iter() { - self.set_vertex_buffer(name, *vertex_buffer, index_buffer.clone()); + self.set_vertex_buffer(name, *vertex_buffer, *index_buffer); } } @@ -241,7 +241,7 @@ impl AssetRenderResourceBindings { pub fn get_or_insert_mut(&mut self, handle: Handle) -> &mut RenderResourceBindings { self.bindings .entry(HandleUntyped::from(handle)) - .or_insert_with(|| RenderResourceBindings::default()) + .or_insert_with(RenderResourceBindings::default) } pub fn get_mut(&mut self, handle: Handle) -> Option<&mut RenderResourceBindings> { diff --git a/crates/bevy_render/src/renderer/render_resource/shared_buffers.rs b/crates/bevy_render/src/renderer/render_resource/shared_buffers.rs index 47dac74d6f..0f9d82217b 100644 --- a/crates/bevy_render/src/renderer/render_resource/shared_buffers.rs +++ b/crates/bevy_render/src/renderer/render_resource/shared_buffers.rs @@ -85,7 +85,7 @@ impl SharedBuffers { pub fn reset_command_queue(&self) -> CommandQueue { let mut command_queue = self.command_queue.write().unwrap(); - std::mem::replace(&mut *command_queue, CommandQueue::default()) + std::mem::take(&mut *command_queue) } } diff --git a/crates/bevy_render/src/renderer/render_resource/texture.rs b/crates/bevy_render/src/renderer/render_resource/texture.rs index 5e20227e65..06bafa787e 100644 --- a/crates/bevy_render/src/renderer/render_resource/texture.rs +++ b/crates/bevy_render/src/renderer/render_resource/texture.rs @@ -4,6 +4,7 @@ use uuid::Uuid; pub struct TextureId(Uuid); impl TextureId { + #[allow(clippy::new_without_default)] pub fn new() -> Self { TextureId(Uuid::new_v4()) } @@ -13,6 +14,7 @@ impl TextureId { pub struct SamplerId(Uuid); impl SamplerId { + #[allow(clippy::new_without_default)] pub fn new() -> Self { SamplerId(Uuid::new_v4()) } diff --git a/crates/bevy_render/src/shader/mod.rs b/crates/bevy_render/src/shader/mod.rs index 01fe65deb8..b3fa7d61cc 100644 --- a/crates/bevy_render/src/shader/mod.rs +++ b/crates/bevy_render/src/shader/mod.rs @@ -1,3 +1,4 @@ +#[allow(clippy::module_inception)] mod shader; mod shader_defs; mod shader_reflect; diff --git a/crates/bevy_render/src/shader/shader_reflect.rs b/crates/bevy_render/src/shader/shader_reflect.rs index 86bc07c611..bf4e2bb55d 100644 --- a/crates/bevy_render/src/shader/shader_reflect.rs +++ b/crates/bevy_render/src/shader/shader_reflect.rs @@ -61,7 +61,7 @@ impl ShaderLayout { } else { let parts = vertex_attribute_descriptor .name - .splitn(3, "_") + .splitn(3, '_') .collect::>(); if parts.len() == 3 { if parts[0] == "I" { @@ -82,13 +82,11 @@ impl ShaderLayout { }; if let Some(current) = current_descriptor.as_mut() { - if ¤t.name == ¤t_buffer_name { + if current.name == current_buffer_name { current.attributes.push(vertex_attribute_descriptor); continue; - } else { - if visited_buffer_descriptors.contains(¤t_buffer_name) { - panic!("Vertex attribute buffer names must be consecutive.") - } + } else if visited_buffer_descriptors.contains(¤t_buffer_name) { + panic!("Vertex attribute buffer names must be consecutive.") } } diff --git a/crates/bevy_render/src/texture/hdr_texture_loader.rs b/crates/bevy_render/src/texture/hdr_texture_loader.rs index c9b4628a99..013c9c3c08 100644 --- a/crates/bevy_render/src/texture/hdr_texture_loader.rs +++ b/crates/bevy_render/src/texture/hdr_texture_loader.rs @@ -13,7 +13,7 @@ impl AssetLoader for HdrTextureLoader { let format = TextureFormat::Rgba32Float; debug_assert_eq!( format.pixel_size(), - 4 * 4 * 1, + 4 * 4, "Format should have 32bit x 4 size" ); diff --git a/crates/bevy_render/src/texture/image_texture_loader.rs b/crates/bevy_render/src/texture/image_texture_loader.rs index e06266afd1..06b1b04be2 100644 --- a/crates/bevy_render/src/texture/image_texture_loader.rs +++ b/crates/bevy_render/src/texture/image_texture_loader.rs @@ -108,28 +108,29 @@ impl AssetLoader for ImageTextureLoader { data = raw_data.as_slice().as_bytes().to_owned(); } - image::DynamicImage::ImageRgb16(i) => { - width = i.width(); - height = i.height(); + + image::DynamicImage::ImageRgb16(image) => { + width = image.width(); + height = image.height(); format = TextureFormat::Rgba16Uint; - let mut d = + let mut local_data = Vec::with_capacity(width as usize * height as usize * format.pixel_size()); - for pixel in i.into_raw().chunks_exact(3) { + for pixel in image.into_raw().chunks_exact(3) { // TODO unsafe_get in release builds? let r = pixel[0]; let g = pixel[1]; let b = pixel[2]; let a = u16::max_value(); - d.extend_from_slice(&r.to_ne_bytes()); - d.extend_from_slice(&g.to_ne_bytes()); - d.extend_from_slice(&b.to_ne_bytes()); - d.extend_from_slice(&a.to_ne_bytes()); + local_data.extend_from_slice(&r.to_ne_bytes()); + local_data.extend_from_slice(&g.to_ne_bytes()); + local_data.extend_from_slice(&b.to_ne_bytes()); + local_data.extend_from_slice(&a.to_ne_bytes()); } - data = d; + data = local_data; } image::DynamicImage::ImageRgba16(i) => { width = i.width(); diff --git a/crates/bevy_render/src/texture/mod.rs b/crates/bevy_render/src/texture/mod.rs index daa11e563e..70ccf030a4 100644 --- a/crates/bevy_render/src/texture/mod.rs +++ b/crates/bevy_render/src/texture/mod.rs @@ -3,6 +3,7 @@ mod hdr_texture_loader; #[cfg(feature = "png")] mod image_texture_loader; mod sampler_descriptor; +#[allow(clippy::module_inception)] mod texture; mod texture_descriptor; mod texture_dimension; diff --git a/crates/bevy_render/src/texture/texture.rs b/crates/bevy_render/src/texture/texture.rs index bbcdc29eeb..cf22a88c0e 100644 --- a/crates/bevy_render/src/texture/texture.rs +++ b/crates/bevy_render/src/texture/texture.rs @@ -155,7 +155,7 @@ impl RenderResource for Option> { } fn texture(&self) -> Option> { - self.clone() + *self } } @@ -171,6 +171,6 @@ impl RenderResource for Handle { } fn texture(&self) -> Option> { - Some(self.clone()) + Some(*self) } } diff --git a/crates/bevy_ron/src/error.rs b/crates/bevy_ron/src/error.rs index 2a5052159a..7cf7145b40 100644 --- a/crates/bevy_ron/src/error.rs +++ b/crates/bevy_ron/src/error.rs @@ -14,6 +14,7 @@ pub struct Error { pub type Result = std::result::Result; #[derive(Clone, Debug, PartialEq)] +#[non_exhaustive] pub enum ErrorCode { Io(String), Message(String), @@ -54,9 +55,6 @@ pub enum ErrorCode { Utf8Error(Utf8Error), TrailingCharacters, - - #[doc(hidden)] - __Nonexhaustive, } impl fmt::Display for Error { @@ -108,7 +106,6 @@ impl fmt::Display for ErrorCode { ErrorCode::UnderscoreAtBeginning => f.write_str("Found underscore at the beginning"), ErrorCode::UnexpectedByte(_) => f.write_str("Unexpected byte"), ErrorCode::TrailingCharacters => f.write_str("Non-whitespace trailing characters"), - _ => f.write_str("Unknown ErrorCode"), } } } diff --git a/crates/bevy_ron/src/ser/mod.rs b/crates/bevy_ron/src/ser/mod.rs index d3b8c7ab5f..3d16ac47f6 100644 --- a/crates/bevy_ron/src/ser/mod.rs +++ b/crates/bevy_ron/src/ser/mod.rs @@ -62,6 +62,7 @@ struct Pretty { /// .indentor("\t".to_owned()); /// ``` #[derive(Clone, Debug, Serialize, Deserialize)] +#[non_exhaustive] pub struct PrettyConfig { /// Limit the pretty-ness up to the given depth. #[serde(default = "default_depth_limit")] @@ -83,9 +84,6 @@ pub struct PrettyConfig { pub decimal_floats: bool, /// Enable extensions. Only configures 'implicit_some' for now. pub extensions: Extensions, - /// Private field to ensure adding a field is non-breaking. - #[serde(skip)] - _future_proof: (), } impl PrettyConfig { @@ -206,7 +204,6 @@ impl Default for PrettyConfig { enumerate_arrays: default_enumerate_arrays(), extensions: Extensions::default(), decimal_floats: default_decimal_floats(), - _future_proof: (), } } } @@ -391,7 +388,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer { fn serialize_f32(self, v: f32) -> Result<()> { write!(self.output, "{}", v)?; // TODO: use f32::EPSILON when minimum supported rust version is 1.43 - pub const EPSILON: f32 = 1.19209290e-07_f32; + pub const EPSILON: f32 = 1.1920929e-7_f32; if self.decimal_floats() && (v - v.floor()).abs() < EPSILON { write!(self.output, ".0")?; } @@ -401,7 +398,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer { fn serialize_f64(self, v: f64) -> Result<()> { write!(self.output, "{}", v)?; // TODO: use f64::EPSILON when minimum supported rust version is 1.43 - pub const EPSILON: f64 = 2.2204460492503131e-16_f64; + pub const EPSILON: f64 = 2.220446049250313e-16_f64; if self.decimal_floats() && (v - v.floor()).abs() < EPSILON { write!(self.output, ".0")?; } diff --git a/crates/bevy_scene/src/scene_spawner.rs b/crates/bevy_scene/src/scene_spawner.rs index c4f659fb86..d9fe6f96cb 100644 --- a/crates/bevy_scene/src/scene_spawner.rs +++ b/crates/bevy_scene/src/scene_spawner.rs @@ -73,7 +73,7 @@ impl SceneSpawner { let spawned = self .spawned_scenes .entry(scene_handle) - .or_insert_with(|| Vec::new()); + .or_insert_with(Vec::new); spawned.push(instance_id); Ok(()) } @@ -98,7 +98,7 @@ impl SceneSpawner { *instance_info .entity_map .entry(scene_entity.entity) - .or_insert_with(|| bevy_ecs::Entity::new()) + .or_insert_with(bevy_ecs::Entity::new) } else { bevy_ecs::Entity::from_id(scene_entity.entity) }; diff --git a/crates/bevy_sprite/src/render/mod.rs b/crates/bevy_sprite/src/render/mod.rs index 2499b2e94f..7de7e8876d 100644 --- a/crates/bevy_sprite/src/render/mod.rs +++ b/crates/bevy_sprite/src/render/mod.rs @@ -109,10 +109,10 @@ pub fn build_sprite_pipeline(shaders: &mut Assets) -> PipelineDescriptor } pub mod node { - pub const COLOR_MATERIAL: &'static str = "color_material"; - pub const SPRITE: &'static str = "sprite"; - pub const SPRITE_SHEET: &'static str = "sprite_sheet"; - pub const SPRITE_SHEET_SPRITE: &'static str = "sprite_sheet_sprite"; + pub const COLOR_MATERIAL: &str = "color_material"; + pub const SPRITE: &str = "sprite"; + pub const SPRITE_SHEET: &str = "sprite_sheet"; + pub const SPRITE_SHEET_SPRITE: &str = "sprite_sheet_sprite"; } pub trait SpriteRenderGraphBuilder { diff --git a/crates/bevy_sprite/src/texture_atlas.rs b/crates/bevy_sprite/src/texture_atlas.rs index 7c242fa3aa..5858672692 100644 --- a/crates/bevy_sprite/src/texture_atlas.rs +++ b/crates/bevy_sprite/src/texture_atlas.rs @@ -93,6 +93,10 @@ impl TextureAtlas { self.textures.len() } + pub fn is_empty(&self) -> bool { + self.textures.is_empty() + } + pub fn get_texture_index(&self, texture: Handle) -> Option { self.texture_handles .as_ref() diff --git a/crates/bevy_transform/src/components/translation.rs b/crates/bevy_transform/src/components/translation.rs index f676e2c2a0..50574c3a01 100644 --- a/crates/bevy_transform/src/components/translation.rs +++ b/crates/bevy_transform/src/components/translation.rs @@ -25,7 +25,7 @@ impl Default for Translation { impl From for Translation { fn from(translation: Vec3) -> Self { - Self(Vec3::from(translation)) + Self(translation) } } diff --git a/crates/bevy_transform/src/hierarchy/child_builder.rs b/crates/bevy_transform/src/hierarchy/child_builder.rs index 51b462516f..4acef50ce6 100644 --- a/crates/bevy_transform/src/hierarchy/child_builder.rs +++ b/crates/bevy_transform/src/hierarchy/child_builder.rs @@ -32,7 +32,7 @@ impl WorldWriter for InsertChildren { // NOTE: ideally this is just an else statement, but currently that _incorrectly_ fails borrow-checking if !added { world - .insert_one(self.parent, Children(SmallVec::from(self.children))) + .insert_one(self.parent, Children(self.children)) .unwrap(); } } @@ -73,7 +73,7 @@ impl WorldWriter for PushChildren { // NOTE: ideally this is just an else statement, but currently that _incorrectly_ fails borrow-checking if !added { world - .insert_one(self.parent, Children(SmallVec::from(self.children))) + .insert_one(self.parent, Children(self.children)) .unwrap(); } } diff --git a/crates/bevy_transform/src/hierarchy/hierarchy.rs b/crates/bevy_transform/src/hierarchy/hierarchy.rs index 34040e53ec..354c08d614 100644 --- a/crates/bevy_transform/src/hierarchy/hierarchy.rs +++ b/crates/bevy_transform/src/hierarchy/hierarchy.rs @@ -14,13 +14,10 @@ where { // TODO: not a huge fan of this pattern. are there ways to do recursive updates in legion without allocations? // TODO: the problem above might be resolvable with world splitting - let children = children_query.get::(entity).ok().map(|children| { - children - .0 - .iter() - .map(|entity| *entity) - .collect::>() - }); + let children = children_query + .get::(entity) + .ok() + .map(|children| children.0.iter().cloned().collect::>()); let parent_result = run(state, entity, parent_result, previous_result); previous_result = None; @@ -47,13 +44,11 @@ pub struct DespawnRecursive { } fn despawn_with_children_recursive(world: &mut World, entity: Entity) { - if let Some(children) = world.get::(entity).ok().map(|children| { - children - .0 - .iter() - .map(|entity| *entity) - .collect::>() - }) { + if let Some(children) = world + .get::(entity) + .ok() + .map(|children| children.0.iter().cloned().collect::>()) + { for e in children { despawn_with_children_recursive(world, e); } diff --git a/crates/bevy_transform/src/hierarchy/mod.rs b/crates/bevy_transform/src/hierarchy/mod.rs index b2fb6d4ce3..76d685a381 100644 --- a/crates/bevy_transform/src/hierarchy/mod.rs +++ b/crates/bevy_transform/src/hierarchy/mod.rs @@ -1,4 +1,5 @@ mod child_builder; +#[allow(clippy::module_inception)] mod hierarchy; mod hierarchy_maintenance_system; mod world_child_builder; diff --git a/crates/bevy_type_registry/src/type_registry.rs b/crates/bevy_type_registry/src/type_registry.rs index b741faa735..c4c1b0b055 100644 --- a/crates/bevy_type_registry/src/type_registry.rs +++ b/crates/bevy_type_registry/src/type_registry.rs @@ -59,10 +59,8 @@ impl ComponentRegistry { let mut registration = self.get_with_short_name(type_name); if registration.is_none() { registration = self.get_with_full_name(type_name); - if registration.is_none() { - if self.ambigous_names.contains(type_name) { - panic!("Type name is ambiguous: {}", type_name); - } + if registration.is_none() && self.ambigous_names.contains(type_name) { + panic!("Type name is ambiguous: {}", type_name); } } registration @@ -99,11 +97,7 @@ impl ComponentRegistration { component_properties_fn: |archetype: &Archetype, index: usize| { // the type has been looked up by the caller, so this is safe unsafe { - let ptr = archetype - .get::() - .unwrap() - .as_ptr() - .offset(index as isize); + let ptr = archetype.get::().unwrap().as_ptr().add(index); ptr.as_ref().unwrap() } }, diff --git a/crates/bevy_ui/src/flex/convert.rs b/crates/bevy_ui/src/flex/convert.rs index 61698eb80f..30de449998 100644 --- a/crates/bevy_ui/src/flex/convert.rs +++ b/crates/bevy_ui/src/flex/convert.rs @@ -44,8 +44,8 @@ impl From<&Style> for stretch::style::Style { margin: from_rect(value.margin), padding: from_rect(value.padding), border: from_rect(value.border), - flex_grow: value.flex_grow.into(), - flex_shrink: value.flex_shrink.into(), + flex_grow: value.flex_grow, + flex_shrink: value.flex_shrink, flex_basis: value.flex_basis.into(), size: from_size(value.size), min_size: from_size(value.min_size), diff --git a/crates/bevy_ui/src/flex/mod.rs b/crates/bevy_ui/src/flex/mod.rs index ab76b3e429..c9a597ddff 100644 --- a/crates/bevy_ui/src/flex/mod.rs +++ b/crates/bevy_ui/src/flex/mod.rs @@ -31,8 +31,7 @@ impl FlexSurface { let stretch_style = style.into(); let stretch_node = self.entity_to_stretch.entry(entity).or_insert_with(|| { added = true; - let stretch_node = stretch.new_node(stretch_style, Vec::new()).unwrap(); - stretch_node + stretch.new_node(stretch_style, Vec::new()).unwrap() }); if !added { diff --git a/crates/bevy_ui/src/lib.rs b/crates/bevy_ui/src/lib.rs index 607f36260e..56ccaeb31a 100644 --- a/crates/bevy_ui/src/lib.rs +++ b/crates/bevy_ui/src/lib.rs @@ -33,7 +33,7 @@ use update::ui_z_system; pub struct UiPlugin; pub mod stage { - pub const UI: &'static str = "ui"; + pub const UI: &str = "ui"; } impl Plugin for UiPlugin { diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index e8d2c158e7..e3765a3a05 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -66,13 +66,13 @@ pub fn build_ui_pipeline(shaders: &mut Assets) -> PipelineDescriptor { } pub mod node { - pub const UI_CAMERA: &'static str = "ui_camera"; - pub const NODE: &'static str = "node"; - pub const UI_PASS: &'static str = "ui_pass"; + pub const UI_CAMERA: &str = "ui_camera"; + pub const NODE: &str = "node"; + pub const UI_PASS: &str = "ui_pass"; } pub mod camera { - pub const UI_CAMERA: &'static str = "UiCamera"; + pub const UI_CAMERA: &str = "UiCamera"; } pub trait UiRenderGraphBuilder { diff --git a/crates/bevy_ui/src/update.rs b/crates/bevy_ui/src/update.rs index 6032ad5c13..93e54b6358 100644 --- a/crates/bevy_ui/src/update.rs +++ b/crates/bevy_ui/src/update.rs @@ -51,5 +51,5 @@ fn update_node_entity( position.set_z(z); transform.set_w_axis(position); - return Some(global_z); + Some(global_z) } diff --git a/crates/bevy_ui/src/widget/image.rs b/crates/bevy_ui/src/widget/image.rs index 42350ef472..d469760389 100644 --- a/crates/bevy_ui/src/widget/image.rs +++ b/crates/bevy_ui/src/widget/image.rs @@ -21,15 +21,15 @@ pub fn image_node_system( mut query: Query<(&Image, &mut CalculatedSize, &Handle)>, ) { for (_image, mut calculated_size, material_handle) in &mut query.iter() { - materials + if let Some(texture) = materials .get(material_handle) .and_then(|material| material.texture) .and_then(|texture_handle| textures.get(&texture_handle)) - .map(|texture| { - calculated_size.size = Size { - width: texture.size.x(), - height: texture.size.y(), - }; - }); + { + calculated_size.size = Size { + width: texture.size.x(), + height: texture.size.y(), + }; + } } } diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index 7cad33605b..f631e82d1e 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -49,6 +49,7 @@ pub fn text_system( } } +#[allow(clippy::too_many_arguments)] pub fn draw_text_system( mut draw_context: DrawContext, fonts: Res>, diff --git a/crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs b/crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs index f32696b0b4..865a595fc9 100644 --- a/crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs +++ b/crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs @@ -59,6 +59,7 @@ impl WgpuRenderResourceContext { ); } + #[allow(clippy::too_many_arguments)] pub fn copy_buffer_to_texture( &self, command_encoder: &mut wgpu::CommandEncoder, @@ -466,7 +467,7 @@ impl RenderResourceContext for WgpuRenderResourceContext { RenderResourceBinding::Texture(resource) => { let texture_view = texture_views .get(&resource) - .expect(&format!("{:?}", resource)); + .unwrap_or_else(|| panic!("{:?}", resource)); wgpu::BindingResource::TextureView(texture_view) } RenderResourceBinding::Sampler(resource) => { @@ -495,7 +496,7 @@ impl RenderResourceContext for WgpuRenderResourceContext { let bind_group_info = bind_groups .entry(bind_group_descriptor_id) - .or_insert_with(|| WgpuBindGroupInfo::default()); + .or_insert_with(WgpuBindGroupInfo::default); bind_group_info .bind_groups .insert(bind_group.id, wgpu_bind_group); @@ -540,7 +541,7 @@ impl RenderResourceContext for WgpuRenderResourceContext { let buffer_slice = buffer.slice(..); let data = buffer_slice.map_async(wgpu::MapMode::Write); self.device.poll(wgpu::Maintain::Wait); - if let Err(_) = pollster::block_on(data) { + if pollster::block_on(data).is_err() { panic!("failed to map buffer to host"); } } diff --git a/crates/bevy_wgpu/src/wgpu_render_pass.rs b/crates/bevy_wgpu/src/wgpu_render_pass.rs index ec8faa977e..ef1ea961f1 100644 --- a/crates/bevy_wgpu/src/wgpu_render_pass.rs +++ b/crates/bevy_wgpu/src/wgpu_render_pass.rs @@ -61,7 +61,7 @@ impl<'a> RenderPass for WgpuRenderPass<'a> { .get(&bind_group_descriptor_id) { if let Some(wgpu_bind_group) = bind_group_info.bind_groups.get(&bind_group) { - const EMPTY: &'static [u32] = &[]; + const EMPTY: &[u32] = &[]; let dynamic_uniform_indices = if let Some(dynamic_uniform_indices) = dynamic_uniform_indices { dynamic_uniform_indices diff --git a/crates/bevy_wgpu/src/wgpu_type_converter.rs b/crates/bevy_wgpu/src/wgpu_type_converter.rs index e5bf27d3f7..5a7d2d4e61 100644 --- a/crates/bevy_wgpu/src/wgpu_type_converter.rs +++ b/crates/bevy_wgpu/src/wgpu_type_converter.rs @@ -185,16 +185,12 @@ impl WgpuFrom<&BindType> for wgpu::BindingType { properties: _properties, } => wgpu::BindingType::UniformBuffer { dynamic: *dynamic, - min_binding_size: bind_type - .get_uniform_size() - .and_then(|size| wgpu::BufferSize::new(size)), + min_binding_size: bind_type.get_uniform_size().and_then(wgpu::BufferSize::new), }, BindType::StorageBuffer { dynamic, readonly } => wgpu::BindingType::StorageBuffer { dynamic: *dynamic, readonly: *readonly, - min_binding_size: bind_type - .get_uniform_size() - .and_then(|size| wgpu::BufferSize::new(size)), + min_binding_size: bind_type.get_uniform_size().and_then(wgpu::BufferSize::new), }, BindType::SampledTexture { dimension, @@ -532,7 +528,7 @@ impl WgpuFrom for wgpu::SamplerDescriptor<'_> { lod_min_clamp: sampler_descriptor.lod_min_clamp, lod_max_clamp: sampler_descriptor.lod_max_clamp, compare: sampler_descriptor.compare_function.map(|c| c.wgpu_into()), - anisotropy_clamp: sampler_descriptor.anisotropy_clamp.clone(), + anisotropy_clamp: sampler_descriptor.anisotropy_clamp, ..Default::default() } } diff --git a/crates/bevy_window/src/lib.rs b/crates/bevy_window/src/lib.rs index dbc50d871f..45c81d14c7 100644 --- a/crates/bevy_window/src/lib.rs +++ b/crates/bevy_window/src/lib.rs @@ -44,11 +44,11 @@ impl Plugin for WindowPlugin { let window_descriptor = resources .get::() .map(|descriptor| (*descriptor).clone()) - .unwrap_or_else(|| WindowDescriptor::default()); + .unwrap_or_else(WindowDescriptor::default); let mut create_window_event = resources.get_mut::>().unwrap(); create_window_event.send(CreateWindow { id: WindowId::primary(), - descriptor: window_descriptor.clone(), + descriptor: window_descriptor, }); } diff --git a/crates/bevy_window/src/system.rs b/crates/bevy_window/src/system.rs index 19669fbb33..cf4db08cc8 100644 --- a/crates/bevy_window/src/system.rs +++ b/crates/bevy_window/src/system.rs @@ -15,8 +15,12 @@ pub fn exit_on_window_close_system( mut app_exit_events: ResMut>, window_close_requested_events: Res>, ) { - for _ in state.event_reader.iter(&window_close_requested_events) { + if state + .event_reader + .iter(&window_close_requested_events) + .next() + .is_some() + { app_exit_events.send(AppExit); - break; } } diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index c15e6d7b5c..9cfcdf581f 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -15,9 +15,13 @@ impl WindowId { pub fn is_primary(&self) -> bool { *self == WindowId::primary() } +} - pub fn to_string(&self) -> String { - self.0.to_simple().to_string() +use std::fmt; + +impl fmt::Display for WindowId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.to_simple().fmt(f) } } @@ -65,6 +69,7 @@ impl Window { } #[derive(Debug, Clone)] +#[allow(clippy::manual_non_exhaustive)] pub struct WindowDescriptor { pub width: u32, pub height: u32, @@ -72,6 +77,11 @@ pub struct WindowDescriptor { pub vsync: bool, pub resizable: bool, pub mode: WindowMode, + + // this is a manual implementation of the non exhaustive pattern, + // especially made to allow ..Default::default() + #[doc(hidden)] + pub __non_exhaustive: (), } impl Default for WindowDescriptor { @@ -83,6 +93,7 @@ impl Default for WindowDescriptor { vsync: true, resizable: true, mode: WindowMode::Windowed, + __non_exhaustive: (), } } } diff --git a/crates/bevy_winit/src/converters.rs b/crates/bevy_winit/src/converters.rs index aa4f12b478..3014da2d43 100644 --- a/crates/bevy_winit/src/converters.rs +++ b/crates/bevy_winit/src/converters.rs @@ -7,9 +7,7 @@ pub fn convert_keyboard_input(keyboard_input: &winit::event::KeyboardInput) -> K KeyboardInput { scan_code: keyboard_input.scancode, state: convert_element_state(keyboard_input.state), - key_code: keyboard_input - .virtual_keycode - .map(|v| convert_virtual_key_code(v)), + key_code: keyboard_input.virtual_keycode.map(convert_virtual_key_code), } } diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 69e64c783d..941064972a 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -115,22 +115,21 @@ pub fn winit_runner(mut app: App) { let mut mouse_button_input_events = app.resources.get_mut::>().unwrap(); mouse_button_input_events.send(MouseButtonInput { - button: converters::convert_mouse_button(button.into()), + button: converters::convert_mouse_button(button), state: converters::convert_element_state(state), }); } _ => {} }, - event::Event::DeviceEvent { ref event, .. } => match event { - DeviceEvent::MouseMotion { delta } => { + event::Event::DeviceEvent { ref event, .. } => { + if let DeviceEvent::MouseMotion { delta } = event { let mut mouse_motion_events = app.resources.get_mut::>().unwrap(); mouse_motion_events.send(MouseMotion { delta: Vec2::new(delta.0 as f32, delta.1 as f32), }); } - _ => {} - }, + } event::Event::MainEventsCleared => { handle_create_window_events( &mut app.resources, diff --git a/examples/3d/parenting.rs b/examples/3d/parenting.rs index abcf7a0d22..16ebd93c5f 100644 --- a/examples/3d/parenting.rs +++ b/examples/3d/parenting.rs @@ -17,7 +17,7 @@ struct Rotator; /// rotates the parent, which will result in the child also rotating fn rotator_system(time: Res