Skip to contentSkip to conversation

rust: use checked_div()

PATCH Applied John Snow 4 participants 5 messages Feb 19 – Feb 25 Rust

lore
Share & tools
lore

John Snow

author
#0 lore raw

v2 cover rust: use checked_div()

When upgrading from Fedora 41 to Fedora 43 for our CI tests, (Which I do in
https://patchew.org/QEMU/[email protected]/),
clippy begins complaining about not using checked_div instead of manually checking divisors; see https://gitlab.com/jsnow/qemu/-/jobs/13169193931

This patch aims to make Clippy happy and prevent any regressions caused by the lcitool refresh.

v2:

- Fixed divisor/dividend order for period_to_hz; the correct order is DIVIDEND.checked_div(DIVISOR); I had a thinko where I just inverted the formula from period_from_hz(), believing it was a perfect inverse; it isn't! Thank you very much to Richard Henderson for noticing my mistake.

John Snow (1):
rust: use checked_div to make clippy happy

 rust/hw/core/src/qdev.rs | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
Signature · 1 line
2.53.0
author
#1 lore raw

PATCH v2 1/1 rust: use checked_div to make clippy happy

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.

---
 rust/hw/core/src/qdev.rs | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
rust/hw/core/src/qdev.rs +6 -8
index 145e20a984f..b2e5441079d 100644
@@ -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 Self::PERIOD_1SEC.checked_div(period) {
+            Some(value) => value,
+            None => 0,
         }
     }
 
Signature · 1 line
2.53.0
#2 lore raw

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

Quoted 25 lines · John Snow

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.

---
rust/hw/core/src/qdev.rs | 14 ++++++--------

1 file changed, 6 insertions(+), 8 deletions(-)
rust/hw/core/src/qdev.rs +3 -4
index 145e20a984f..b2e5441079d 100644
@@ -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,
        }

rust/hw/core/src/qdev.rs pub const fn period_from_ns(ns: u64) -> u64 { · line 437

Quoted 10 lines
rust/hw/core/src/qdev.rs +3 -4
    }
 
    pub const fn period_to_hz(period: u64) -> u64 {
-        if period == 0 {
-            0
-        } else {
-            Self::PERIOD_1SEC / period
+        match Self::PERIOD_1SEC.checked_div(period) {
+            Some(value) => value,
+            None => 0,

This looks like unwrap_or_default:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&code=fn%20main()%20%7B%0A%20%20let%20asdf%20%3D%20100u64%3B%0A%0A%20%20println!(%22%7B%3A%23%3F%7D%22%2C%20asdf.checked_div(5).unwrap_or_default())%3B%0A%20%20println!(%22%7B%3A%23%3F%7D%22%2C%20asdf.checked_div(0).unwrap_or_default())%3B%0A%7D%0A

in case you want to make it shorter O:-)

Quoted 7 lines
rust/hw/core/src/qdev.rs +0 -0
        }
    }
 
Signature · 1 line
2.53.0
#3 lore raw

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

Quoted 29 lines · Martin Kletzander
Quoted 27 lines · John Snow

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.

---
rust/hw/core/src/qdev.rs | 14 ++++++--------

1 file changed, 6 insertions(+), 8 deletions(-)
rust/hw/core/src/qdev.rs +3 -4
index 145e20a984f..b2e5441079d 100644
@@ -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,
        }
    }
 
Quoted 11 lines

rust/hw/core/src/qdev.rs pub const fn period_from_ns(ns: u64) -> u64 { · line 437

Quoted 8 lines
rust/hw/core/src/qdev.rs +3 -4
    pub const fn period_to_hz(period: u64) -> u64 {
-        if period == 0 {
-            0
-        } else {
-            Self::PERIOD_1SEC / period
+        match Self::PERIOD_1SEC.checked_div(period) {
+            Some(value) => value,
+            None => 0,

This looks like unwrap_or_default:

unwrap_or_default is not stable in const contexts so won't compile.

Quoted 10 lines

in case you want to make it shorter O:-)

Quoted 7 lines
rust/hw/core/src/qdev.rs +0 -1
        }
    }
 
--

2.53.0

Signature · 2 lines
Manos Pitsidianakis
Emulation and Virtualization Engineer at Linaro Ltd