Skip to content

Commit 856b39d

Browse files
Apply Clippy lints regarding lazy evaluation and closures (#14015)
# Objective - Lazily evaluate [default](https://rust-lang.github.io/rust-clippy/master/index.html#/unwrap_or_default)~~/[or](https://rust-lang.github.io/rust-clippy/master/index.html#/or_fun_call)~~ values where it makes sense - ~~`unwrap_or(foo())` -> `unwrap_or_else(|| foo())`~~ - `unwrap_or(Default::default())` -> `unwrap_or_default()` - etc. - Avoid creating [redundant closures](https://rust-lang.github.io/rust-clippy/master/index.html#/redundant_closure), even for [method calls](https://rust-lang.github.io/rust-clippy/master/index.html#/redundant_closure_for_method_calls) - `map(|something| something.into())` -> `map(Into:into)` ## Solution - Apply Clippy lints: - ~~[or_fun_call](https://rust-lang.github.io/rust-clippy/master/index.html#/or_fun_call)~~ - [unwrap_or_default](https://rust-lang.github.io/rust-clippy/master/index.html#/unwrap_or_default) - [redundant_closure_for_method_calls](https://rust-lang.github.io/rust-clippy/master/index.html#/redundant_closure_for_method_calls) ([redundant closures](https://rust-lang.github.io/rust-clippy/master/index.html#/redundant_closure) is already enabled) ## Testing - Tested on Windows 11 (`stable-x86_64-pc-windows-gnu`, 1.79.0) - Bevy compiles without errors or warnings and examples seem to work as intended - `cargo clippy` ✅ - `cargo run -p ci -- compile` ✅ --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
1 parent 9055fc1 commit 856b39d

File tree

67 files changed

+121
-126
lines changed

Some content is hidden

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

67 files changed

+121
-126
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ undocumented_unsafe_blocks = "warn"
3838
redundant_else = "warn"
3939
match_same_arms = "warn"
4040
semicolon_if_nothing_returned = "warn"
41+
redundant_closure_for_method_calls = "warn"
42+
unwrap_or_default = "warn"
4143

4244
ptr_as_ptr = "warn"
4345
ptr_cast_constness = "warn"

crates/bevy_animation/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,15 +612,15 @@ impl AnimationPlayer {
612612
pub fn all_finished(&self) -> bool {
613613
self.active_animations
614614
.values()
615-
.all(|playing_animation| playing_animation.is_finished())
615+
.all(ActiveAnimation::is_finished)
616616
}
617617

618618
/// Check if all playing animations are paused.
619619
#[doc(alias = "is_paused")]
620620
pub fn all_paused(&self) -> bool {
621621
self.active_animations
622622
.values()
623-
.all(|playing_animation| playing_animation.is_paused())
623+
.all(ActiveAnimation::is_paused)
624624
}
625625

626626
/// Resume all playing animations.

crates/bevy_app/src/app.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl App {
242242
let main = self.main_mut();
243243
main.plugin_registry = plugins;
244244
main.plugins_state = PluginsState::Finished;
245-
self.sub_apps.iter_mut().skip(1).for_each(|s| s.finish());
245+
self.sub_apps.iter_mut().skip(1).for_each(SubApp::finish);
246246
}
247247

248248
/// Runs [`Plugin::cleanup`] for each plugin. This is usually called by the event loop after
@@ -256,12 +256,12 @@ impl App {
256256
let main = self.main_mut();
257257
main.plugin_registry = plugins;
258258
main.plugins_state = PluginsState::Cleaned;
259-
self.sub_apps.iter_mut().skip(1).for_each(|s| s.cleanup());
259+
self.sub_apps.iter_mut().skip(1).for_each(SubApp::cleanup);
260260
}
261261

262262
/// Returns `true` if any of the sub-apps are building plugins.
263263
pub(crate) fn is_building_plugins(&self) -> bool {
264-
self.sub_apps.iter().any(|s| s.is_building_plugins())
264+
self.sub_apps.iter().any(SubApp::is_building_plugins)
265265
}
266266

267267
/// Adds one or more systems to the given schedule in this app's [`Schedules`].

crates/bevy_asset/src/io/file/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ pub(crate) fn get_base_path() -> PathBuf {
2222
PathBuf::from(manifest_dir)
2323
} else {
2424
env::current_exe()
25-
.map(|path| {
26-
path.parent()
27-
.map(|exe_parent_path| exe_parent_path.to_owned())
28-
.unwrap()
29-
})
25+
.map(|path| path.parent().map(ToOwned::to_owned).unwrap())
3026
.unwrap()
3127
}
3228
}

crates/bevy_asset/src/loader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ where
9393
.expect("AssetLoader settings should match the loader type");
9494
let asset = <L as AssetLoader>::load(self, reader, settings, &mut load_context)
9595
.await
96-
.map_err(|error| error.into())?;
96+
.map_err(Into::into)?;
9797
Ok(load_context.finish(asset, Some(meta)).into())
9898
})
9999
}
@@ -540,7 +540,7 @@ impl<'a> LoadContext<'a> {
540540
.meta
541541
.as_ref()
542542
.and_then(|m| m.processed_info().as_ref());
543-
let hash = info.map(|i| i.full_hash).unwrap_or(Default::default());
543+
let hash = info.map(|i| i.full_hash).unwrap_or_default();
544544
self.loader_dependencies.insert(path, hash);
545545
Ok(loaded_asset)
546546
}

