Skip to content

Commit da00088

Browse files
authored
style: address many Clippy lints
PR #392
1 parent f973cd4 commit da00088

File tree

11 files changed

+53
-91
lines changed

11 files changed

+53
-91
lines changed

csv-core/src/lib.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ mod writer;
114114
///
115115
/// Use this to specify the record terminator while parsing CSV. The default is
116116
/// CRLF, which treats `\r`, `\n` or `\r\n` as a single record terminator.
117-
#[derive(Clone, Copy, Debug)]
117+
#[derive(Clone, Copy, Debug, Default)]
118118
#[non_exhaustive]
119119
pub enum Terminator {
120120
/// Parses `\r`, `\n` or `\r\n` as a single record terminator.
121+
#[default]
121122
CRLF,
122123
/// Parses the byte given as a record terminator.
123124
Any(u8),
@@ -140,14 +141,8 @@ impl Terminator {
140141
}
141142
}
142143

143-
impl Default for Terminator {
144-
fn default() -> Terminator {
145-
Terminator::CRLF
146-
}
147-
}
148-
149144
/// The quoting style to use when writing CSV data.
150-
#[derive(Clone, Copy, Debug)]
145+
#[derive(Clone, Copy, Debug, Default)]
151146
#[non_exhaustive]
152147
pub enum QuoteStyle {
153148
/// This puts quotes around every field. Always.
@@ -159,6 +154,7 @@ pub enum QuoteStyle {
159154
/// (which is indistinguishable from a record with one empty field).
160155
///
161156
/// This is the default.
157+
#[default]
162158
Necessary,
163159
/// This puts quotes around all fields that are non-numeric. Namely, when
164160
/// writing a field that does not parse as a valid float or integer, then
@@ -167,9 +163,3 @@ pub enum QuoteStyle {
167163
/// This *never* writes quotes, even if it would produce invalid CSV data.
168164
Never,
169165
}
170-
171-
impl Default for QuoteStyle {
172-
fn default() -> QuoteStyle {
173-
QuoteStyle::Necessary
174-
}
175-
}

csv-core/src/reader.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ enum NfaState {
439439
}
440440

441441
/// A list of NFA states that have an explicit representation in the DFA.
442-
const NFA_STATES: &'static [NfaState] = &[
442+
const NFA_STATES: &[NfaState] = &[
443443
NfaState::StartRecord,
444444
NfaState::StartField,
445445
NfaState::EndFieldDelim,
@@ -455,21 +455,18 @@ const NFA_STATES: &'static [NfaState] = &[
455455
impl NfaState {
456456
/// Returns true if this state indicates that a field has been parsed.
457457
fn is_field_final(&self) -> bool {
458-
match *self {
458+
matches!(
459+
*self,
459460
NfaState::End
460-
| NfaState::EndRecord
461-
| NfaState::CRLF
462-
| NfaState::EndFieldDelim => true,
463-
_ => false,
464-
}
461+
| NfaState::EndRecord
462+
| NfaState::CRLF
463+
| NfaState::EndFieldDelim
464+
)
465465
}
466466

467467
/// Returns true if this state indicates that a record has been parsed.
468468
fn is_record_final(&self) -> bool {
469-
match *self {
470-
NfaState::End | NfaState::EndRecord | NfaState::CRLF => true,
471-
_ => false,
472-
}
469+
matches!(*self, NfaState::End | NfaState::EndRecord | NfaState::CRLF)
473470
}
474471
}
475472

@@ -1238,11 +1235,11 @@ impl DfaClasses {
12381235
panic!("added too many classes")
12391236
}
12401237
self.classes[b as usize] = self.next_class as u8;
1241-
self.next_class = self.next_class + 1;
1238+
self.next_class += 1;
12421239
}
12431240

12441241
fn num_classes(&self) -> usize {
1245-
self.next_class as usize
1242+
self.next_class
12461243
}
12471244

12481245
/// Scan and copy the input bytes to the output buffer quickly.

csv-core/src/writer.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl WriterBuilder {
2828
double_quote: true,
2929
comment: None,
3030
};
31-
WriterBuilder { wtr: wtr }
31+
WriterBuilder { wtr }
3232
}
3333

3434
/// Builder a CSV writer from this configuration.
@@ -189,13 +189,9 @@ pub struct Writer {
189189

190190
impl Clone for Writer {
191191
fn clone(&self) -> Writer {
192-
let mut requires_quotes = [false; 256];
193-
for i in 0..256 {
194-
requires_quotes[i] = self.requires_quotes[i];
195-
}
196192
Writer {
197193
state: self.state.clone(),
198-
requires_quotes: requires_quotes,
194+
requires_quotes: self.requires_quotes,
199195
delimiter: self.delimiter,
200196
term: self.term,
201197
style: self.style,
@@ -393,7 +389,7 @@ impl Writer {
393389
self.state.quoting = false;
394390
}
395391
let (res, o) = match self.term {
396-
Terminator::CRLF => write_pessimistic(&[b'\r', b'\n'], output),
392+
Terminator::CRLF => write_pessimistic(b"\r\n", output),
397393
Terminator::Any(b) => write_pessimistic(&[b], output),
398394
};
399395
if o == 0 {
@@ -515,7 +511,7 @@ pub fn is_non_numeric(input: &[u8]) -> bool {
515511
// I suppose this could be faster if we wrote validators of numbers instead
516512
// of using the actual parser, but that's probably a lot of work for a bit
517513
// of a niche feature.
518-
!s.parse::<f64>().is_ok() && !s.parse::<i128>().is_ok()
514+
s.parse::<f64>().is_err() && s.parse::<i128>().is_err()
519515
}
520516

521517
/// Escape quotes `input` and writes the result to `output`.

src/byte_record.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ impl Bounds {
698698
/// If there are no fields, this returns `0`.
699699
#[inline]
700700
fn end(&self) -> usize {
701-
self.ends().last().map(|&i| i).unwrap_or(0)
701+
self.ends().last().copied().unwrap_or(0)
702702
}
703703

704704
/// Returns the number of fields in these bounds.

src/debug.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ impl<'a> core::fmt::Debug for Bytes<'a> {
5050
pub(crate) fn utf8_decode(bytes: &[u8]) -> Option<Result<char, u8>> {
5151
fn len(byte: u8) -> Option<usize> {
5252
if byte <= 0x7F {
53-
return Some(1);
53+
Some(1)
5454
} else if byte & 0b1100_0000 == 0b1000_0000 {
55-
return None;
55+
None
5656
} else if byte <= 0b1101_1111 {
5757
Some(2)
5858
} else if byte <= 0b1110_1111 {

src/deserializer.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn deserialize_string_record<'de, D: Deserialize<'de>>(
2929
});
3030
D::deserialize(&mut deser).map_err(|err| {
3131
Error::new(ErrorKind::Deserialize {
32-
pos: record.position().map(Clone::clone),
32+
pos: record.position().cloned(),
3333
err,
3434
})
3535
})
@@ -46,7 +46,7 @@ pub fn deserialize_byte_record<'de, D: Deserialize<'de>>(
4646
});
4747
D::deserialize(&mut deser).map_err(|err| {
4848
Error::new(ErrorKind::Deserialize {
49-
pos: record.position().map(Clone::clone),
49+
pos: record.position().cloned(),
5050
err,
5151
})
5252
})
@@ -284,7 +284,7 @@ impl<'r> DeRecord<'r> for DeByteRecord<'r> {
284284

285285
#[inline]
286286
fn peek_field(&mut self) -> Option<&'r [u8]> {
287-
self.it.peek().map(|s| *s)
287+
self.it.peek().copied()
288288
}
289289

290290
fn error(&self, kind: DeserializeErrorKind) -> DeserializeError {
@@ -329,8 +329,8 @@ macro_rules! deserialize_int {
329329
visitor: V,
330330
) -> Result<V::Value, Self::Error> {
331331
let field = self.next_field()?;
332-
let num = if field.starts_with("0x") {
333-
<$inttype>::from_str_radix(&field[2..], 16)
332+
let num = if let Some(digits) = field.strip_prefix("0x") {
333+
<$inttype>::from_str_radix(digits, 16)
334334
} else {
335335
field.parse()
336336
};
@@ -425,7 +425,7 @@ impl<'a, 'de: 'a, T: DeRecord<'de>> Deserializer<'de>
425425
self,
426426
visitor: V,
427427
) -> Result<V::Value, Self::Error> {
428-
self.next_field().and_then(|f| visitor.visit_str(f.into()))
428+
self.next_field().and_then(|f| visitor.visit_str(f))
429429
}
430430

431431
fn deserialize_bytes<V: Visitor<'de>>(
@@ -449,7 +449,7 @@ impl<'a, 'de: 'a, T: DeRecord<'de>> Deserializer<'de>
449449
) -> Result<V::Value, Self::Error> {
450450
match self.peek_field() {
451451
None => visitor.visit_none(),
452-
Some(f) if f.is_empty() => {
452+
Some([]) => {
453453
self.next_field().expect("empty field");
454454
visitor.visit_none()
455455
}

src/error.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ impl Error {
4040
/// If this is true, the underlying `ErrorKind` is guaranteed to be
4141
/// `ErrorKind::Io`.
4242
pub fn is_io_error(&self) -> bool {
43-
match *self.0 {
44-
ErrorKind::Io(_) => true,
45-
_ => false,
46-
}
43+
matches!(*self.0, ErrorKind::Io(_))
4744
}
4845

4946
/// Return the position for this error, if one exists.

src/lib.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ pub mod tutorial;
166166
mod writer;
167167

168168
/// The quoting style to use when writing CSV data.
169-
#[derive(Clone, Copy, Debug)]
169+
#[derive(Clone, Copy, Debug, Default)]
170170
#[non_exhaustive]
171171
pub enum QuoteStyle {
172172
/// This puts quotes around every field. Always.
@@ -178,6 +178,7 @@ pub enum QuoteStyle {
178178
/// (which is indistinguishable from a record with one empty field).
179179
///
180180
/// This is the default.
181+
#[default]
181182
Necessary,
182183
/// This puts quotes around all fields that are non-numeric. Namely, when
183184
/// writing a field that does not parse as a valid float or integer, then
@@ -198,20 +199,15 @@ impl QuoteStyle {
198199
}
199200
}
200201

201-
impl Default for QuoteStyle {
202-
fn default() -> QuoteStyle {
203-
QuoteStyle::Necessary
204-
}
205-
}
206-
207202
/// A record terminator.
208203
///
209204
/// Use this to specify the record terminator while parsing CSV. The default is
210205
/// CRLF, which treats `\r`, `\n` or `\r\n` as a single record terminator.
211-
#[derive(Clone, Copy, Debug)]
206+
#[derive(Clone, Copy, Debug, Default)]
212207
#[non_exhaustive]
213208
pub enum Terminator {
214209
/// Parses `\r`, `\n` or `\r\n` as a single record terminator.
210+
#[default]
215211
CRLF,
216212
/// Parses the byte given as a record terminator.
217213
Any(u8),
@@ -227,17 +223,12 @@ impl Terminator {
227223
}
228224
}
229225

230-
impl Default for Terminator {
231-
fn default() -> Terminator {
232-
Terminator::CRLF
233-
}
234-
}
235-
236226
/// The whitespace preservation behaviour when reading CSV data.
237-
#[derive(Clone, Copy, Debug, PartialEq)]
227+
#[derive(Clone, Copy, Debug, Default, PartialEq)]
238228
#[non_exhaustive]
239229
pub enum Trim {
240230
/// Preserves fields and headers. This is the default.
231+
#[default]
241232
None,
242233
/// Trim whitespace from headers.
243234
Headers,
@@ -257,12 +248,6 @@ impl Trim {
257248
}
258249
}
259250

260-
impl Default for Trim {
261-
fn default() -> Trim {
262-
Trim::None
263-
}
264-
}
265-
266251
/// A custom Serde deserializer for possibly invalid `Option<T>` fields.
267252
///
268253
/// When deserializing CSV data, it is sometimes desirable to simply ignore

src/reader.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,7 @@ impl<R: io::Read> Reader<R> {
13301330
match headers.string_record {
13311331
Ok(ref record) => Ok(record),
13321332
Err(ref err) => Err(Error::new(ErrorKind::Utf8 {
1333-
pos: headers.byte_record.position().map(Clone::clone),
1333+
pos: headers.byte_record.position().cloned(),
13341334
err: err.clone(),
13351335
})),
13361336
}
@@ -1888,7 +1888,7 @@ impl ReaderState {
18881888
Some(expected) => {
18891889
if record.len() as u64 != expected {
18901890
return Err(Error::new(ErrorKind::UnequalLengths {
1891-
pos: record.position().map(Clone::clone),
1891+
pos: record.position().cloned(),
18921892
expected_len: expected,
18931893
len: record.len() as u64,
18941894
}));
@@ -1916,7 +1916,7 @@ impl<R: io::Read, D: DeserializeOwned> DeserializeRecordsIntoIter<R, D> {
19161916
let headers = if !rdr.state.has_headers {
19171917
None
19181918
} else {
1919-
rdr.headers().ok().map(Clone::clone)
1919+
rdr.headers().ok().cloned()
19201920
};
19211921
DeserializeRecordsIntoIter {
19221922
rdr,
@@ -1974,7 +1974,7 @@ impl<'r, R: io::Read, D: DeserializeOwned> DeserializeRecordsIter<'r, R, D> {
19741974
let headers = if !rdr.state.has_headers {
19751975
None
19761976
} else {
1977-
rdr.headers().ok().map(Clone::clone)
1977+
rdr.headers().ok().cloned()
19781978
};
19791979
DeserializeRecordsIter {
19801980
rdr,
@@ -1986,12 +1986,12 @@ impl<'r, R: io::Read, D: DeserializeOwned> DeserializeRecordsIter<'r, R, D> {
19861986

19871987
/// Return a reference to the underlying CSV reader.
19881988
pub fn reader(&self) -> &Reader<R> {
1989-
&self.rdr
1989+
self.rdr
19901990
}
19911991

19921992
/// Return a mutable reference to the underlying CSV reader.
19931993
pub fn reader_mut(&mut self) -> &mut Reader<R> {
1994-
&mut self.rdr
1994+
self.rdr
19951995
}
19961996
}
19971997

@@ -2064,12 +2064,12 @@ impl<'r, R: io::Read> StringRecordsIter<'r, R> {
20642064

20652065
/// Return a reference to the underlying CSV reader.
20662066
pub fn reader(&self) -> &Reader<R> {
2067-
&self.rdr
2067+
self.rdr
20682068
}
20692069

20702070
/// Return a mutable reference to the underlying CSV reader.
20712071
pub fn reader_mut(&mut self) -> &mut Reader<R> {
2072-
&mut self.rdr
2072+
self.rdr
20732073
}
20742074
}
20752075

@@ -2140,12 +2140,12 @@ impl<'r, R: io::Read> ByteRecordsIter<'r, R> {
21402140

21412141
/// Return a reference to the underlying CSV reader.
21422142
pub fn reader(&self) -> &Reader<R> {
2143-
&self.rdr
2143+
self.rdr
21442144
}
21452145

21462146
/// Return a mutable reference to the underlying CSV reader.
21472147
pub fn reader_mut(&mut self) -> &mut Reader<R> {
2148-
&mut self.rdr
2148+
self.rdr
21492149
}
21502150
}
21512151

0 commit comments

Comments
 (0)