Re: [PATCH 1/1] rust: use checked_div to make clippy happy

Richard Henderson <[email protected]> Thu, 19 Feb 2026 13:38:57 +1000
Newsgroups org.nongnu.qemu-rust,org.nongnu.qemu-devel
Message-ID <[email protected]>
On 2/19/26 05:24, John Snow wrote:
> When upgrading from Fedora 41 to Fedora 43 for CI tests, clippy begins
> complaining about not using checked_div instead of manually checking
> divisors. Make clippy happy and use checked_div() instead.
> 
> Signed-off-by: John Snow <[email protected]>
> ---
>   rust/hw/core/src/qdev.rs | 14 ++++++--------
>   1 file changed, 6 insertions(+), 8 deletions(-)
> 
> diff --git a/rust/hw/core/src/qdev.rs b/rust/hw/core/src/qdev.rs
> index 145e20a984f..c4a7312168f 100644
> --- a/rust/hw/core/src/qdev.rs
> +++ b/rust/hw/core/src/qdev.rs
> @@ -425,18 +425,16 @@ pub const fn period_from_ns(ns: u64) -> u64 {
>       }
>   
>       pub const fn period_from_hz(hz: u64) -> u64 {
> -        if hz == 0 {
> -            0
> -        } else {
> -            Self::PERIOD_1SEC / hz
> +        match Self::PERIOD_1SEC.checked_div(hz) {
> +            Some(value) => value,
> +            None => 0,
>           }
>       }
>   
>       pub const fn period_to_hz(period: u64) -> u64 {
> -        if period == 0 {
> -            0
> -        } else {
> -            Self::PERIOD_1SEC / period
> +        match period.checked_div(Self::PERIOD_1SEC) {
> +            Some(value) => value,
> +            None => 0,
>           }
>       }
>   

These conversions are inconsistent about the placement of divisor and dividend.

r~