crates/bevy_asset/src/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ impl<'a> AssetPath<'a> {
320320
AssetPath {
321321
source: self.source.into_owned(),
322322
path: self.path.into_owned(),
323-
label: self.label.map(|l| l.into_owned()),
323+
label: self.label.map(CowArc::into_owned),
324324
}
325325
}
326326

crates/bevy_asset/src/reflect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::any::{Any, TypeId};
33
use bevy_ecs::world::{unsafe_world_cell::UnsafeWorldCell, World};
44
use bevy_reflect::{FromReflect, FromType, Reflect};
55

6-
use crate::{Asset, Assets, Handle, UntypedAssetId, UntypedHandle};
6+
use crate::{Asset, AssetId, Assets, Handle, UntypedAssetId, UntypedHandle};
77

88
/// Type data for the [`TypeRegistry`](bevy_reflect::TypeRegistry) used to operate on reflected [`Asset`]s.
99
///
@@ -160,7 +160,7 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
160160
},
161161
ids: |world| {
162162
let assets = world.resource::<Assets<A>>();
163-
Box::new(assets.ids().map(|i| i.untyped()))
163+
Box::new(assets.ids().map(AssetId::untyped))
164164
},
165165
remove: |world, handle| {
166166
let mut assets = world.resource_mut::<Assets<A>>();

crates/bevy_asset/src/server/loaders.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl AssetLoaders {
212212
}
213213

214214
// Try extracting the extension from the path
215-
if let Some(full_extension) = asset_path.and_then(|path| path.get_full_extension()) {
215+
if let Some(full_extension) = asset_path.and_then(AssetPath::get_full_extension) {
216216
if let Some(&index) = try_extension(full_extension.as_str()) {
217217
return self.get_by_index(index);
218218
}

crates/bevy_asset/src/server/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl AssetServer {
230230

231231
let mut extensions = vec![full_extension.clone()];
232232
extensions.extend(
233-
AssetPath::iter_secondary_extensions(&full_extension).map(|e| e.to_string()),
233+
AssetPath::iter_secondary_extensions(&full_extension).map(ToString::to_string),
234234
);
235235

236236
MissingAssetLoaderForExtensionError { extensions }
@@ -493,7 +493,7 @@ impl AssetServer {
493493
force: bool,
494494
meta_transform: Option<MetaTransform>,
495495
) -> Result<UntypedHandle, AssetLoadError> {
496-
let asset_type_id = input_handle.as_ref().map(|handle| handle.type_id());
496+
let asset_type_id = input_handle.as_ref().map(UntypedHandle::type_id);
497497

498498
let path = path.into_owned();
499499
let path_clone = path.clone();
@@ -938,7 +938,7 @@ impl AssetServer {
938938
/// or is still "alive".
939939
pub fn get_handle<'a, A: Asset>(&self, path: impl Into<AssetPath<'a>>) -> Option<Handle<A>> {
940940
self.get_path_and_type_id_handle(&path.into(), TypeId::of::<A>())
941-
.map(|h| h.typed_debug_checked())
941+
.map(UntypedHandle::typed_debug_checked)
942942
}
943943

944944
/// Get a `Handle` from an `AssetId`.
@@ -949,7 +949,8 @@ impl AssetServer {
949949
/// Consider using [`Assets::get_strong_handle`] in the case the `Handle`
950950
/// comes from [`Assets::add`].
951951
pub fn get_id_handle<A: Asset>(&self, id: AssetId<A>) -> Option<Handle<A>> {
952-
self.get_id_handle_untyped(id.untyped()).map(|h| h.typed())
952+
self.get_id_handle_untyped(id.untyped())
953+
.map(UntypedHandle::typed)
953954
}
954955

955956
/// Get an `UntypedHandle` from an `UntypedAssetId`.

crates/bevy_core_pipeline/src/skybox/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl ExtractComponent for Skybox {
9999

100100
fn extract_component((skybox, exposure): QueryItem<'_, Self::QueryData>) -> Option<Self::Out> {
101101
let exposure = exposure
102-
.map(|e| e.exposure())
102+
.map(Exposure::exposure)
103103
.unwrap_or_else(|| Exposure::default().exposure());
104104

105105
Some((

0 commit comments

Comments
 (0)