diff --git a/.travis.yml b/.travis.yml index 0885d97..c4ecf94 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,7 +26,13 @@ branches: - trying.tmp before_script: - rustup component add rustfmt + - rustup component add clippy script: - cargo fmt -- --check + # Ideally we would run CLippy on all targets, but benchmarks depend on an + # unstable feature and only work on nightly. + #- cargo clippy --all-targets -- -D warnings + - cargo clippy -- -D warnings + - cargo clippy --tests -- -D warnings - cargo build --verbose - cargo test --verbose diff --git a/src/decode/lzma.rs b/src/decode/lzma.rs index e159d55..9961a09 100644 --- a/src/decode/lzma.rs +++ b/src/decode/lzma.rs @@ -225,10 +225,8 @@ where rangecoder: &mut rangecoder::RangeDecoder<'a, R>, ) -> error::Result<()> { loop { - if let Some(_) = self.unpacked_size { - if rangecoder.is_finished_ok()? { - break; - } + if self.unpacked_size.is_some() && rangecoder.is_finished_ok()? { + break; } let pos_state = self.output.len() & ((1 << self.pb) - 1); @@ -244,12 +242,10 @@ where self.state = if self.state < 4 { 0 + } else if self.state < 10 { + self.state - 3 } else { - if self.state < 10 { - self.state - 3 - } else { - self.state - 6 - } + self.state - 6 }; continue; } @@ -275,12 +271,10 @@ where let idx: usize; if !rangecoder.decode_bit(&mut self.is_rep_g1[self.state])? { idx = 1; + } else if !rangecoder.decode_bit(&mut self.is_rep_g2[self.state])? { + idx = 2; } else { - if !rangecoder.decode_bit(&mut self.is_rep_g2[self.state])? { - idx = 2; - } else { - idx = 3; - } + idx = 3; } // Update LRU let dist = self.rep[idx]; diff --git a/src/decode/rangecoder.rs b/src/decode/rangecoder.rs index c040c90..c4af27b 100644 --- a/src/decode/rangecoder.rs +++ b/src/decode/rangecoder.rs @@ -36,7 +36,7 @@ where #[inline] fn normalize(&mut self) -> io::Result<()> { trace!(" {{ range: {:08x}, code: {:08x} }}", self.range, self.code); - if self.range < 0x1000000 { + if self.range < 0x0100_0000 { self.range <<= 8; self.code = (self.code << 8) ^ (self.stream.read_u8()? as u32); @@ -171,12 +171,10 @@ impl LenDecoder { ) -> io::Result { if !rangecoder.decode_bit(&mut self.choice)? { Ok(self.low_coder[pos_state].parse(rangecoder)? as usize) + } else if !rangecoder.decode_bit(&mut self.choice2)? { + Ok(self.mid_coder[pos_state].parse(rangecoder)? as usize + 8) } else { - if !rangecoder.decode_bit(&mut self.choice2)? { - Ok(self.mid_coder[pos_state].parse(rangecoder)? as usize + 8) - } else { - Ok(self.high_coder.parse(rangecoder)? as usize + 16) - } + Ok(self.high_coder.parse(rangecoder)? as usize + 16) } } } diff --git a/src/decode/util.rs b/src/decode/util.rs index bb97cfd..86b4c85 100644 --- a/src/decode/util.rs +++ b/src/decode/util.rs @@ -9,7 +9,7 @@ pub fn read_tag(input: &mut R, tag: &[u8]) -> io::Result { pub fn is_eof(input: &mut R) -> io::Result { let buf = input.fill_buf()?; - Ok(buf.len() == 0) + Ok(buf.is_empty()) } pub fn discard(input: &mut R, n: usize) -> io::Result<()> { diff --git a/src/decode/xz.rs b/src/decode/xz.rs index 4aabfef..940f83b 100644 --- a/src/decode/xz.rs +++ b/src/decode/xz.rs @@ -129,9 +129,9 @@ where } if !util::is_eof(input)? { - return Err(error::Error::XZError(format!( - "Unexpected data after last XZ block" - ))); + return Err(error::Error::XZError( + "Unexpected data after last XZ block".to_string(), + )); } Ok(()) } @@ -193,9 +193,9 @@ where for _ in 0..padding_size { let byte = digested.read_u8()?; if byte != 0 { - return Err(error::Error::XZError(format!( - "Invalid index padding, must be null bytes" - ))); + return Err(error::Error::XZError( + "Invalid index padding, must be null bytes".to_string(), + )); } } } @@ -314,9 +314,9 @@ where for _ in 0..padding_size { let byte = count_input.read_u8()?; if byte != 0 { - return Err(error::Error::XZError(format!( - "Invalid block padding, must be null bytes" - ))); + return Err(error::Error::XZError( + "Invalid block padding, must be null bytes".to_string(), + )); } } check_checksum(count_input, tmpbuf.as_slice(), check_method)?; @@ -463,9 +463,9 @@ where } if !util::flush_zero_padding(input)? { - return Err(error::Error::XZError(format!( - "Invalid block header padding, must be null bytes" - ))); + return Err(error::Error::XZError( + "Invalid block header padding, must be null bytes".to_string(), + )); } Ok(BlockHeader { @@ -488,7 +488,7 @@ where } } - Err(error::Error::XZError(format!( - "Invalid multi-byte encoding" - ))) + Err(error::Error::XZError( + "Invalid multi-byte encoding".to_string(), + )) } diff --git a/src/encode/dumbencoder.rs b/src/encode/dumbencoder.rs index 3a9e719..43750d7 100644 --- a/src/encode/dumbencoder.rs +++ b/src/encode/dumbencoder.rs @@ -22,7 +22,7 @@ where W: io::Write, { pub fn from_stream(stream: &'a mut W, options: &Options) -> io::Result { - let dict_size = 0x800000; + let dict_size = 0x0080_0000; // Properties let props = (LC + 9 * (LP + 5 * PB)) as u8; diff --git a/src/encode/rangecoder.rs b/src/encode/rangecoder.rs index dc1eb9a..910cdbd 100644 --- a/src/encode/rangecoder.rs +++ b/src/encode/rangecoder.rs @@ -59,7 +59,7 @@ where } fn normalize(&mut self) -> io::Result<()> { - while self.range < 0x1000000 { + while self.range < 0x0100_0000 { debug!( "+ {{ range: {:08x}, low: {:010x}, cache: {:02x}, {} }}", self.range, self.low, self.cache, self.cachesz diff --git a/tests/lzma.rs b/tests/lzma.rs index 4a86d90..28e11d0 100644 --- a/tests/lzma.rs +++ b/tests/lzma.rs @@ -67,7 +67,7 @@ fn decompress_short_header() { assert_eq!( format!( "{:?}", - lzma_rs::lzma_decompress(&mut "".as_bytes(), &mut decomp).unwrap_err() + lzma_rs::lzma_decompress(&mut (b"" as &[u8]), &mut decomp).unwrap_err() ), String::from("LZMAError(\"LZMA header too short: failed to fill whole buffer\")") )