From dd806bd9ee40533e6266b2aa4585380b4f1e8e63 Mon Sep 17 00:00:00 2001 From: Adrian Ratiu Date: Sat, 21 Dec 2024 14:18:02 +0200 Subject: [PATCH] coord: ranged1d: fix irrefutable if let warning I keep getting this annoying warning with rust 1.83: warning: irrefutable `if let` pattern --> plotters/src/coord/ranged1d/types/numeric.rs:29:20 | 29 | if let Ok(index) = Self::ValueType::try_from(index) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 365 | impl_discrete_trait!(RangedCoordusize); | -------------------------------------- in this macro invocation | = note: this pattern will always match, so the `if let` is useless = help: consider replacing the `if let` with a `let` = note: `#[warn(irrefutable_let_patterns)]` on by default = note: this warning originates in the macro `impl_discrete_trait` (in Nightly builds, run with -Z macro-backtrace for more info) This is because the conversion of usize to ValueType always passes, so we don't need the try_from() in all cases. An additional problem are potential overflows so we use checked_add() which returns None if the addition fails, thus also avoiding the irrefutable let pattern while also protecting against overflows. Signed-off-by: Adrian Ratiu --- plotters/src/coord/ranged1d/types/numeric.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/plotters/src/coord/ranged1d/types/numeric.rs b/plotters/src/coord/ranged1d/types/numeric.rs index e648aa04..7de59db1 100644 --- a/plotters/src/coord/ranged1d/types/numeric.rs +++ b/plotters/src/coord/ranged1d/types/numeric.rs @@ -26,10 +26,9 @@ macro_rules! impl_discrete_trait { } fn from_index(&self, index: usize) -> Option { - if let Ok(index) = Self::ValueType::try_from(index) { - return Some(self.0 + index); - } - None + Self::ValueType::try_from(index) + .ok() + .and_then(|index| self.0.checked_add(index)) } } };