From 9033d72f1417cae9faa02e98cbdceccdff3e8d4d Mon Sep 17 00:00:00 2001 From: JieningYu Date: Sun, 25 Aug 2024 18:13:28 +0800 Subject: [PATCH] apply clippy fix --- Cargo.toml | 1 + crates/client/input/src/keyboard_input.rs | 8 +++---- crates/client/option/src/callbacks/mod.rs | 2 +- .../potential_values_based_callbacks.rs | 2 +- .../src/callbacks/suppliable_int_callbacks.rs | 9 +++---- .../validating_int_slider_callbacks.rs | 11 +-------- crates/client/option/src/enums/mod.rs | 8 +++---- crates/core/block-entity/src/lib.rs | 10 ++++++-- crates/core/block/src/lib.rs | 2 +- crates/core/component/src/dyn_any.rs | 6 +++-- crates/core/component/src/lib.rs | 24 ++++++++++++------- crates/util/downcast/src/lib.rs | 4 ++-- 12 files changed, 43 insertions(+), 44 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 00dd07c7..4b69b59d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,3 +32,4 @@ str-to-string = "warn" use-debug = "warn" iter-without-into-iter = "warn" decimal-literal-representation = "warn" +ref-as-ptr = "warn" diff --git a/crates/client/input/src/keyboard_input.rs b/crates/client/input/src/keyboard_input.rs index 59f14862..6cd48d85 100644 --- a/crates/client/input/src/keyboard_input.rs +++ b/crates/client/input/src/keyboard_input.rs @@ -16,12 +16,10 @@ impl Input { pub fn get_movement_modifier(positive: bool, negative: bool) -> f32 { if positive == negative { 0.0 + } else if positive { + 1.0 } else { - if positive { - 1.0 - } else { - -1.0 - } + -1.0 } } } diff --git a/crates/client/option/src/callbacks/mod.rs b/crates/client/option/src/callbacks/mod.rs index ba89141c..b2bd220b 100644 --- a/crates/client/option/src/callbacks/mod.rs +++ b/crates/client/option/src/callbacks/mod.rs @@ -21,7 +21,7 @@ trait CyclingCallbacks: Callbacks where Txt: ProvideTextTy, { - fn get_values(&self) -> (); // CyclingButtonWidget.Values + fn get_values(&self); // CyclingButtonWidget.Values fn value_setter(&self) -> ValueSetter { |option, value| option.set_value(value) diff --git a/crates/client/option/src/callbacks/potential_values_based_callbacks.rs b/crates/client/option/src/callbacks/potential_values_based_callbacks.rs index a4cd1276..629de1a2 100644 --- a/crates/client/option/src/callbacks/potential_values_based_callbacks.rs +++ b/crates/client/option/src/callbacks/potential_values_based_callbacks.rs @@ -8,7 +8,7 @@ impl CyclingCallbacks for PotentialValuesBasedCallbacks where Txt: ProvideTextTy, { - fn get_values(&self) -> () { + fn get_values(&self) { todo!() } } diff --git a/crates/client/option/src/callbacks/suppliable_int_callbacks.rs b/crates/client/option/src/callbacks/suppliable_int_callbacks.rs index 73d4d1b5..09d788ce 100644 --- a/crates/client/option/src/callbacks/suppliable_int_callbacks.rs +++ b/crates/client/option/src/callbacks/suppliable_int_callbacks.rs @@ -18,7 +18,7 @@ impl CyclingCallbacks for SuppliableIntCallbacks where Txt: ProvideTextTy, { - fn get_values(&self) -> () { + fn get_values(&self) { todo!() } } @@ -36,14 +36,11 @@ where } fn i32_validate(&self, value: Option) -> Option { - match value { - Some(value) => Some(rimecraft_math::clamp( + value.map(|value| rimecraft_math::clamp( value as f32, >::min_inclusive(self) as f32, >::max_inclusive(self) as f32, - ) as i32), - None => None, - } + ) as i32) } } diff --git a/crates/client/option/src/callbacks/validating_int_slider_callbacks.rs b/crates/client/option/src/callbacks/validating_int_slider_callbacks.rs index 05b36220..73e917f4 100644 --- a/crates/client/option/src/callbacks/validating_int_slider_callbacks.rs +++ b/crates/client/option/src/callbacks/validating_int_slider_callbacks.rs @@ -18,16 +18,7 @@ where } fn i32_validate(&self, value: Option) -> Option { - match value { - Some(value) => { - if value >= self.min && value <= self.max { - Some(value) - } else { - None - } - } - None => None, - } + value.filter(|&value| value >= self.min && value <= self.max) } } diff --git a/crates/client/option/src/enums/mod.rs b/crates/client/option/src/enums/mod.rs index aee2f053..3f585302 100644 --- a/crates/client/option/src/enums/mod.rs +++ b/crates/client/option/src/enums/mod.rs @@ -26,12 +26,10 @@ pub trait ByUSizeId: Sequence { if wraps { let id = id % size; Some(all.remove(id)) + } else if id < size { + Some(all.remove(id)) } else { - if id < size { - Some(all.remove(id)) - } else { - None - } + None } } } diff --git a/crates/core/block-entity/src/lib.rs b/crates/core/block-entity/src/lib.rs index a29b27d0..1e7a24a0 100644 --- a/crates/core/block-entity/src/lib.rs +++ b/crates/core/block-entity/src/lib.rs @@ -307,7 +307,10 @@ where pub unsafe fn downcast_ref(&self) -> Option<&RawBlockEntity<'w, T, Cx>> { if self.matches_type::() { unsafe { - Some(&*(self as *const BlockEntity<'w, Cx> as *const RawBlockEntity<'w, T, Cx>)) + Some( + &*(std::ptr::from_ref::>(self) + as *const RawBlockEntity<'w, T, Cx>), + ) } } else { None @@ -325,7 +328,10 @@ where pub unsafe fn downcast_mut(&mut self) -> Option<&mut RawBlockEntity<'w, T, Cx>> { if self.matches_type::() { unsafe { - Some(&mut *(self as *mut BlockEntity<'w, Cx> as *mut RawBlockEntity<'w, T, Cx>)) + Some( + &mut *(std::ptr::from_mut::>(self) + as *mut RawBlockEntity<'w, T, Cx>), + ) } } else { None diff --git a/crates/core/block/src/lib.rs b/crates/core/block/src/lib.rs index b8cfca78..a314af1b 100644 --- a/crates/core/block/src/lib.rs +++ b/crates/core/block/src/lib.rs @@ -183,6 +183,6 @@ where { fn hash(&self, state: &mut H) { self.block.hash(state); - (self.state as *const State<'_, Cx::BlockStateExt>).hash(state); + std::ptr::from_ref(self.state).hash(state); } } diff --git a/crates/core/component/src/dyn_any.rs b/crates/core/component/src/dyn_any.rs index 03329e72..245143c8 100644 --- a/crates/core/component/src/dyn_any.rs +++ b/crates/core/component/src/dyn_any.rs @@ -12,7 +12,7 @@ impl Any for T {} impl dyn Any + Send + Sync + '_ { pub unsafe fn downcast_ref(&self) -> Option<&T> { if (*self).type_id() == typeid::of::() { - unsafe { Some(&*(self as *const dyn Any as *const T)) } + unsafe { Some(&*(std::ptr::from_ref::(self) as *const T)) } } else { None } @@ -20,7 +20,9 @@ impl dyn Any + Send + Sync + '_ { pub unsafe fn downcast_mut(&mut self) -> Option<&mut T> { if (*self).type_id() == typeid::of::() { - unsafe { Some(&mut *(self as *mut dyn Any as *mut T)) } + unsafe { + Some(&mut *(std::ptr::from_mut::(self) as *mut T)) + } } else { None } diff --git a/crates/core/component/src/lib.rs b/crates/core/component/src/lib.rs index b0863ef6..3b571da9 100644 --- a/crates/core/component/src/lib.rs +++ b/crates/core/component/src/lib.rs @@ -60,12 +60,15 @@ where T: Clone + Eq + Hash + Send + Sync + 'a, { const UTIL: DynUtil<'a> = DynUtil { - clone: |obj| Box::new(unsafe { &*(obj as *const Object<'_> as *const T) }.clone()), + clone: |obj| { + Box::new(unsafe { &*(std::ptr::from_ref::>(obj) as *const T) }.clone()) + }, eq: |a, b| unsafe { - *(a as *const Object<'_> as *const T) == *(b as *const Object<'_> as *const T) + *(std::ptr::from_ref::>(a) as *const T) + == *(std::ptr::from_ref::>(b) as *const T) }, hash: |obj, mut state| { - let obj = unsafe { &*(obj as *const Object<'_> as *const T) }; + let obj = unsafe { &*(std::ptr::from_ref::>(obj) as *const T) }; obj.hash(&mut state); }, }; @@ -82,7 +85,9 @@ where { PacketCodec { codec: UnsafePacketCodec { - encode: |obj, buf| unsafe { &*(obj as *const Object<'_> as *const T) }.encode(buf), + encode: |obj, buf| { + unsafe { &*(std::ptr::from_ref::>(obj) as *const T) }.encode(buf) + }, decode: { assert!( >::SUPPORT_NON_IN_PLACE, @@ -91,7 +96,8 @@ where |buf| Ok(Box::new(T::decode(buf)?)) }, upd: |obj, buf| { - unsafe { &mut *(obj as *mut Object<'_> as *mut T) }.decode_in_place(buf) + unsafe { &mut *(std::ptr::from_mut::>(obj) as *mut T) } + .decode_in_place(buf) }, }, _marker: PhantomData, @@ -108,7 +114,7 @@ where codec: UnsafePacketCodec { encode: |obj, buf| { Cx::write_nbt( - unsafe { &*(obj as *const Object<'_> as *const T) }, + unsafe { &*(std::ptr::from_ref::>(obj) as *const T) }, buf.writer(), ) .map_err(Into::into) @@ -116,7 +122,7 @@ where decode: |buf| Ok(Box::new(Cx::read_nbt(buf.reader())?)), upd: |obj, buf| { Cx::update_nbt( - unsafe { &mut *(obj as *mut Object<'_> as *mut T) }, + unsafe { &mut *(std::ptr::from_mut::>(obj) as *mut T) }, buf.reader(), ) .map_err(Into::into) @@ -134,7 +140,7 @@ where SerdeCodec { codec: UnsafeSerdeCodec { ser: |obj| unsafe { - &*(obj as *const Object<'_> as *const T + &*(std::ptr::from_ref::>(obj) as *const T as *const (dyn erased_serde::Serialize + 'a)) }, de: |deserializer| { @@ -144,7 +150,7 @@ where }) }, upd: |obj, deserializer| { - *unsafe { &mut *(obj as *mut Object<'_> as *mut T) } = + *unsafe { &mut *(std::ptr::from_mut::>(obj) as *mut T) } = erased_serde::deserialize::(deserializer)?; Ok(()) }, diff --git a/crates/util/downcast/src/lib.rs b/crates/util/downcast/src/lib.rs index 06a0c613..f703aed7 100644 --- a/crates/util/downcast/src/lib.rs +++ b/crates/util/downcast/src/lib.rs @@ -76,7 +76,7 @@ impl Downcast { #[inline] pub unsafe fn downcast_ref(&self) -> Option<&V> { if self.is_safe::() { - unsafe { Some(&*(&self.value as *const T as *const V)) } + unsafe { Some(&*(core::ptr::from_ref::(&self.value) as *const V)) } } else { None } @@ -90,7 +90,7 @@ impl Downcast { #[inline] pub unsafe fn downcast_mut(&mut self) -> Option<&mut V> { if self.is_safe::() { - unsafe { Some(&mut *(&mut self.value as *mut T as *mut V)) } + unsafe { Some(&mut *(core::ptr::from_mut::(&mut self.value) as *mut V)) } } else { None }