Skip to content

Commit e89af28

Browse files
chipnertkjemilio
authored andcommitted
Apply clippy lints for more idiomatic code
1 parent c3314d1 commit e89af28

11 files changed

+157
-165
lines changed

color/tests.rs

+24-23
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,21 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
use super::*;
6-
use crate::{ColorParser, PredefinedColorSpace, Color, RgbaLegacy};
7-
use cssparser::{Parser, ParserInput};
8-
use serde_json::{self, json, Value};
6+
use cssparser::ParserInput;
7+
use serde_json::{json, Value};
98

109
fn almost_equals(a: &Value, b: &Value) -> bool {
1110
match (a, b) {
12-
(&Value::Number(ref a), &Value::Number(ref b)) => {
11+
(Value::Number(a), Value::Number(b)) => {
1312
let a = a.as_f64().unwrap();
1413
let b = b.as_f64().unwrap();
1514
(a - b).abs() <= a.abs() * 1e-6
1615
}
1716

1817
(&Value::Bool(a), &Value::Bool(b)) => a == b,
19-
(&Value::String(ref a), &Value::String(ref b)) => a == b,
20-
(&Value::Array(ref a), &Value::Array(ref b)) => {
21-
a.len() == b.len()
22-
&& a.iter()
23-
.zip(b.iter())
24-
.all(|(ref a, ref b)| almost_equals(*a, *b))
18+
(Value::String(a), Value::String(b)) => a == b,
19+
(Value::Array(a), Value::Array(b)) => {
20+
a.len() == b.len() && a.iter().zip(b.iter()).all(|(a, b)| almost_equals(a, b))
2521
}
2622
(&Value::Object(_), &Value::Object(_)) => panic!("Not implemented"),
2723
(&Value::Null, &Value::Null) => true,
@@ -43,8 +39,7 @@ fn assert_json_eq(results: Value, expected: Value, message: &str) {
4339
}
4440
}
4541

46-
47-
fn run_raw_json_tests<F: Fn(Value, Value) -> ()>(json_data: &str, run: F) {
42+
fn run_raw_json_tests<F: Fn(Value, Value)>(json_data: &str, run: F) {
4843
let items = match serde_json::from_str(json_data) {
4944
Ok(Value::Array(items)) => items,
5045
other => panic!("Invalid JSON: {:?}", other),
@@ -92,11 +87,14 @@ fn color3() {
9287
#[cfg_attr(all(miri, feature = "skip_long_tests"), ignore)]
9388
#[test]
9489
fn color3_hsl() {
95-
run_color_tests(include_str!("../src/css-parsing-tests/color3_hsl.json"), |c| {
96-
c.ok()
97-
.map(|v| v.to_css_string().to_json())
98-
.unwrap_or(Value::Null)
99-
})
90+
run_color_tests(
91+
include_str!("../src/css-parsing-tests/color3_hsl.json"),
92+
|c| {
93+
c.ok()
94+
.map(|v| v.to_css_string().to_json())
95+
.unwrap_or(Value::Null)
96+
},
97+
)
10098
}
10199

102100
/// color3_keywords.json is different: R, G and B are in 0..255 rather than 0..1
@@ -115,11 +113,14 @@ fn color3_keywords() {
115113
#[cfg_attr(all(miri, feature = "skip_long_tests"), ignore)]
116114
#[test]
117115
fn color4_hwb() {
118-
run_color_tests(include_str!("../src/css-parsing-tests/color4_hwb.json"), |c| {
119-
c.ok()
120-
.map(|v| v.to_css_string().to_json())
121-
.unwrap_or(Value::Null)
122-
})
116+
run_color_tests(
117+
include_str!("../src/css-parsing-tests/color4_hwb.json"),
118+
|c| {
119+
c.ok()
120+
.map(|v| v.to_css_string().to_json())
121+
.unwrap_or(Value::Null)
122+
},
123+
)
123124
}
124125

125126
#[cfg_attr(all(miri, feature = "skip_long_tests"), ignore)]
@@ -355,7 +356,7 @@ fn generic_parser() {
355356
];
356357

357358
for (input, expected) in TESTS {
358-
let mut input = ParserInput::new(*input);
359+
let mut input = ParserInput::new(input);
359360
let mut input = Parser::new(&mut input);
360361

361362
let actual: OutputType = parse_color_with(&TestColorParser, &mut input).unwrap();

macros/lib.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ fn get_byte_from_lit(lit: &syn::Lit) -> u8 {
4848

4949
fn get_byte_from_expr_lit(expr: &syn::Expr) -> u8 {
5050
match *expr {
51-
syn::Expr::Lit(syn::ExprLit { ref lit, .. }) => {
52-
get_byte_from_lit(lit)
53-
}
51+
syn::Expr::Lit(syn::ExprLit { ref lit, .. }) => get_byte_from_lit(lit),
5452
_ => unreachable!(),
5553
}
5654
}
@@ -63,15 +61,17 @@ fn parse_pat_to_table<'a>(
6361
table: &mut [u8; 256],
6462
) {
6563
match pat {
66-
&syn::Pat::Lit(syn::PatLit { ref lit, .. }) => {
64+
syn::Pat::Lit(syn::PatLit { ref lit, .. }) => {
6765
let value = get_byte_from_lit(lit);
6866
if table[value as usize] == 0 {
6967
table[value as usize] = case_id;
7068
}
7169
}
72-
&syn::Pat::Range(syn::PatRange { ref start, ref end, .. }) => {
73-
let lo = get_byte_from_expr_lit(&start.as_ref().unwrap());
74-
let hi = get_byte_from_expr_lit(&end.as_ref().unwrap());
70+
syn::Pat::Range(syn::PatRange {
71+
ref start, ref end, ..
72+
}) => {
73+
let lo = get_byte_from_expr_lit(start.as_ref().unwrap());
74+
let hi = get_byte_from_expr_lit(end.as_ref().unwrap());
7575
for value in lo..hi {
7676
if table[value as usize] == 0 {
7777
table[value as usize] = case_id;
@@ -81,14 +81,14 @@ fn parse_pat_to_table<'a>(
8181
table[hi as usize] = case_id;
8282
}
8383
}
84-
&syn::Pat::Wild(_) => {
84+
syn::Pat::Wild(_) => {
8585
for byte in table.iter_mut() {
8686
if *byte == 0 {
8787
*byte = case_id;
8888
}
8989
}
9090
}
91-
&syn::Pat::Ident(syn::PatIdent { ref ident, .. }) => {
91+
syn::Pat::Ident(syn::PatIdent { ref ident, .. }) => {
9292
assert_eq!(*wildcard, None);
9393
*wildcard = Some(ident);
9494
for byte in table.iter_mut() {
@@ -97,7 +97,7 @@ fn parse_pat_to_table<'a>(
9797
}
9898
}
9999
}
100-
&syn::Pat::Or(syn::PatOr { ref cases, .. }) => {
100+
syn::Pat::Or(syn::PatOr { ref cases, .. }) => {
101101
for case in cases {
102102
parse_pat_to_table(case, case_id, wildcard, table);
103103
}

src/cow_rc_str.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use std::borrow::{Borrow, Cow};
66
use std::rc::Rc;
7-
use std::{cmp, fmt, hash, marker, mem, ops, slice, str, ptr};
7+
use std::{cmp, fmt, hash, marker, mem, ops, ptr, slice, str};
88

99
/// A string that is either shared (heap-allocated and reference-counted) or borrowed.
1010
///
@@ -23,9 +23,9 @@ pub struct CowRcStr<'a> {
2323
phantom: marker::PhantomData<Result<&'a str, Rc<String>>>,
2424
}
2525

26-
fn _static_assert_same_size<'a>() {
26+
fn _static_assert_same_size() {
2727
// "Instantiate" the generic function without calling it.
28-
let _ = mem::transmute::<CowRcStr<'a>, Option<CowRcStr<'a>>>;
28+
let _ = mem::transmute::<CowRcStr<'_>, Option<CowRcStr<'_>>>;
2929
}
3030

3131
impl<'a> From<Cow<'a, str>> for CowRcStr<'a> {

src/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub fn _cssparser_internal_to_lowercase<'a>(
182182
let input_bytes =
183183
unsafe { &*(input.as_bytes() as *const [u8] as *const [MaybeUninit<u8>]) };
184184

185-
buffer.copy_from_slice(&*input_bytes);
185+
buffer.copy_from_slice(input_bytes);
186186

187187
// Same as above re layout, plus these bytes have been initialized:
188188
let buffer = unsafe { &mut *(buffer as *mut [MaybeUninit<u8>] as *mut [u8]) };
@@ -195,7 +195,7 @@ pub fn _cssparser_internal_to_lowercase<'a>(
195195
}
196196

197197
Some(
198-
match input.bytes().position(|byte| matches!(byte, b'A'..=b'Z')) {
198+
match input.bytes().position(|byte| byte.is_ascii_uppercase()) {
199199
Some(first_uppercase) => make_ascii_lowercase(buffer, input, first_uppercase),
200200
// common case: input is already lower-case
201201
None => input,

src/nth.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use super::{BasicParseError, Parser, ParserInput, Token};
88
/// The input is typically the arguments of a function,
99
/// in which case the caller needs to check if the arguments’ parser is exhausted.
1010
/// Return `Ok((A, B))`, or `Err(())` for a syntax error.
11-
pub fn parse_nth<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(i32, i32), BasicParseError<'i>> {
11+
pub fn parse_nth<'i>(input: &mut Parser<'i, '_>) -> Result<(i32, i32), BasicParseError<'i>> {
1212
match *input.next()? {
1313
Token::Number {
1414
int_value: Some(b), ..
@@ -22,7 +22,7 @@ pub fn parse_nth<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(i32, i32), Basic
2222
unit,
2323
"n" => Ok(parse_b(input, a)?),
2424
"n-" => Ok(parse_signless_b(input, a, -1)?),
25-
_ => match parse_n_dash_digits(&*unit) {
25+
_ => match parse_n_dash_digits(unit) {
2626
Ok(b) => Ok((a, b)),
2727
Err(()) => {
2828
let unit = unit.clone();
@@ -40,8 +40,8 @@ pub fn parse_nth<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(i32, i32), Basic
4040
"n-" => Ok(parse_signless_b(input, 1, -1)?),
4141
"-n-" => Ok(parse_signless_b(input, -1, -1)?),
4242
_ => {
43-
let (slice, a) = if value.starts_with("-") {
44-
(&value[1..], -1)
43+
let (slice, a) = if let Some(stripped) = value.strip_prefix('-') {
44+
(stripped, -1)
4545
} else {
4646
(&**value, 1)
4747
};
@@ -81,7 +81,7 @@ pub fn parse_nth<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(i32, i32), Basic
8181
}
8282
}
8383

84-
fn parse_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32) -> Result<(i32, i32), BasicParseError<'i>> {
84+
fn parse_b<'i>(input: &mut Parser<'i, '_>, a: i32) -> Result<(i32, i32), BasicParseError<'i>> {
8585
let start = input.state();
8686
match input.next() {
8787
Ok(&Token::Delim('+')) => parse_signless_b(input, a, 1),
@@ -98,8 +98,8 @@ fn parse_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32) -> Result<(i32, i32), Bas
9898
}
9999
}
100100

101-
fn parse_signless_b<'i, 't>(
102-
input: &mut Parser<'i, 't>,
101+
fn parse_signless_b<'i>(
102+
input: &mut Parser<'i, '_>,
103103
a: i32,
104104
b_sign: i32,
105105
) -> Result<(i32, i32), BasicParseError<'i>> {
@@ -118,7 +118,7 @@ fn parse_n_dash_digits(string: &str) -> Result<i32, ()> {
118118
let bytes = string.as_bytes();
119119
if bytes.len() >= 3
120120
&& bytes[..2].eq_ignore_ascii_case(b"n-")
121-
&& bytes[2..].iter().all(|&c| matches!(c, b'0'..=b'9'))
121+
&& bytes[2..].iter().all(|&c| c.is_ascii_digit())
122122
{
123123
Ok(parse_number_saturate(&string[1..]).unwrap()) // Include the minus sign
124124
} else {

src/parser.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl<'i, T> From<BasicParseError<'i>> for ParseError<'i, T> {
116116
impl SourceLocation {
117117
/// Create a new BasicParseError at this location for an unexpected token
118118
#[inline]
119-
pub fn new_basic_unexpected_token_error<'i>(self, token: Token<'i>) -> BasicParseError<'i> {
119+
pub fn new_basic_unexpected_token_error(self, token: Token<'_>) -> BasicParseError<'_> {
120120
BasicParseError {
121121
kind: BasicParseErrorKind::UnexpectedToken(token),
122122
location: self,
@@ -125,7 +125,7 @@ impl SourceLocation {
125125

126126
/// Create a new ParseError at this location for an unexpected token
127127
#[inline]
128-
pub fn new_unexpected_token_error<'i, E>(self, token: Token<'i>) -> ParseError<'i, E> {
128+
pub fn new_unexpected_token_error<E>(self, token: Token<'_>) -> ParseError<'_, E> {
129129
ParseError {
130130
kind: ParseErrorKind::Basic(BasicParseErrorKind::UnexpectedToken(token)),
131131
location: self,
@@ -652,9 +652,8 @@ impl<'i: 't, 't> Parser<'i, 't> {
652652
let token = if using_cached_token {
653653
let cached_token = self.input.cached_token.as_ref().unwrap();
654654
self.input.tokenizer.reset(&cached_token.end_state);
655-
match cached_token.token {
656-
Token::Function(ref name) => self.input.tokenizer.see_function(name),
657-
_ => {}
655+
if let Token::Function(ref name) = cached_token.token {
656+
self.input.tokenizer.see_function(name)
658657
}
659658
&cached_token.token
660659
} else {
@@ -748,7 +747,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
748747
match self.parse_until_before(Delimiter::Comma, &mut parse_one) {
749748
Ok(v) => values.push(v),
750749
Err(e) if !ignore_errors => return Err(e),
751-
Err(_) => {},
750+
Err(_) => {}
752751
}
753752
match self.next() {
754753
Err(_) => return Ok(values),
@@ -835,7 +834,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
835834
/// expect_ident, but clone the CowRcStr
836835
#[inline]
837836
pub fn expect_ident_cloned(&mut self) -> Result<CowRcStr<'i>, BasicParseError<'i>> {
838-
self.expect_ident().map(|s| s.clone())
837+
self.expect_ident().cloned()
839838
}
840839

841840
/// Parse a <ident-token> whose unescaped value is an ASCII-insensitive match for the given value.
@@ -860,7 +859,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
860859
/// expect_string, but clone the CowRcStr
861860
#[inline]
862861
pub fn expect_string_cloned(&mut self) -> Result<CowRcStr<'i>, BasicParseError<'i>> {
863-
self.expect_string().map(|s| s.clone())
862+
self.expect_string().cloned()
864863
}
865864

866865
/// Parse either a <ident-token> or a <string-token>, and return the unescaped value.
@@ -879,7 +878,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
879878
Token::UnquotedUrl(ref value) => Ok(value.clone()),
880879
Token::Function(ref name) if name.eq_ignore_ascii_case("url") => {
881880
self.parse_nested_block(|input| {
882-
input.expect_string().map_err(Into::into).map(|s| s.clone())
881+
input.expect_string().map_err(Into::into).cloned()
883882
})
884883
.map_err(ParseError::<()>::basic)
885884
}
@@ -894,7 +893,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
894893
Token::QuotedString(ref value) => Ok(value.clone()),
895894
Token::Function(ref name) if name.eq_ignore_ascii_case("url") => {
896895
self.parse_nested_block(|input| {
897-
input.expect_string().map_err(Into::into).map(|s| s.clone())
896+
input.expect_string().map_err(Into::into).cloned()
898897
})
899898
.map_err(ParseError::<()>::basic)
900899
}

src/rules_and_declarations.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::parser::{parse_nested_block, parse_until_after, ParseUntilErrorBehavi
1414
///
1515
/// Typical usage is `input.try_parse(parse_important).is_ok()`
1616
/// at the end of a `DeclarationParser::parse_value` implementation.
17-
pub fn parse_important<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(), BasicParseError<'i>> {
17+
pub fn parse_important<'i>(input: &mut Parser<'i, '_>) -> Result<(), BasicParseError<'i>> {
1818
input.expect_delim('!')?;
1919
input.expect_ident_matching("important")
2020
}
@@ -253,10 +253,10 @@ where
253253
self.input.skip_whitespace();
254254
let start = self.input.state();
255255
match self.input.next_including_whitespace_and_comments().ok()? {
256-
Token::CloseCurlyBracket |
257-
Token::WhiteSpace(..) |
258-
Token::Semicolon |
259-
Token::Comment(..) => continue,
256+
Token::CloseCurlyBracket
257+
| Token::WhiteSpace(..)
258+
| Token::Semicolon
259+
| Token::Comment(..) => continue,
260260
Token::AtKeyword(ref name) => {
261261
let name = name.clone();
262262
return Some(parse_at_rule(&start, name, self.input, &mut *self.parser));
@@ -294,7 +294,7 @@ where
294294
&mut *self.parser,
295295
Delimiter::Semicolon | Delimiter::CurlyBracketBlock,
296296
) {
297-
return Some(Ok(qual))
297+
return Some(Ok(qual));
298298
}
299299
}
300300

@@ -367,7 +367,7 @@ where
367367
let start = self.input.state();
368368
let at_keyword = match self.input.next_byte()? {
369369
b'@' => match self.input.next_including_whitespace_and_comments() {
370-
Ok(&Token::AtKeyword(ref name)) => Some(name.clone()),
370+
Ok(Token::AtKeyword(name)) => Some(name.clone()),
371371
_ => {
372372
self.input.reset(&start);
373373
None
@@ -503,5 +503,5 @@ where
503503
input.expect_curly_bracket_block()?;
504504
// Do this here so that we consume the `{` even if the prelude is `Err`.
505505
let prelude = prelude?;
506-
parse_nested_block(input, |input| parser.parse_block(prelude, &start, input))
506+
parse_nested_block(input, |input| parser.parse_block(prelude, start, input))
507507
}

0 commit comments

Comments
 (0)