Skip to contentSkip to conversation

rust: use checked_div()

PATCH Superseded John Snow 3 participants 5 messages Feb 18 – Feb 19 Rust

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

John Snow

author
#0 lore raw

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.

(Tested only via GitLab CI, and I am not exactly a rust wizard...)

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 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..c4a7312168f 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 period.checked_div(Self::PERIOD_1SEC) {
+            Some(value) => value,
+            None => 0,
         }
     }
 
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#2 lore raw

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

Quoted 7 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.

---

Quoted 35 lines
 rust/hw/core/src/qdev.rs | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
rust/hw/core/src/qdev.rs +6 -9
index 145e20a984f..c4a7312168f 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 period.checked_div(Self::PERIOD_1SEC) {
+            Some(value) => value,
+            None => 0,
         }
     }
 
--

2.53.0

#3 lore raw

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

In reply to John Snow
Quoted 28 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..c4a7312168f 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 {

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

Quoted 10 lines
rust/hw/core/src/qdev.rs +3 -4
-        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~

author
#4 lore raw

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

Quoted 43 lines · Richard Henderson
Quoted 28 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..c4a7312168f 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 {

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

Quoted 10 lines
rust/hw/core/src/qdev.rs +3 -4
-        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.

Quoted 2 lines

r~

Whoops, fugue state. I'll fix.