Skip to content

Commit 0fa0a7b

Browse files
authored
Make Clippy happier, minor cleanups (#283)
* Make Clippy happier, minor cleanups I ran `cargo clippy` and addressed some issues it raised, while leaving other issues for another time. Hopefully this will make this code a bit easier for newcomers to read. Biggest changes: * `foo.len() > 0` ➡ `!foo.is_empty()` * `return x;` ➡ `x` * `{ foo: foo }` ➡ `{ foo }` * adding a few `Default` trait implementations * some `match` statement cleanup Also, fixed two `ffi::uInt::max_value()` ➡ `ffi::uInt::MAX` due to deprecation. Biggest TODOs (please give feedback if they should be fixed too): * Clippy has complained about many `foo >> 0` -- which is a noop that might confuse (?) * some other corner cases that I don't know enough about * cargo fmt
1 parent a5a38c5 commit 0fa0a7b

File tree

8 files changed

+50
-60
lines changed

8 files changed

+50
-60
lines changed

src/bufreader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<R: Read> BufReader<R> {
4242

4343
pub fn with_buf(buf: Vec<u8>, inner: R) -> BufReader<R> {
4444
BufReader {
45-
inner: inner,
45+
inner,
4646
buf: buf.into_boxed_slice(),
4747
pos: 0,
4848
cap: 0,

src/crc.rs

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ pub struct CrcReader<R> {
2323
crc: Crc,
2424
}
2525

26+
impl Default for Crc {
27+
fn default() -> Self {
28+
Self::new()
29+
}
30+
}
31+
2632
impl Crc {
2733
/// Create a new CRC.
2834
pub fn new() -> Crc {

src/gz/bufread.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn copy(into: &mut [u8], from: &[u8], pos: &mut usize) -> usize {
2020
*slot = *val;
2121
}
2222
*pos += min;
23-
return min;
23+
min
2424
}
2525

2626
pub(crate) fn corrupt() -> io::Error {
@@ -126,15 +126,7 @@ pub(crate) fn read_gz_header<R: Read>(r: &mut R) -> io::Result<GzHeader> {
126126
let mut reader = Buffer::new(&mut part, r);
127127
read_gz_header_part(&mut reader)
128128
};
129-
130-
match result {
131-
Ok(()) => {
132-
return Ok(part.take_header());
133-
}
134-
Err(err) => {
135-
return Err(err);
136-
}
137-
};
129+
result.map(|()| part.take_header())
138130
}
139131

140132
/// A gzip streaming encoder
@@ -179,7 +171,7 @@ pub fn gz_encoder<R: BufRead>(header: Vec<u8>, r: R, lvl: Compression) -> GzEnco
179171
let crc = CrcReader::new(r);
180172
GzEncoder {
181173
inner: deflate::bufread::DeflateEncoder::new(crc, lvl),
182-
header: header,
174+
header,
183175
pos: 0,
184176
eof: false,
185177
}
@@ -363,7 +355,7 @@ impl GzHeaderPartial {
363355
}
364356

365357
pub fn take_header(self) -> GzHeader {
366-
return self.header;
358+
self.header
367359
}
368360
}
369361

@@ -443,7 +435,7 @@ where
443435
self.part.buf.truncate(0);
444436
self.buf_cur = 0;
445437
self.buf_max = 0;
446-
return Ok(rlen);
438+
Ok(rlen)
447439
}
448440
}
449441

@@ -523,7 +515,7 @@ impl<R: BufRead> Read for GzDecoder<R> {
523515
let mut reader = Buffer::new(&mut part, reader.get_mut().get_mut());
524516
read_gz_header_part(&mut reader)
525517
};
526-
let state = match result {
518+
match result {
527519
Ok(()) => {
528520
*header = Some(part.take_header());
529521
GzState::Body
@@ -533,8 +525,7 @@ impl<R: BufRead> Read for GzDecoder<R> {
533525
return Err(err);
534526
}
535527
Err(err) => return Err(err),
536-
};
537-
state
528+
}
538529
}
539530
GzState::Body => {
540531
if into.is_empty() {

src/gz/mod.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ pub struct GzBuilder {
117117
mtime: u32,
118118
}
119119

120+
impl Default for GzBuilder {
121+
fn default() -> Self {
122+
Self::new()
123+
}
124+
}
125+
120126
impl GzBuilder {
121127
/// Create a new blank builder with no header by default.
122128
pub fn new() -> GzBuilder {
@@ -204,28 +210,19 @@ impl GzBuilder {
204210
} = self;
205211
let mut flg = 0;
206212
let mut header = vec![0u8; 10];
207-
match extra {
208-
Some(v) => {
209-
flg |= FEXTRA;
210-
header.push((v.len() >> 0) as u8);
211-
header.push((v.len() >> 8) as u8);
212-
header.extend(v);
213-
}
214-
None => {}
213+
if let Some(v) = extra {
214+
flg |= FEXTRA;
215+
header.push((v.len() >> 0) as u8);
216+
header.push((v.len() >> 8) as u8);
217+
header.extend(v);
215218
}
216-
match filename {
217-
Some(filename) => {
218-
flg |= FNAME;
219-
header.extend(filename.as_bytes_with_nul().iter().map(|x| *x));
220-
}
221-
None => {}
219+
if let Some(filename) = filename {
220+
flg |= FNAME;
221+
header.extend(filename.as_bytes_with_nul().iter().map(|x| *x));
222222
}
223-
match comment {
224-
Some(comment) => {
225-
flg |= FCOMMENT;
226-
header.extend(comment.as_bytes_with_nul().iter().map(|x| *x));
227-
}
228-
None => {}
223+
if let Some(comment) = comment {
224+
flg |= FCOMMENT;
225+
header.extend(comment.as_bytes_with_nul().iter().map(|x| *x));
229226
}
230227
header[0] = 0x1f;
231228
header[1] = 0x8b;
@@ -248,7 +245,7 @@ impl GzBuilder {
248245
// default this value to 255. I'm not sure that if we "correctly" set
249246
// this it'd do anything anyway...
250247
header[9] = operating_system.unwrap_or(255);
251-
return header;
248+
header
252249
}
253250
}
254251

src/gz/read.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub struct GzEncoder<R> {
4343
}
4444

4545
pub fn gz_encoder<R: Read>(inner: bufread::GzEncoder<BufReader<R>>) -> GzEncoder<R> {
46-
GzEncoder { inner: inner }
46+
GzEncoder { inner }
4747
}
4848

4949
impl<R: Read> GzEncoder<R> {

src/gz/write.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn gz_encoder<W: Write>(header: Vec<u8>, w: W, lvl: Compression) -> GzEncode
4747
GzEncoder {
4848
inner: zio::Writer::new(w, Compress::new(lvl, false)),
4949
crc: Crc::new(),
50-
header: header,
50+
header,
5151
crc_bytes_written: 0,
5252
}
5353
}
@@ -134,7 +134,7 @@ impl<W: Write> GzEncoder<W> {
134134
}
135135

136136
fn write_header(&mut self) -> io::Result<()> {
137-
while self.header.len() > 0 {
137+
while !self.header.is_empty() {
138138
let n = self.inner.get_mut().write(&self.header)?;
139139
self.header.drain(..n);
140140
}
@@ -368,13 +368,11 @@ impl<W: Write> Write for GzDecoder<W> {
368368
} else {
369369
let (n, status) = self.inner.write_with_status(buf)?;
370370

371-
if status == Status::StreamEnd {
372-
if n < buf.len() && self.crc_bytes.len() < 8 {
373-
let remaining = buf.len() - n;
374-
let crc_bytes = cmp::min(remaining, CRC_BYTES_LEN - self.crc_bytes.len());
375-
self.crc_bytes.extend(&buf[n..n + crc_bytes]);
376-
return Ok(n + crc_bytes);
377-
}
371+
if status == Status::StreamEnd && n < buf.len() && self.crc_bytes.len() < 8 {
372+
let remaining = buf.len() - n;
373+
let crc_bytes = cmp::min(remaining, CRC_BYTES_LEN - self.crc_bytes.len());
374+
self.crc_bytes.extend(&buf[n..n + crc_bytes]);
375+
return Ok(n + crc_bytes);
378376
}
379377
Ok(n)
380378
}

src/mem.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl Compress {
283283
let stream = &mut *self.inner.inner.stream_wrapper;
284284
stream.msg = std::ptr::null_mut();
285285
let rc = unsafe {
286-
assert!(dictionary.len() < ffi::uInt::max_value() as usize);
286+
assert!(dictionary.len() < ffi::uInt::MAX as usize);
287287
ffi::deflateSetDictionary(stream, dictionary.as_ptr(), dictionary.len() as ffi::uInt)
288288
};
289289

@@ -367,7 +367,7 @@ impl Compress {
367367
self.compress(input, out, flush)
368368
};
369369
output.set_len((self.total_out() - before) as usize + len);
370-
return ret;
370+
ret
371371
}
372372
}
373373
}
@@ -508,7 +508,7 @@ impl Decompress {
508508
self.decompress(input, out, flush)
509509
};
510510
output.set_len((self.total_out() - before) as usize + len);
511-
return ret;
511+
ret
512512
}
513513
}
514514

@@ -518,7 +518,7 @@ impl Decompress {
518518
let stream = &mut *self.inner.inner.stream_wrapper;
519519
stream.msg = std::ptr::null_mut();
520520
let rc = unsafe {
521-
assert!(dictionary.len() < ffi::uInt::max_value() as usize);
521+
assert!(dictionary.len() < ffi::uInt::MAX as usize);
522522
ffi::inflateSetDictionary(stream, dictionary.as_ptr(), dictionary.len() as ffi::uInt)
523523
};
524524

src/zio.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ where
143143
// then we need to keep asking for more data because if we
144144
// return that 0 bytes of data have been read then it will
145145
// be interpreted as EOF.
146-
Ok(Status::Ok) | Ok(Status::BufError) if read == 0 && !eof && dst.len() > 0 => continue,
146+
Ok(Status::Ok) | Ok(Status::BufError) if read == 0 && !eof && !dst.is_empty() => {
147+
continue
148+
}
147149
Ok(Status::Ok) | Ok(Status::BufError) | Ok(Status::StreamEnd) => return Ok(read),
148150

149151
Err(..) => {
@@ -216,13 +218,9 @@ impl<W: Write, D: Ops> Writer<W, D> {
216218
let before_in = self.data.total_in();
217219
let ret = self.data.run_vec(buf, &mut self.buf, D::Flush::none());
218220
let written = (self.data.total_in() - before_in) as usize;
221+
let is_stream_end = matches!(ret, Ok(Status::StreamEnd));
219222

220-
let is_stream_end = match ret {
221-
Ok(Status::StreamEnd) => true,
222-
_ => false,
223-
};
224-
225-
if buf.len() > 0 && written == 0 && ret.is_ok() && !is_stream_end {
223+
if !buf.is_empty() && written == 0 && ret.is_ok() && !is_stream_end {
226224
continue;
227225
}
228226
return match ret {
@@ -240,7 +238,7 @@ impl<W: Write, D: Ops> Writer<W, D> {
240238
fn dump(&mut self) -> io::Result<()> {
241239
// TODO: should manage this buffer not with `drain` but probably more of
242240
// a deque-like strategy.
243-
while self.buf.len() > 0 {
241+
while !self.buf.is_empty() {
244242
let n = self.obj.as_mut().unwrap().write(&self.buf)?;
245243
if n == 0 {
246244
return Err(io::ErrorKind::WriteZero.into());

0 commit comments

Comments
 (0)