rust: use checked_div()
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
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
@@ -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
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_divinstead of manually checking divisors. Make clippy happy and usechecked_div()instead.Signed-off-by John Snow <[email protected]>---
rust/hw/core/src/qdev.rs| 14 ++++++--------1 file changed, 6 insertions(+), 8 deletions(-)rust/hw/core/src/qdev.rs +3
-4@@ -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:
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
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_divinstead of manually checking divisors. Make clippy happy and usechecked_div()instead.Signed-off-by John Snow <[email protected]>---
rust/hw/core/src/qdev.rs| 14 ++++++--------1 file changed, 6 insertions(+), 8 deletions(-)rust/hw/core/src/qdev.rs +3
-4@@ -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
-4pub 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
Paolo Bonzini
AppliedQueued, thanks.
Paolo