Skip to content

Commit b0f5d4d

Browse files
authored
Enable the unsafe_op_in_unsafe_fn lint (#11591)
# Objective - Partial fix of #11590 ## Solution - Enable `unsafe_op_in_unsafe_fn` at workspace level - Fix the lint for most of the crates
1 parent 79a2e5e commit b0f5d4d

File tree

9 files changed

+33
-13
lines changed

9 files changed

+33
-13
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ match_same_arms = "warn"
4040
semicolon_if_nothing_returned = "warn"
4141
map_flatten = "warn"
4242

43+
[workspace.lints.rust]
44+
unsafe_op_in_unsafe_fn = "warn"
45+
4346
[lints]
4447
workspace = true
4548

crates/bevy_asset/src/reflect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl ReflectAsset {
8888
handle: UntypedHandle,
8989
) -> Option<&'w mut dyn Reflect> {
9090
// SAFETY: requirements are deferred to the caller
91-
(self.get_unchecked_mut)(world, handle)
91+
unsafe { (self.get_unchecked_mut)(world, handle) }
9292
}
9393

9494
/// Equivalent of [`Assets::add`]

crates/bevy_dynamic_plugin/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// FIXME(11590): remove this once the lint is fixed
2+
#![allow(unsafe_op_in_unsafe_fn)]
3+
14
mod loader;
25

36
pub use loader::*;

crates/bevy_ecs/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// FIXME(11590): remove this once the lint is fixed
2+
#![allow(unsafe_op_in_unsafe_fn)]
13
#![warn(missing_docs)]
24
#![doc = include_str!("../README.md")]
35

crates/bevy_gizmos/src/gizmos.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type GizmosState<T> = (
5252
pub struct GizmosFetchState<T: GizmoConfigGroup> {
5353
state: <GizmosState<T> as SystemParam>::State,
5454
}
55-
// SAFETY: All methods are delegated to existing `SystemParam` implemntations
55+
// SAFETY: All methods are delegated to existing `SystemParam` implementations
5656
unsafe impl<T: GizmoConfigGroup> SystemParam for Gizmos<'_, '_, T> {
5757
type State = GizmosFetchState<T>;
5858
type Item<'w, 's> = Gizmos<'w, 's, T>;
@@ -77,8 +77,10 @@ unsafe impl<T: GizmoConfigGroup> SystemParam for Gizmos<'_, '_, T> {
7777
world: UnsafeWorldCell<'w>,
7878
change_tick: Tick,
7979
) -> Self::Item<'w, 's> {
80-
let (f0, f1) =
81-
GizmosState::<T>::get_param(&mut state.state, system_meta, world, change_tick);
80+
// SAFETY: Delegated to existing `SystemParam` implementations
81+
let (f0, f1) = unsafe {
82+
GizmosState::<T>::get_param(&mut state.state, system_meta, world, change_tick)
83+
};
8284
// Accessing the GizmoConfigStore in the immediate mode API reduces performance significantly.
8385
// Implementing SystemParam manually allows us to do it to here
8486
// Having config available allows for early returns when gizmos are disabled

crates/bevy_mikktspace/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
#![allow(clippy::all, clippy::undocumented_unsafe_blocks)]
1+
#![allow(
2+
unsafe_op_in_unsafe_fn,
3+
clippy::all,
4+
clippy::undocumented_unsafe_blocks
5+
)]
26

37
use glam::{Vec2, Vec3};
48

crates/bevy_ptr/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![doc = include_str!("../README.md")]
22
#![no_std]
33
#![warn(missing_docs)]
4+
// FIXME(11590): remove this once the lint is fixed
5+
#![allow(unsafe_op_in_unsafe_fn)]
46

57
use core::fmt::{self, Formatter, Pointer};
68
use core::{

crates/bevy_reflect/src/type_registry.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,8 @@ impl ReflectFromPtr {
540540
/// `val` must be a pointer to value of the type that the [`ReflectFromPtr`] was constructed for.
541541
/// This can be verified by checking that the type id returned by [`ReflectFromPtr::type_id`] is the expected one.
542542
pub unsafe fn as_reflect<'a>(&self, val: Ptr<'a>) -> &'a dyn Reflect {
543-
(self.from_ptr)(val)
543+
// SAFETY: contract uphold by the caller.
544+
unsafe { (self.from_ptr)(val) }
544545
}
545546

546547
/// Convert `PtrMut` into `&mut dyn Reflect`.
@@ -550,7 +551,8 @@ impl ReflectFromPtr {
550551
/// `val` must be a pointer to a value of the type that the [`ReflectFromPtr`] was constructed for
551552
/// This can be verified by checking that the type id returned by [`ReflectFromPtr::type_id`] is the expected one.
552553
pub unsafe fn as_reflect_mut<'a>(&self, val: PtrMut<'a>) -> &'a mut dyn Reflect {
553-
(self.from_ptr_mut)(val)
554+
// SAFETY: contract uphold by the caller.
555+
unsafe { (self.from_ptr_mut)(val) }
554556
}
555557
/// Get a function pointer to turn a `Ptr` into `&dyn Reflect` for
556558
/// the type this [`ReflectFromPtr`] was constructed for.

crates/bevy_render/src/extract_param.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,14 @@ where
8383
// SAFETY:
8484
// - The caller ensures that `world` is the same one that `init_state` was called with.
8585
// - The caller ensures that no other `SystemParam`s will conflict with the accesses we have registered.
86-
let main_world = Res::<MainWorld>::get_param(
87-
&mut state.main_world_state,
88-
system_meta,
89-
world,
90-
change_tick,
91-
);
86+
let main_world = unsafe {
87+
Res::<MainWorld>::get_param(
88+
&mut state.main_world_state,
89+
system_meta,
90+
world,
91+
change_tick,
92+
)
93+
};
9294
let item = state.state.get(main_world.into_inner());
9395
Extract { item }
9496
}

0 commit comments

Comments
 (0)