From 8bebfcea07e621f3da51ed8c5a1ca3eac69c737b Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Fri, 15 Mar 2024 15:39:45 -0400 Subject: [PATCH] Fix `clippy::needless_late_init` Used `cargo clippy -- -A clippy::all -W clippy::needless_late_init` to find all such cases, and manually fix them Additionally simplified logic in backward_references/hq.rs, and converted while into for loop there --- src/enc/backward_references/hq.rs | 100 ++++++++++++++---------------- src/enc/brotli_bit_stream.rs | 19 +++--- src/enc/encode.rs | 9 ++- src/enc/mod.rs | 9 ++- src/enc/reader.rs | 9 ++- 5 files changed, 65 insertions(+), 81 deletions(-) diff --git a/src/enc/backward_references/hq.rs b/src/enc/backward_references/hq.rs index 6e1535ce..3005bbbd 100755 --- a/src/enc/backward_references/hq.rs +++ b/src/enc/backward_references/hq.rs @@ -775,7 +775,6 @@ fn UpdateNodes>( + i32::from(kDistanceCacheOffset[j])) as usize; let mut prev_ix: usize = cur_ix.wrapping_sub(backward); - let len: usize; let continuation: u8 = ringbuffer[cur_ix_masked.wrapping_add(best_len)]; if cur_ix_masked.wrapping_add(best_len) > ringbuffer_mask { break 'break29; @@ -783,66 +782,57 @@ fn UpdateNodes>( if backward > max_distance.wrapping_add(gap) { break 'continue30; } - if backward <= max_distance { - if prev_ix >= cur_ix { - break 'continue30; - } - prev_ix &= ringbuffer_mask; - if prev_ix.wrapping_add(best_len) > ringbuffer_mask - || continuation as i32 - != ringbuffer[(prev_ix.wrapping_add(best_len) as usize)] - as i32 - { - break 'continue30; - } - len = FindMatchLengthWithLimit( - &ringbuffer[(prev_ix as usize)..], - &ringbuffer[cur_ix_masked..], - max_len, - ); - } else { + if backward > max_distance { break 'continue30; } + + if prev_ix >= cur_ix { + break 'continue30; + } + prev_ix &= ringbuffer_mask; + if prev_ix.wrapping_add(best_len) > ringbuffer_mask + || continuation as i32 + != ringbuffer[prev_ix.wrapping_add(best_len)] as i32 { - let dist_cost: floatX = - base_cost + ZopfliCostModelGetDistanceCost(model, j); - let mut l: usize; - l = best_len.wrapping_add(1); - while l <= len { + break 'continue30; + } + + let dist_cost = base_cost + ZopfliCostModelGetDistanceCost(model, j); + let len = FindMatchLengthWithLimit( + &ringbuffer[prev_ix..], + &ringbuffer[cur_ix_masked..], + max_len, + ); + for l in best_len.wrapping_add(1)..=len { + { + let copycode: u16 = GetCopyLengthCode(l); + let cmdcode: u16 = + CombineLengthCodes(inscode, copycode, (j == 0usize) as i32); + let cost: floatX = (if (cmdcode as i32) < 128i32 { + base_cost + } else { + dist_cost + }) + GetCopyExtra(copycode) as (floatX) + + ZopfliCostModelGetCommandCost(model, cmdcode); + if cost + < match (nodes[pos.wrapping_add(l)]).u { + Union1::cost(cost) => cost, + _ => 0.0, + } { - let copycode: u16 = GetCopyLengthCode(l); - let cmdcode: u16 = CombineLengthCodes( - inscode, - copycode, - (j == 0usize) as i32, + UpdateZopfliNode( + nodes, + pos, + start, + l, + l, + backward, + j.wrapping_add(1), + cost, ); - let cost: floatX = (if (cmdcode as i32) < 128i32 { - base_cost - } else { - dist_cost - }) + GetCopyExtra(copycode) as (floatX) - + ZopfliCostModelGetCommandCost(model, cmdcode); - if cost - < match (nodes[pos.wrapping_add(l)]).u { - Union1::cost(cost) => cost, - _ => 0.0, - } - { - UpdateZopfliNode( - nodes, - pos, - start, - l, - l, - backward, - j.wrapping_add(1), - cost, - ); - result = brotli_max_size_t(result, l); - } - best_len = l; + result = brotli_max_size_t(result, l); } - l = l.wrapping_add(1); + best_len = l; } } } diff --git a/src/enc/brotli_bit_stream.rs b/src/enc/brotli_bit_stream.rs index 6f3000ec..2a843e65 100755 --- a/src/enc/brotli_bit_stream.rs +++ b/src/enc/brotli_bit_stream.rs @@ -312,13 +312,11 @@ fn process_command_queue<'a, CmdProcessor: interface::CommandProcessor<'a>>( let copylen_code: u32 = CommandCopyLenCode(cmd); let (prev_dist_index, dist_offset) = CommandDistanceIndexAndOffset(cmd, ¶ms.dist); - let final_distance: usize; - if prev_dist_index == 0 { - final_distance = dist_offset as usize; + let final_distance = if prev_dist_index == 0 { + dist_offset as usize } else { - final_distance = - (local_dist_cache[prev_dist_index - 1] as isize + dist_offset) as usize; - } + (local_dist_cache[prev_dist_index - 1] as isize + dist_offset) as usize + }; let copy_len = copylen_code as usize; let actual_copy_len: usize; let max_distance = core::cmp::min( @@ -1413,12 +1411,11 @@ fn NewBlockEncoder<'a, Alloc: alloc::Allocator + alloc::Allocator>( block_lengths: &'a [u32], num_blocks: usize, ) -> BlockEncoder<'a, Alloc> { - let block_len: usize; - if num_blocks != 0 && !block_lengths.is_empty() { - block_len = block_lengths[0] as usize; + let block_len = if num_blocks != 0 && !block_lengths.is_empty() { + block_lengths[0] as usize } else { - block_len = 0; - } + 0 + }; BlockEncoder:: { histogram_length_: histogram_length, num_block_types_: num_block_types, diff --git a/src/enc/encode.rs b/src/enc/encode.rs index 44b1a789..0372dd29 100755 --- a/src/enc/encode.rs +++ b/src/enc/encode.rs @@ -1660,15 +1660,14 @@ fn UpdateSizeHint( let delta: u64 = UnprocessedInputSize(s); let tail: u64 = available_in as u64; let limit: u32 = 1u32 << 30; - let total: u32; - if delta >= u64::from(limit) + let total: u32 = if delta >= u64::from(limit) || tail >= u64::from(limit) || delta.wrapping_add(tail) >= u64::from(limit) { - total = limit; + limit } else { - total = delta.wrapping_add(tail) as u32; - } + delta.wrapping_add(tail) as u32 + }; s.params.size_hint = total as usize; } } diff --git a/src/enc/mod.rs b/src/enc/mod.rs index a4fe0d0f..74464538 100755 --- a/src/enc/mod.rs +++ b/src/enc/mod.rs @@ -294,12 +294,11 @@ where } } } - let op: BrotliEncoderOperation; - if available_in == 0 { - op = BrotliEncoderOperation::BROTLI_OPERATION_FINISH; + let op = if available_in == 0 { + BrotliEncoderOperation::BROTLI_OPERATION_FINISH } else { - op = BrotliEncoderOperation::BROTLI_OPERATION_PROCESS; - } + BrotliEncoderOperation::BROTLI_OPERATION_PROCESS + }; let result = BrotliEncoderCompressStream( s, op, diff --git a/src/enc/reader.rs b/src/enc/reader.rs index bc77703c..e9af4325 100755 --- a/src/enc/reader.rs +++ b/src/enc/reader.rs @@ -226,12 +226,11 @@ impl, BufferType: SliceWrapperMut, Alloc: Br } } } - let op: BrotliEncoderOperation; - if avail_in == 0 { - op = BrotliEncoderOperation::BROTLI_OPERATION_FINISH; + let op = if avail_in == 0 { + BrotliEncoderOperation::BROTLI_OPERATION_FINISH } else { - op = BrotliEncoderOperation::BROTLI_OPERATION_PROCESS; - } + BrotliEncoderOperation::BROTLI_OPERATION_PROCESS + }; let ret = BrotliEncoderCompressStream( &mut self.state.0, op,