Skip to content

Commit eccfacb

Browse files
committed
Apply clippy lints for more idiomatic code
1 parent 4aed9f4 commit eccfacb

File tree

11 files changed

+150
-157
lines changed

11 files changed

+150
-157
lines changed

color/tests.rs

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

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

1010
fn almost_equals(a: &Value, b: &Value) -> bool {
1111
match (a, b) {
12-
(&Value::Number(ref a), &Value::Number(ref b)) => {
12+
(Value::Number(a), Value::Number(b)) => {
1313
let a = a.as_f64().unwrap();
1414
let b = b.as_f64().unwrap();
1515
(a - b).abs() <= a.abs() * 1e-6
1616
}
1717

1818
(&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))
19+
(Value::String(a), Value::String(b)) => a == b,
20+
(Value::Array(a), Value::Array(b)) => {
21+
a.len() == b.len() && a.iter().zip(b.iter()).all(|(a, b)| almost_equals(a, b))
2522
}
2623
(&Value::Object(_), &Value::Object(_)) => panic!("Not implemented"),
2724
(&Value::Null, &Value::Null) => true,
@@ -43,8 +40,7 @@ fn assert_json_eq(results: Value, expected: Value, message: &str) {
4340
}
4441
}
4542

46-
47-
fn run_raw_json_tests<F: Fn(Value, Value) -> ()>(json_data: &str, run: F) {
43+
fn run_raw_json_tests<F: Fn(Value, Value)>(json_data: &str, run: F) {
4844
let items = match serde_json::from_str(json_data) {
4945
Ok(Value::Array(items)) => items,
5046
other => panic!("Invalid JSON: {:?}", other),
@@ -92,11 +88,14 @@ fn color3() {
9288
#[cfg_attr(all(miri, feature = "skip_long_tests"), ignore)]
9389
#[test]
9490
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-
})
91+
run_color_tests(
92+
include_str!("../src/css-parsing-tests/color3_hsl.json"),
93+
|c| {
94+
c.ok()
95+
.map(|v| v.to_css_string().to_json())
96+
.unwrap_or(Value::Null)
97+
},
98+
)
10099
}
101100

102101
/// color3_keywords.json is different: R, G and B are in 0..255 rather than 0..1
@@ -115,11 +114,14 @@ fn color3_keywords() {
115114
#[cfg_attr(all(miri, feature = "skip_long_tests"), ignore)]
116115
#[test]
117116
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-
})
117+
run_color_tests(
118+
include_str!("../src/css-parsing-tests/color4_hwb.json"),
119+
|c| {
120+
c.ok()
121+
.map(|v| v.to_css_string().to_json())
122+
.unwrap_or(Value::Null)
123+
},
124+
)
123125
}
124126

125127
#[cfg_attr(all(miri, feature = "skip_long_tests"), ignore)]
@@ -355,7 +357,7 @@ fn generic_parser() {
355357
];
356358

357359
for (input, expected) in TESTS {
358-
let mut input = ParserInput::new(*input);
360+
let mut input = ParserInput::new(input);
359361
let mut input = Parser::new(&mut input);
360362

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

macros/lib.rs

Lines changed: 10 additions & 10 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 8 additions & 8 deletions
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

Lines changed: 5 additions & 6 deletions
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),

src/rules_and_declarations.rs

Lines changed: 8 additions & 8 deletions
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)