From a3e59a85b745b8cfe06f87428d91642a02b90593 Mon Sep 17 00:00:00 2001 From: Shun Sakai Date: Sun, 27 Oct 2024 09:18:27 +0900 Subject: [PATCH] refactor: Rewrite manual `let-else` (#2366) --- src/codecs/gif.rs | 23 ++++++++++------------- src/codecs/pnm/decoder.rs | 17 +++++++---------- src/flat.rs | 5 ++--- tests/reference_images.rs | 5 ++--- 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/src/codecs/gif.rs b/src/codecs/gif.rs index 336849ece3..f77766d97b 100644 --- a/src/codecs/gif.rs +++ b/src/codecs/gif.rs @@ -308,19 +308,16 @@ impl Iterator for GifFrameIterator { // correct storage requirement if the result does not fit in `usize`. // on the other hand, `ImageBuffer::from_raw` detects overflow and // reports by returning `None`. - let mut frame_buffer = match ImageBuffer::from_raw(frame.width, frame.height, vec) { - Some(frame_buffer) => frame_buffer, - None => { - return Some(Err(ImageError::Unsupported( - UnsupportedError::from_format_and_kind( - ImageFormat::Gif.into(), - UnsupportedErrorKind::GenericFeature(format!( - "Image dimensions ({}, {}) are too large", - frame.width, frame.height - )), - ), - ))) - } + let Some(mut frame_buffer) = ImageBuffer::from_raw(frame.width, frame.height, vec) else { + return Some(Err(ImageError::Unsupported( + UnsupportedError::from_format_and_kind( + ImageFormat::Gif.into(), + UnsupportedErrorKind::GenericFeature(format!( + "Image dimensions ({}, {}) are too large", + frame.width, frame.height + )), + ), + ))); }; // blend the current frame with the non-disposed frame, then update diff --git a/src/codecs/pnm/decoder.rs b/src/codecs/pnm/decoder.rs index 48f40bb20b..01c2c54b44 100644 --- a/src/codecs/pnm/decoder.rs +++ b/src/codecs/pnm/decoder.rs @@ -540,17 +540,14 @@ trait HeaderReader: Read { } } - let (h, w, d, m) = match (height, width, depth, maxval) { - (Some(h), Some(w), Some(d), Some(m)) => (h, w, d, m), - _ => { - return Err(DecoderError::HeaderLineMissing { - height, - width, - depth, - maxval, - } - .into()) + let (Some(h), Some(w), Some(d), Some(m)) = (height, width, depth, maxval) else { + return Err(DecoderError::HeaderLineMissing { + height, + width, + depth, + maxval, } + .into()); }; let tupltype = match tupltype { diff --git a/src/flat.rs b/src/flat.rs index 687d480368..08f82475b9 100644 --- a/src/flat.rs +++ b/src/flat.rs @@ -421,9 +421,8 @@ impl SampleLayout { let idx_x = x.checked_mul(self.width_stride); let idx_y = y.checked_mul(self.height_stride); - let (idx_c, idx_x, idx_y) = match (idx_c, idx_x, idx_y) { - (Some(idx_c), Some(idx_x), Some(idx_y)) => (idx_c, idx_x, idx_y), - _ => return None, + let (Some(idx_c), Some(idx_x), Some(idx_y)) = (idx_c, idx_x, idx_y) else { + return None; }; Some(0usize) diff --git a/tests/reference_images.rs b/tests/reference_images.rs index 26740670e1..6ecaad3819 100644 --- a/tests/reference_images.rs +++ b/tests/reference_images.rs @@ -267,9 +267,8 @@ fn check_references() { } } - let test_img = match test_img.as_ref() { - Some(img) => img, - None => return, + let Some(test_img) = test_img.as_ref() else { + return; }; let test_crc_actual = {