From 3805eaab2bf10b9804b72dfb9b52de60c04b0583 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Tue, 21 Apr 2020 18:22:26 -0400 Subject: [PATCH] clippy --- src/args.rs | 2 +- src/atrule/mixin.rs | 2 +- src/builtin/list.rs | 2 +- src/builtin/math.rs | 2 +- src/error.rs | 4 +++ src/lexer.rs | 2 +- src/lib.rs | 3 +- src/utils/chars.rs | 2 +- src/utils/number.rs | 2 +- src/utils/read_until.rs | 8 ++--- src/utils/strings.rs | 4 +-- src/value/mod.rs | 64 ++++++++++++++++---------------------- src/value/ops.rs | 2 +- src/value/parse.rs | 13 ++++---- src/value/sass_function.rs | 2 +- 15 files changed, 54 insertions(+), 60 deletions(-) diff --git a/src/args.rs b/src/args.rs index 7e1fa83..c08c8f1 100644 --- a/src/args.rs +++ b/src/args.rs @@ -159,7 +159,7 @@ impl CallArgs { ) } - pub fn span(&self) -> Span { + pub const fn span(&self) -> Span { self.1 } diff --git a/src/atrule/mixin.rs b/src/atrule/mixin.rs index b018270..346e652 100644 --- a/src/atrule/mixin.rs +++ b/src/atrule/mixin.rs @@ -229,7 +229,7 @@ pub(crate) fn eat_include>( Vec::new() }; - let mixin = scope.get_mixin(name)?.clone(); + let mixin = scope.get_mixin(name)?; let rules = mixin .args(args, scope, super_selector)? diff --git a/src/builtin/list.rs b/src/builtin/list.rs index a236748..85ec2ce 100644 --- a/src/builtin/list.rs +++ b/src/builtin/list.rs @@ -308,7 +308,7 @@ pub(crate) fn register(f: &mut HashMap) { }) .collect::>>>()?; - let len = lists.iter().map(|l| l.len()).min().unwrap_or(0); + let len = lists.iter().map(Vec::len).min().unwrap_or(0); if len == 0 { return Ok(Value::List( diff --git a/src/builtin/math.rs b/src/builtin/math.rs index 52ee1f2..a220f41 100644 --- a/src/builtin/math.rs +++ b/src/builtin/math.rs @@ -186,7 +186,7 @@ pub(crate) fn register(f: &mut HashMap) { Some(n) => n, None => { return Err(( - format!("max must be in range 0 < max ≤ 2^32, was {}", limit), + format!("max must be in range 0 < max \u{2264} 2^32, was {}", limit), args.span(), ) .into()) diff --git a/src/error.rs b/src/error.rs index 54bd174..027003e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -46,6 +46,7 @@ impl Display for SassError { // TODO: trim whitespace from start of line shown in error // TODO: color errors // TODO: integrate with codemap-diagnostics + #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let (message, loc) = match &self.kind { SassErrorKind::ParseError { message, loc } => (message, loc), @@ -75,6 +76,7 @@ impl Display for SassError { } impl From for SassError { + #[inline] fn from(error: io::Error) -> Self { SassError { kind: SassErrorKind::IoError(error), @@ -91,6 +93,7 @@ impl From for SassError { } impl From for SassError { + #[inline] fn from(error: FromUtf8Error) -> Self { SassError { kind: SassErrorKind::FromUtf8Error(format!( @@ -120,6 +123,7 @@ impl From<(String, Span)> for SassError { } impl Error for SassError { + #[inline] fn description(&self) -> &'static str { "SASS parsing error" } diff --git a/src/lexer.rs b/src/lexer.rs index bfd3a3c..778fedf 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -44,7 +44,7 @@ impl<'a> Iterator for Lexer<'a> { impl<'a> Lexer<'a> { pub fn new(file: &'a Arc) -> Lexer<'a> { Lexer { - buf: file.source().clone().chars().peekable(), + buf: file.source().chars().peekable(), pos: 0, file, } diff --git a/src/lib.rs b/src/lib.rs index 6d06e7e..388f79e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -77,6 +77,7 @@ grass input.scss clippy::indexing_slicing, clippy::match_same_arms, clippy::or_fun_call, + clippy::redundant_pub_crate, )] #![cfg_attr(feature = "nightly", feature(track_caller))] use std::fs; @@ -136,7 +137,7 @@ pub(crate) enum Stmt { } impl Stmt { - fn span(self, span: Span) -> Spanned { + const fn span(self, span: Span) -> Spanned { Spanned { node: self, span } } } diff --git a/src/utils/chars.rs b/src/utils/chars.rs index 7b6a0c2..08d6a67 100644 --- a/src/utils/chars.rs +++ b/src/utils/chars.rs @@ -14,7 +14,7 @@ pub(crate) fn read_until_char>( while let Some(tok) = toks.next() { match tok.kind { '"' | '\'' => { - v.push(tok.clone()); + v.push(tok); v.extend(read_until_closing_quote(toks, tok.kind)); continue; } diff --git a/src/utils/number.rs b/src/utils/number.rs index 2879e57..68e03de 100644 --- a/src/utils/number.rs +++ b/src/utils/number.rs @@ -31,7 +31,7 @@ pub(crate) fn eat_number>( let mut dec = String::new(); - let next_tok = toks.peek().unwrap().clone(); + let next_tok = *toks.peek().unwrap(); if next_tok.kind == '.' { toks.next(); diff --git a/src/utils/read_until.rs b/src/utils/read_until.rs index 94e5c1b..5216d00 100644 --- a/src/utils/read_until.rs +++ b/src/utils/read_until.rs @@ -128,7 +128,7 @@ pub(crate) fn read_until_semicolon_or_closing_curly_brace { let quote = toks.next().unwrap(); - t.push(quote.clone()); + t.push(quote); t.extend(read_until_closing_quote(toks, quote.kind)); } '{' => { @@ -174,7 +174,7 @@ pub(crate) fn read_until_semicolon_or_open_or_closing_curly_brace { let quote = toks.next().unwrap(); - t.push(quote.clone()); + t.push(quote); t.extend(read_until_closing_quote(toks, quote.kind)); } '#' => { @@ -238,7 +238,7 @@ pub(crate) fn read_until_closing_paren>( } '(' => scope += 1, '"' | '\'' => { - v.push(tok.clone()); + v.push(tok); v.extend(read_until_closing_quote(toks, tok.kind)); continue; } @@ -266,7 +266,7 @@ pub(crate) fn read_until_closing_square_brace>( } '[' => scope += 1, '"' | '\'' => { - v.push(tok.clone()); + v.push(tok); v.extend(read_until_closing_quote(toks, tok.kind)); continue; } diff --git a/src/utils/strings.rs b/src/utils/strings.rs index 9737551..46651f8 100644 --- a/src/utils/strings.rs +++ b/src/utils/strings.rs @@ -23,7 +23,7 @@ fn ident_body_no_interpolation>( if unit && tok.kind == '-' { // Disallow `-` followed by a dot or a digit digit in units. let second = match toks.peek_forward(1) { - Some(v) => v.clone(), + Some(v) => *v, None => break, }; @@ -294,7 +294,7 @@ pub(crate) fn parse_quoted_string>( toks.next(); } - if value == 0 || (value >= 0xD800 && value <= 0xDFFF) || value >= 0x10FFFF { + if value == 0 || (value >= 0xD800 && value <= 0xDFFF) || value >= 0x0010_FFFF { s.push('\u{FFFD}'); } else { s.push(std::char::from_u32(value).unwrap()); diff --git a/src/value/mod.rs b/src/value/mod.rs index eb27267..cdc0d05 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -115,14 +115,14 @@ impl Value { Self::BinaryOp(..) | Self::Paren(..) | Self::UnaryOp(..) => { self.clone().eval(span)?.is_null(span) } - Self::List(v, ..) => Ok(v.into_iter().all(|f| f.is_null(span).unwrap())), + Self::List(v, ..) => Ok(v.iter().all(|f| f.is_null(span).unwrap())), _ => Ok(false), } } pub fn to_css_string(&self, span: Span) -> SassResult { Ok(match self { - Self::Important => format!("!important"), + Self::Important => "!important".to_string(), Self::Dimension(num, unit) => match unit { Unit::Mul(..) => { return Err((format!("{}{} isn't a valid CSS value.", num, unit), span).into()); @@ -138,14 +138,12 @@ impl Value { } Self::Function(func) => format!("get-function(\"{}\")", func.name()), Self::List(vals, sep, brackets) => match brackets { - Brackets::None => format!( - "{}", - vals.iter() - .filter(|x| !x.is_null(span).unwrap()) - .map(|x| x.to_css_string(span)) - .collect::>>()? - .join(sep.as_str()), - ), + Brackets::None => vals + .iter() + .filter(|x| !x.is_null(span).unwrap()) + .map(|x| x.to_css_string(span)) + .collect::>>()? + .join(sep.as_str()), Brackets::Bracketed => format!( "[{}]", vals.iter() @@ -157,9 +155,9 @@ impl Value { }, Self::Color(c) => format!("{}", c), Self::UnaryOp(..) | Self::BinaryOp(..) => { - format!("{}", self.clone().eval(span)?.to_css_string(span)?) + self.clone().eval(span)?.to_css_string(span)? } - Self::Paren(val) => format!("{}", val.to_css_string(span)?), + Self::Paren(val) => val.to_css_string(span)?, Self::Ident(string, QuoteKind::None) => { let mut after_newline = false; let mut buf = String::with_capacity(string.len()); @@ -190,14 +188,12 @@ impl Value { Self::True => "true".to_string(), Self::False => "false".to_string(), Self::Null => String::new(), - Self::ArgList(args) => format!( - "{}", - args.iter() - .filter(|x| !x.is_null(span).unwrap()) - .map(|a| Ok(a.node.to_css_string(span)?)) - .collect::>>()? - .join(", "), - ), + Self::ArgList(args) => args + .iter() + .filter(|x| !x.is_null(span).unwrap()) + .map(|a| Ok(a.node.to_css_string(span)?)) + .collect::>>()? + .join(", "), }) } @@ -215,13 +211,13 @@ impl Value { match self { Self::Ident(s1, _) => Self::Ident(s1, QuoteKind::None), Self::List(v, sep, bracket) => { - Self::List(v.into_iter().map(|x| x.unquote()).collect(), sep, bracket) + Self::List(v.into_iter().map(Value::unquote).collect(), sep, bracket) } v => v, } } - pub fn span(self, span: Span) -> Spanned { + pub const fn span(self, span: Span) -> Spanned { Spanned { node: self, span } } @@ -265,7 +261,7 @@ impl Value { }, Value::List(v, sep, brackets) if v.len() == 1 => match brackets { Brackets::None => match sep { - ListSeparator::Space => format!("{}", v[0].inspect(span)?), + ListSeparator::Space => v[0].inspect(span)?, ListSeparator::Comma => format!("({},)", v[0].inspect(span)?), }, Brackets::Bracketed => match sep { @@ -274,13 +270,11 @@ impl Value { }, }, Self::List(vals, sep, brackets) => match brackets { - Brackets::None => format!( - "{}", - vals.iter() - .map(|x| x.inspect(span)) - .collect::>>()? - .join(sep.as_str()), - ), + Brackets::None => vals + .iter() + .map(|x| x.inspect(span)) + .collect::>>()? + .join(sep.as_str()), Brackets::Bracketed => format!( "[{}]", vals.iter() @@ -355,7 +349,7 @@ impl Value { Op::LessThanEqual => return lhs.cmp(*rhs, op, span), Op::Not => unreachable!(), Op::And => { - if lhs.clone().is_true(span)? { + if lhs.is_true(span)? { rhs.eval(span)?.node } else { lhs.eval(span)?.node @@ -389,16 +383,12 @@ impl Value { let ordering = match self { Self::Dimension(num, unit) => match &other { Self::Dimension(num2, unit2) => { - if !unit.comparable(&unit2) { + if !unit.comparable(unit2) { return Err( (format!("Incompatible units {} and {}.", unit2, unit), span).into(), ); } - if &unit == unit2 { - num.cmp(num2) - } else if unit == Unit::None { - num.cmp(num2) - } else if unit2 == &Unit::None { + if &unit == unit2 || unit == Unit::None || unit2 == &Unit::None { num.cmp(num2) } else { num.cmp( diff --git a/src/value/ops.rs b/src/value/ops.rs index 43d9dd6..d1661c5 100644 --- a/src/value/ops.rs +++ b/src/value/ops.rs @@ -32,7 +32,7 @@ impl Value { }, Self::Null => match other { Self::Null => Self::Null, - _ => Value::Ident(format!("{}", other.to_css_string(span)?), QuoteKind::None), + _ => Value::Ident(other.to_css_string(span)?, QuoteKind::None), }, Self::Dimension(num, unit) => match other { Self::Dimension(num2, unit2) => { diff --git a/src/value/parse.rs b/src/value/parse.rs index fe23359..d179ad1 100644 --- a/src/value/parse.rs +++ b/src/value/parse.rs @@ -538,13 +538,12 @@ impl Value { }, }; Ok(IntermediateValue::Value( - func.clone() - .eval( - eat_call_args(toks, scope, super_selector)?, - scope, - super_selector, - )? - .span(span), + func.eval( + eat_call_args(toks, scope, super_selector)?, + scope, + super_selector, + )? + .span(span), )) } _ => { diff --git a/src/value/sass_function.rs b/src/value/sass_function.rs index edaa8b2..f7cd14a 100644 --- a/src/value/sass_function.rs +++ b/src/value/sass_function.rs @@ -60,7 +60,7 @@ impl SassFunction { match self { Self::Builtin(f, ..) => f.0(args, scope, super_selector), // todo: superselector - Self::UserDefined(f, ..) => f.clone().eval(args, scope, super_selector), + Self::UserDefined(f, ..) => f.eval(args, scope, super_selector), } } }