Skip to content

Commit ea0720c

Browse files
nbdd0121jwnrt
authored andcommitted
Apply all clippy suggestions
Signed-off-by: Gary Guo <[email protected]>
1 parent 2b0128d commit ea0720c

File tree

8 files changed

+58
-73
lines changed

8 files changed

+58
-73
lines changed

annotate_derive/src/ast.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ impl<'a> Struct<'a> {
5959
let fields = Field::multiple_from_syn(&data.fields, span)?;
6060
Ok(Struct {
6161
original: node,
62-
attrs: attrs,
62+
attrs,
6363
ident: node.ident.clone(),
64-
fields: fields,
64+
fields,
6565
})
6666
}
6767
}
@@ -80,9 +80,9 @@ impl<'a> Enum<'a> {
8080
.collect::<Result<_>>()?;
8181
Ok(Enum {
8282
original: node,
83-
attrs: attrs,
83+
attrs,
8484
ident: node.ident.clone(),
85-
variants: variants,
85+
variants,
8686
})
8787
}
8888
}
@@ -116,7 +116,7 @@ impl<'a> Variant<'a> {
116116
let attrs = attr::get(&node.attrs)?;
117117
Ok(Variant {
118118
original: node,
119-
attrs: attrs,
119+
attrs,
120120
ident: node.ident.clone(),
121121
fields: Field::multiple_from_syn(&node.fields, span)?,
122122
})

examples/autoschema.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Schema {
6262
}
6363

6464
fn check_str(&mut self, s: &str) {
65-
if let Ok(_) = Int::from_str_radix(s, 0) {
65+
if Int::from_str_radix(s, 0).is_ok() {
6666
self.integer += 1;
6767
} else {
6868
match s {

src/integer.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,16 @@ impl Int {
184184
self.value.format(*base.unwrap_or(&Base::Dec), self.width)
185185
}
186186

187-
fn strip_numeric_prefix<'a>(src: &'a str, ch: u8) -> &'a str {
187+
fn strip_numeric_prefix(src: &str, ch: u8) -> &str {
188188
let lo = ['0', (ch | 0x20) as char];
189189
let up = ['0', (ch & !0x20) as char];
190-
if src.starts_with(&lo) || src.starts_with(&up) {
190+
if src.starts_with(lo) || src.starts_with(up) {
191191
&src[2..]
192192
} else {
193193
src
194194
}
195195
}
196-
fn detect_numeric_prefix<'a>(src: &'a str) -> (Base, &'a str) {
196+
fn detect_numeric_prefix(src: &str) -> (Base, &str) {
197197
let bytes = src.as_bytes();
198198
if src.len() >= 2 && bytes[0] == b'0' {
199199
match bytes[1] {

src/json.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -677,14 +677,7 @@ const SPACE: &str = "
677677

678678
// More strict than javascript.
679679
fn bad_identifier_char(ch: char) -> bool {
680-
match ch {
681-
'0'..='9' => false,
682-
'A'..='Z' => false,
683-
'a'..='z' => false,
684-
'_' => false,
685-
'$' => false,
686-
_ => true,
687-
}
680+
!matches!(ch, '0'..='9' | 'A'..='Z' | 'a'..='z' | '_' | '$')
688681
}
689682

690683
fn is_reserved_word(word: &str) -> bool {
@@ -743,11 +736,11 @@ fn is_reserved_word(word: &str) -> bool {
743736
}
744737

745738
fn is_legal_bareword(word: &str) -> bool {
746-
if word.len() == 0 {
739+
if word.is_empty() {
747740
return false;
748741
}
749-
let ch = word.chars().nth(0).unwrap();
750-
!((ch >= '0' && ch <= '9') || word.contains(bad_identifier_char) || is_reserved_word(word))
742+
let ch = word.chars().next().unwrap();
743+
!(ch.is_ascii_digit() || word.contains(bad_identifier_char) || is_reserved_word(word))
751744
}
752745

753746
#[cfg(test)]

src/relax.rs

+29-27
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ impl Default for Relax {
7070
impl Relax {
7171
/// Creates a strict json parser.
7272
pub fn json() -> Self {
73-
Relax {
74-
inner: Default::default(),
73+
Self {
7574
comma_trailing: false,
7675
comma_optional: false,
7776
number_bin: false,
@@ -87,31 +86,34 @@ impl Relax {
8786
comment_slash: false,
8887
comment_hash: false,
8988
comment_block: false,
89+
..Self::default()
9090
}
9191
}
9292

9393
/// Creates a json5 parser.
9494
pub fn json5() -> Self {
95-
let mut r = Self::default();
96-
r.comma_optional = false;
97-
r.string_unquoted = false;
98-
r.string_hjson_multiline = false;
99-
r.comment_hash = false;
100-
r.number_bin = false;
101-
r.number_oct = false;
102-
r
95+
Self {
96+
comma_optional: false,
97+
string_unquoted: false,
98+
string_hjson_multiline: false,
99+
comment_hash: false,
100+
number_bin: false,
101+
number_oct: false,
102+
..Self::default()
103+
}
103104
}
104105

105106
/// Creates a hjson parser.
106107
pub fn hjson() -> Self {
107-
let mut r = Self::default();
108-
r.string_json5_multiline = false;
109-
r.number_bin = false;
110-
r.number_hex = false;
111-
r.number_oct = false;
112-
r.number_plus = false;
113-
r.number_lax_dec_point = false;
114-
r
108+
Self {
109+
string_json5_multiline: false,
110+
number_bin: false,
111+
number_hex: false,
112+
number_oct: false,
113+
number_plus: false,
114+
number_lax_dec_point: false,
115+
..Self::default()
116+
}
115117
}
116118

117119
/// Parses a string into a `Document`.
@@ -217,7 +219,7 @@ impl Relax {
217219
"hexadecimal literal",
218220
pair.as_span().start_pos(),
219221
)?;
220-
return Self::from_str_radix(text, 16);
222+
Self::from_str_radix(text, 16)
221223
} else if t.starts_with("0b") || t.starts_with("0B") {
222224
// Binary integer.
223225
Self::syntax_error(
@@ -389,10 +391,10 @@ impl Relax {
389391
pair.as_span().start_pos(),
390392
)?;
391393
let c = c.strip_suffix("*/").unwrap().trim_end();
392-
let lines = c.split("\n").map(str::trim).collect::<Vec<_>>();
394+
let lines = c.split('\n').map(str::trim).collect::<Vec<_>>();
393395
let lines = Self::strip_leading_prefix(&lines, '*');
394396
let lines = Self::strip_leading_prefix(&lines, ' ');
395-
let start = if lines.get(0).map(|s| s.is_empty()) == Some(true) {
397+
let start = if lines.first().map(|s| s.is_empty()) == Some(true) {
396398
1
397399
} else {
398400
0
@@ -405,7 +407,7 @@ impl Relax {
405407
"slash comment",
406408
pair.as_span().start_pos(),
407409
)?;
408-
let lines = comment.split("\n").map(str::trim).collect::<Vec<_>>();
410+
let lines = comment.split('\n').map(str::trim).collect::<Vec<_>>();
409411
let lines = Self::strip_leading_prefix(&lines, '/');
410412
let lines = Self::strip_leading_prefix(&lines, ' ');
411413
let end = lines.len()
@@ -416,13 +418,13 @@ impl Relax {
416418
};
417419
let c = lines[..end].join("\n");
418420
Ok(Document::Comment(c, CommentFormat::SlashSlash))
419-
} else if comment.starts_with("#") {
421+
} else if comment.starts_with('#') {
420422
Self::syntax_error(
421423
!self.comment_hash,
422424
"hash comment",
423425
pair.as_span().start_pos(),
424426
)?;
425-
let lines = comment.split("\n").map(str::trim).collect::<Vec<_>>();
427+
let lines = comment.split('\n').map(str::trim).collect::<Vec<_>>();
426428
let lines = Self::strip_leading_prefix(&lines, '#');
427429
let lines = Self::strip_leading_prefix(&lines, ' ');
428430
let end = lines.len()
@@ -463,7 +465,7 @@ impl Relax {
463465
Ok(Document::String(value.join("\n"), StrFormat::Multiline))
464466
} else if s.starts_with('\'') || s.starts_with('"') {
465467
Self::syntax_error(
466-
!self.string_single_quote && s.starts_with("'"),
468+
!self.string_single_quote && s.starts_with('\''),
467469
"single quote",
468470
pair.as_span().start_pos(),
469471
)?;
@@ -595,7 +597,7 @@ impl Relax {
595597
}
596598
}
597599

598-
_ => Err(Error::Unknown(format!("{:?}", pair)).into()),
600+
_ => Err(Error::Unknown(format!("{:?}", pair))),
599601
}
600602
}
601603
}
@@ -723,7 +725,7 @@ mod tests {
723725
}
724726
}
725727

726-
fn kv_extract<'a>(kv: Option<&'a Document>) -> Result<(&'a str, &'a str)> {
728+
fn kv_extract(kv: Option<&Document>) -> Result<(&str, &str)> {
727729
if let Some((Document::String(k, _), Document::String(v, _))) =
728730
kv.map(Document::as_kv).transpose()?
729731
{

src/ser.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<'a> AnnotatedSerializer<'a> {
6060
}
6161

6262
fn annotate(&self, variant: Option<&str>, field: &MemberId) -> Option<Self> {
63-
match self.annotator.map(|a| a.format(variant, field)).flatten() {
63+
match self.annotator.and_then(|a| a.format(variant, field)) {
6464
Some(Format::Block) => Some(self.with_strformat(StrFormat::Multiline)),
6565
Some(Format::Binary) => Some(self.with_base(Base::Bin)),
6666
Some(Format::Decimal) => Some(self.with_base(Base::Dec)),
@@ -76,8 +76,7 @@ impl<'a> AnnotatedSerializer<'a> {
7676

7777
fn comment(&self, variant: Option<&str>, field: &MemberId) -> Option<Document> {
7878
self.annotator
79-
.map(|a| a.comment(variant, field))
80-
.flatten()
79+
.and_then(|a| a.comment(variant, field))
8180
.map(|c| Document::Comment(c, CommentFormat::Standard))
8281
}
8382

@@ -152,7 +151,7 @@ impl<'s, 'a> ser::Serializer for &'s mut AnnotatedSerializer<'a> {
152151
}
153152

154153
fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
155-
Ok(Document::Float(v as f64))
154+
Ok(Document::Float(v))
156155
}
157156

158157
fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {

src/yaml.rs

+11-19
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,7 @@ impl YamlEmitter {
189189
if let Document::Fragment(frags) = value {
190190
let mut val_done = false;
191191
let mut it = frags.iter().peekable();
192-
loop {
193-
let node = if let Some(n) = it.next() {
194-
n
195-
} else {
196-
break;
197-
};
192+
while let Some(node) = it.next() {
198193
let next = it.peek();
199194
if let Some((c, f)) = node.comment() {
200195
if val_done {
@@ -239,12 +234,7 @@ impl YamlEmitter {
239234
let mut key_done = false;
240235
let mut val_done = false;
241236
let mut it = nodes.iter().peekable();
242-
loop {
243-
let node = if let Some(n) = it.next() {
244-
n
245-
} else {
246-
break;
247-
};
237+
while let Some(node) = it.next() {
248238
let next = it.peek();
249239
if let Some((c, f)) = node.comment() {
250240
if val_done {
@@ -515,13 +505,16 @@ fn need_quotes(string: &str) -> bool {
515505
string.starts_with(' ') || string.ends_with(' ')
516506
}
517507

518-
string == ""
508+
string.is_empty()
519509
|| need_quotes_spaces(string)
520-
|| string.starts_with(|character: char| match character {
521-
'&' | '*' | '?' | '|' | '-' | '<' | '>' | '=' | '!' | '%' | '@' => true,
522-
_ => false,
510+
|| string.starts_with(|character: char| {
511+
matches!(
512+
character,
513+
'&' | '*' | '?' | '|' | '-' | '<' | '>' | '=' | '!' | '%' | '@'
514+
)
523515
})
524-
|| string.contains(|character: char| match character {
516+
|| string.contains(|character: char| {
517+
matches!(character,
525518
':'
526519
| '{'
527520
| '}'
@@ -538,8 +531,7 @@ fn need_quotes(string: &str) -> bool {
538531
| '\n'
539532
| '\r'
540533
| '\x0e'..='\x1a'
541-
| '\x1c'..='\x1f' => true,
542-
_ => false,
534+
| '\x1c'..='\x1f')
543535
})
544536
|| [
545537
// http://yaml.org/type/bool.html

tests/test_erased.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ struct Hello {
1010
}
1111

1212
fn hello() -> Box<dyn Annotate> {
13-
let hello = Box::new(Hello {
13+
Box::new(Hello {
1414
message: "Hello World!".into(),
15-
});
16-
hello
15+
})
1716
}
1817

1918
#[test]

0 commit comments

Comments
 (0)