Skip to content

Commit

Permalink
Enable the unsafe_op_in_unsafe_fn lint (#11591)
Browse files Browse the repository at this point in the history
# Objective

- Partial fix of #11590

## Solution

- Enable `unsafe_op_in_unsafe_fn` at workspace level
- Fix the lint for most of the crates
  • Loading branch information
tguichaoua authored Jan 28, 2024
1 parent 79a2e5e commit b0f5d4d
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ match_same_arms = "warn"
semicolon_if_nothing_returned = "warn"
map_flatten = "warn"

[workspace.lints.rust]
unsafe_op_in_unsafe_fn = "warn"

[lints]
workspace = true

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl ReflectAsset {
handle: UntypedHandle,
) -> Option<&'w mut dyn Reflect> {
// SAFETY: requirements are deferred to the caller
(self.get_unchecked_mut)(world, handle)
unsafe { (self.get_unchecked_mut)(world, handle) }
}

/// Equivalent of [`Assets::add`]
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_dynamic_plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// FIXME(11590): remove this once the lint is fixed
#![allow(unsafe_op_in_unsafe_fn)]

mod loader;

pub use loader::*;
2 changes: 2 additions & 0 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FIXME(11590): remove this once the lint is fixed
#![allow(unsafe_op_in_unsafe_fn)]
#![warn(missing_docs)]
#![doc = include_str!("../README.md")]

Expand Down
8 changes: 5 additions & 3 deletions crates/bevy_gizmos/src/gizmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type GizmosState<T> = (
pub struct GizmosFetchState<T: GizmoConfigGroup> {
state: <GizmosState<T> as SystemParam>::State,
}
// SAFETY: All methods are delegated to existing `SystemParam` implemntations
// SAFETY: All methods are delegated to existing `SystemParam` implementations
unsafe impl<T: GizmoConfigGroup> SystemParam for Gizmos<'_, '_, T> {
type State = GizmosFetchState<T>;
type Item<'w, 's> = Gizmos<'w, 's, T>;
Expand All @@ -77,8 +77,10 @@ unsafe impl<T: GizmoConfigGroup> SystemParam for Gizmos<'_, '_, T> {
world: UnsafeWorldCell<'w>,
change_tick: Tick,
) -> Self::Item<'w, 's> {
let (f0, f1) =
GizmosState::<T>::get_param(&mut state.state, system_meta, world, change_tick);
// SAFETY: Delegated to existing `SystemParam` implementations
let (f0, f1) = unsafe {
GizmosState::<T>::get_param(&mut state.state, system_meta, world, change_tick)
};
// Accessing the GizmoConfigStore in the immediate mode API reduces performance significantly.
// Implementing SystemParam manually allows us to do it to here
// Having config available allows for early returns when gizmos are disabled
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_mikktspace/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#![allow(clippy::all, clippy::undocumented_unsafe_blocks)]
#![allow(
unsafe_op_in_unsafe_fn,
clippy::all,
clippy::undocumented_unsafe_blocks
)]

use glam::{Vec2, Vec3};

Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_ptr/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![doc = include_str!("../README.md")]
#![no_std]
#![warn(missing_docs)]
// FIXME(11590): remove this once the lint is fixed
#![allow(unsafe_op_in_unsafe_fn)]

use core::fmt::{self, Formatter, Pointer};
use core::{
Expand Down
6 changes: 4 additions & 2 deletions crates/bevy_reflect/src/type_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,8 @@ impl ReflectFromPtr {
/// `val` must be a pointer to value of the type that the [`ReflectFromPtr`] was constructed for.
/// This can be verified by checking that the type id returned by [`ReflectFromPtr::type_id`] is the expected one.
pub unsafe fn as_reflect<'a>(&self, val: Ptr<'a>) -> &'a dyn Reflect {
(self.from_ptr)(val)
// SAFETY: contract uphold by the caller.
unsafe { (self.from_ptr)(val) }
}

/// Convert `PtrMut` into `&mut dyn Reflect`.
Expand All @@ -550,7 +551,8 @@ impl ReflectFromPtr {
/// `val` must be a pointer to a value of the type that the [`ReflectFromPtr`] was constructed for
/// This can be verified by checking that the type id returned by [`ReflectFromPtr::type_id`] is the expected one.
pub unsafe fn as_reflect_mut<'a>(&self, val: PtrMut<'a>) -> &'a mut dyn Reflect {
(self.from_ptr_mut)(val)
// SAFETY: contract uphold by the caller.
unsafe { (self.from_ptr_mut)(val) }
}
/// Get a function pointer to turn a `Ptr` into `&dyn Reflect` for
/// the type this [`ReflectFromPtr`] was constructed for.
Expand Down
14 changes: 8 additions & 6 deletions crates/bevy_render/src/extract_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ where
// SAFETY:
// - The caller ensures that `world` is the same one that `init_state` was called with.
// - The caller ensures that no other `SystemParam`s will conflict with the accesses we have registered.
let main_world = Res::<MainWorld>::get_param(
&mut state.main_world_state,
system_meta,
world,
change_tick,
);
let main_world = unsafe {
Res::<MainWorld>::get_param(
&mut state.main_world_state,
system_meta,
world,
change_tick,
)
};
let item = state.state.get(main_world.into_inner());
Extract { item }
}
Expand Down

0 comments on commit b0f5d4d

Please sign in to comment.