Skip to content

Commit b3d3daa

Browse files
authored
Fix Clippy lints on WASM (#13030)
# Objective - Fixes #13024. ## Solution - Run `cargo clippy --target wasm32-unknown-unknown` until there are no more errors. - I recommend reviewing one commit at a time :) --- ## Changelog - Fixed Clippy lints for `wasm32-unknown-unknown` target. - Updated `bevy_transform`'s `README.md`.
1 parent 70d9dfd commit b3d3daa

File tree

8 files changed

+27
-17
lines changed

8 files changed

+27
-17
lines changed

crates/bevy_app/src/schedule_runner.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,10 @@ impl Plugin for ScheduleRunnerPlugin {
141141
let g = f.clone();
142142

143143
let c = move || {
144-
let mut app = Rc::get_mut(&mut rc).unwrap();
145-
let delay = tick(&mut app, wait);
146-
match delay {
147-
Ok(delay) => {
148-
set_timeout(f.borrow().as_ref().unwrap(), delay.unwrap_or(asap))
149-
}
150-
Err(_) => {}
144+
let app = Rc::get_mut(&mut rc).unwrap();
145+
let delay = tick(app, wait);
146+
if let Ok(delay) = delay {
147+
set_timeout(f.borrow().as_ref().unwrap(), delay.unwrap_or(asap));
151148
}
152149
};
153150
*g.borrow_mut() = Some(Closure::wrap(Box::new(c) as Box<dyn FnMut()>));

crates/bevy_asset/src/io/wasm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl HttpWasmAssetReader {
3737
}
3838
}
3939

40-
fn js_value_to_err<'a>(context: &'a str) -> impl FnOnce(JsValue) -> std::io::Error + 'a {
40+
fn js_value_to_err(context: &str) -> impl FnOnce(JsValue) -> std::io::Error + '_ {
4141
move |value| {
4242
let message = match JSON::stringify(&value) {
4343
Ok(js_str) => format!("Failed to {context}: {js_str}"),
@@ -81,7 +81,7 @@ impl HttpWasmAssetReader {
8181
Ok(reader)
8282
}
8383
404 => Err(AssetReaderError::NotFound(path)),
84-
status => Err(AssetReaderError::HttpError(status as u16)),
84+
status => Err(AssetReaderError::HttpError(status)),
8585
}
8686
}
8787
}
@@ -94,7 +94,7 @@ impl AssetReader for HttpWasmAssetReader {
9494

9595
async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
9696
let meta_path = get_meta_path(&self.root_path.join(path));
97-
Ok(self.fetch_bytes(meta_path).await?)
97+
self.fetch_bytes(meta_path).await
9898
}
9999

100100
async fn read_directory<'a>(

crates/bevy_ecs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct Position { x: f32, y: f32 }
3737

3838
### Worlds
3939

40-
Entities, Components, and Resources are stored in a `World`. Worlds, much like Rust std collections like HashSet and Vec, expose operations to insert, read, write, and remove the data they store.
40+
Entities, Components, and Resources are stored in a `World`. Worlds, much like `std::collections`'s `HashSet` and `Vec`, expose operations to insert, read, write, and remove the data they store.
4141

4242
```rust
4343
use bevy_ecs::world::World;

crates/bevy_render/src/view/window/screenshot.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl ScreenshotManager {
7979

8080
#[cfg(target_arch = "wasm32")]
8181
{
82-
match (|| {
82+
let save_screenshot = || {
8383
use image::EncodableLayout;
8484
use wasm_bindgen::{JsCast, JsValue};
8585

@@ -107,7 +107,9 @@ impl ScreenshotManager {
107107
html_element.click();
108108
web_sys::Url::revoke_object_url(&url)?;
109109
Ok::<(), JsValue>(())
110-
})() {
110+
};
111+
112+
match (save_screenshot)() {
111113
Ok(_) => info!("Screenshot saved to {}", path.display()),
112114
Err(e) => error!("Cannot save screenshot, error: {e:?}"),
113115
};

crates/bevy_tasks/src/single_threaded_task_pool.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::Arc;
22
use std::{cell::RefCell, future::Future, marker::PhantomData, mem, rc::Rc};
33

44
thread_local! {
5-
static LOCAL_EXECUTOR: async_executor::LocalExecutor<'static> = async_executor::LocalExecutor::new();
5+
static LOCAL_EXECUTOR: async_executor::LocalExecutor<'static> = const { async_executor::LocalExecutor::new() };
66
}
77

88
/// Used to create a [`TaskPool`].
@@ -105,11 +105,21 @@ impl TaskPool {
105105
F: for<'scope> FnOnce(&'env mut Scope<'scope, 'env, T>),
106106
T: Send + 'static,
107107
{
108+
// SAFETY: This safety comment applies to all references transmuted to 'env.
109+
// Any futures spawned with these references need to return before this function completes.
110+
// This is guaranteed because we drive all the futures spawned onto the Scope
111+
// to completion in this function. However, rust has no way of knowing this so we
112+
// transmute the lifetimes to 'env here to appease the compiler as it is unable to validate safety.
113+
// Any usages of the references passed into `Scope` must be accessed through
114+
// the transmuted reference for the rest of this function.
115+
108116
let executor = &async_executor::LocalExecutor::new();
117+
// SAFETY: As above, all futures must complete in this function so we can change the lifetime
109118
let executor: &'env async_executor::LocalExecutor<'env> =
110119
unsafe { mem::transmute(executor) };
111120

112121
let results: RefCell<Vec<Rc<RefCell<Option<T>>>>> = RefCell::new(Vec::new());
122+
// SAFETY: As above, all futures must complete in this function so we can change the lifetime
113123
let results: &'env RefCell<Vec<Rc<RefCell<Option<T>>>>> =
114124
unsafe { mem::transmute(&results) };
115125

@@ -120,6 +130,7 @@ impl TaskPool {
120130
env: PhantomData,
121131
};
122132

133+
// SAFETY: As above, all futures must complete in this function so we can change the lifetime
123134
let scope_ref: &'env mut Scope<'_, 'env, T> = unsafe { mem::transmute(&mut scope) };
124135

125136
f(scope_ref);

crates/bevy_transform/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Bevy Transform
22

3-
This crate is largely a 1:1 port from [legion_transform](https://github.com/AThilenius/legion_transform) (ecs: legion, math: nalgebra) to bevy (ecs: bevy_ecs, math: glam)
3+
This crate contains types and functions associated with the `Transform` component.

crates/bevy_winit/src/system.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ pub(crate) fn changed_windows(
293293

294294
#[cfg(target_arch = "wasm32")]
295295
if window.canvas != cache.window.canvas {
296-
window.canvas = cache.window.canvas.clone();
296+
window.canvas.clone_from(&cache.window.canvas);
297297
warn!(
298298
"Bevy currently doesn't support modifying the window canvas after initialization."
299299
);

crates/bevy_winit/src/winit_windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl WinitWindows {
191191
let window = web_sys::window().unwrap();
192192
let document = window.document().unwrap();
193193
let canvas = document
194-
.query_selector(&selector)
194+
.query_selector(selector)
195195
.expect("Cannot query for canvas element.");
196196
if let Some(canvas) = canvas {
197197
let canvas = canvas.dyn_into::<web_sys::HtmlCanvasElement>().ok();

0 commit comments

Comments
 (0)