Skip to content

Commit fc1e4ea

Browse files
teksturiUwe Kleine-König
authored andcommitted
rust: pwm: Simplify to_result call sites and unsafe blocks
Remove unnecessary temporary variables around to_result() calls and move trailing semicolons outside unsafe blocks to improve readability and produce cleaner rustfmt output. No functional change intended. Signed-off-by: Kari Argillander <kari.argillander@gmail.com> Acked-by: Michal Wilczynski <m.wilczynski@samsung.com> Link: https://patch.msgid.link/20260102-pwm-rust-v2-2-2702ce57d571@gmail.com Signed-off-by: Uwe Kleine-König <ukleinek@kernel.org>
1 parent a2633dc commit fc1e4ea

1 file changed

Lines changed: 14 additions & 33 deletions

File tree

rust/kernel/pwm.rs

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ impl Device {
129129
// SAFETY: `self.as_raw()` provides a valid `*mut pwm_device` pointer.
130130
// `&c_wf` is a valid pointer to a `pwm_waveform` struct. The C function
131131
// handles all necessary internal locking.
132-
let ret = unsafe { bindings::pwm_set_waveform_might_sleep(self.as_raw(), &c_wf, exact) };
133-
to_result(ret)
132+
to_result(unsafe { bindings::pwm_set_waveform_might_sleep(self.as_raw(), &c_wf, exact) })
134133
}
135134

136135
/// Queries the hardware for the configuration it would apply for a given
@@ -160,9 +159,7 @@ impl Device {
160159

161160
// SAFETY: `self.as_raw()` is a valid pointer. We provide a valid pointer
162161
// to a stack-allocated `pwm_waveform` struct for the kernel to fill.
163-
let ret = unsafe { bindings::pwm_get_waveform_might_sleep(self.as_raw(), &mut c_wf) };
164-
165-
to_result(ret)?;
162+
to_result(unsafe { bindings::pwm_get_waveform_might_sleep(self.as_raw(), &mut c_wf) })?;
166163

167164
Ok(Waveform::from(c_wf))
168165
}
@@ -263,8 +260,8 @@ impl<T: PwmOps> Adapter<T> {
263260
core::ptr::from_ref::<T::WfHw>(wfhw).cast::<u8>(),
264261
wfhw_ptr.cast::<u8>(),
265262
size,
266-
);
267-
}
263+
)
264+
};
268265

269266
Ok(())
270267
}
@@ -284,8 +281,8 @@ impl<T: PwmOps> Adapter<T> {
284281
wfhw_ptr.cast::<u8>(),
285282
core::ptr::from_mut::<T::WfHw>(&mut wfhw).cast::<u8>(),
286283
size,
287-
);
288-
}
284+
)
285+
};
289286

290287
Ok(wfhw)
291288
}
@@ -311,9 +308,7 @@ impl<T: PwmOps> Adapter<T> {
311308
// Now, call the original release function to free the `pwm_chip` itself.
312309
// SAFETY: `dev` is the valid pointer passed into this callback, which is
313310
// the expected argument for `pwmchip_release`.
314-
unsafe {
315-
bindings::pwmchip_release(dev);
316-
}
311+
unsafe { bindings::pwmchip_release(dev) };
317312
}
318313

319314
/// # Safety
@@ -413,9 +408,7 @@ impl<T: PwmOps> Adapter<T> {
413408
match T::round_waveform_fromhw(chip, pwm, &wfhw, &mut rust_wf) {
414409
Ok(()) => {
415410
// SAFETY: `wf_ptr` is guaranteed valid by the C caller.
416-
unsafe {
417-
*wf_ptr = rust_wf.into();
418-
};
411+
unsafe { *wf_ptr = rust_wf.into() };
419412
0
420413
}
421414
Err(e) => e.to_errno(),
@@ -614,16 +607,12 @@ impl<T: PwmOps> Chip<T> {
614607
})?;
615608

616609
// SAFETY: `c_chip_ptr` points to a valid chip.
617-
unsafe {
618-
(*c_chip_ptr).dev.release = Some(Adapter::<T>::release_callback);
619-
}
610+
unsafe { (*c_chip_ptr).dev.release = Some(Adapter::<T>::release_callback) };
620611

621612
// SAFETY: `c_chip_ptr` points to a valid chip.
622613
// The `Adapter`'s `VTABLE` has a 'static lifetime, so the pointer
623614
// returned by `as_raw()` is always valid.
624-
unsafe {
625-
(*c_chip_ptr).ops = Adapter::<T>::VTABLE.as_raw();
626-
}
615+
unsafe { (*c_chip_ptr).ops = Adapter::<T>::VTABLE.as_raw() };
627616

628617
// Cast the `*mut bindings::pwm_chip` to `*mut Chip`. This is valid because
629618
// `Chip` is `repr(transparent)` over `Opaque<bindings::pwm_chip>`, and
@@ -645,9 +634,7 @@ unsafe impl<T: PwmOps> AlwaysRefCounted for Chip<T> {
645634
fn inc_ref(&self) {
646635
// SAFETY: `self.0.get()` points to a valid `pwm_chip` because `self` exists.
647636
// The embedded `dev` is valid. `get_device` increments its refcount.
648-
unsafe {
649-
bindings::get_device(&raw mut (*self.0.get()).dev);
650-
}
637+
unsafe { bindings::get_device(&raw mut (*self.0.get()).dev) };
651638
}
652639

653640
#[inline]
@@ -656,9 +643,7 @@ unsafe impl<T: PwmOps> AlwaysRefCounted for Chip<T> {
656643

657644
// SAFETY: `obj` is a valid pointer to a `Chip` (and thus `bindings::pwm_chip`)
658645
// with a non-zero refcount. `put_device` handles decrement and final release.
659-
unsafe {
660-
bindings::put_device(&raw mut (*c_chip_ptr).dev);
661-
}
646+
unsafe { bindings::put_device(&raw mut (*c_chip_ptr).dev) };
662647
}
663648
}
664649

@@ -692,9 +677,7 @@ impl<T: PwmOps> UnregisteredChip<'_, T> {
692677

693678
// SAFETY: `c_chip_ptr` points to a valid chip with its ops initialized.
694679
// `__pwmchip_add` is the C function to register the chip with the PWM core.
695-
unsafe {
696-
to_result(bindings::__pwmchip_add(c_chip_ptr, core::ptr::null_mut()))?;
697-
}
680+
to_result(unsafe { bindings::__pwmchip_add(c_chip_ptr, core::ptr::null_mut()) })?;
698681

699682
let registration = Registration {
700683
chip: ARef::clone(&self.chip),
@@ -730,9 +713,7 @@ impl<T: PwmOps> Drop for Registration<T> {
730713
// SAFETY: `chip_raw` points to a chip that was successfully registered.
731714
// `bindings::pwmchip_remove` is the correct C function to unregister it.
732715
// This `drop` implementation is called automatically by `devres` on driver unbind.
733-
unsafe {
734-
bindings::pwmchip_remove(chip_raw);
735-
}
716+
unsafe { bindings::pwmchip_remove(chip_raw) };
736717
}
737718
}
738719

0 commit comments

Comments
 (0)