From 8ea601ee43a890a4efb95d9f0368860f764398cf Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Sun, 4 Jul 2021 01:24:08 -0400 Subject: [PATCH] nightly clippy --- src/builtin/functions/map.rs | 4 +- src/main.rs | 2 +- src/parse/args.rs | 2 +- src/parse/control_flow.rs | 8 +-- src/parse/ident.rs | 4 +- src/parse/keyframes.rs | 6 +- src/parse/media.rs | 18 +++--- src/parse/mod.rs | 9 ++- src/parse/style.rs | 2 +- src/parse/throw_away.rs | 2 +- src/parse/value/parse.rs | 2 +- src/selector/attribute.rs | 2 +- src/selector/extend/mod.rs | 119 +++++++++++++++++------------------ src/selector/simple.rs | 2 +- src/utils/peek_until.rs | 4 +- src/value/map.rs | 8 +-- 16 files changed, 100 insertions(+), 94 deletions(-) diff --git a/src/builtin/functions/map.rs b/src/builtin/functions/map.rs index f84ff57..db1d1b3 100644 --- a/src/builtin/functions/map.rs +++ b/src/builtin/functions/map.rs @@ -25,7 +25,7 @@ pub(crate) fn map_get(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult .into()) } }; - Ok(map.get(&key)?.unwrap_or(Value::Null)) + Ok(map.get(&key).unwrap_or(Value::Null)) } pub(crate) fn map_has_key(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { @@ -43,7 +43,7 @@ pub(crate) fn map_has_key(mut args: CallArgs, parser: &mut Parser<'_>) -> SassRe .into()) } }; - Ok(Value::bool(map.get(&key)?.is_some())) + Ok(Value::bool(map.get(&key).is_some())) } pub(crate) fn map_keys(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { diff --git a/src/main.rs b/src/main.rs index 0ba8fa2..404214d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -206,7 +206,7 @@ fn main() -> std::io::Result<()> { buf_out.write_all( if let Some(name) = matches.value_of("INPUT") { - from_path(name, &options) + from_path(name, options) } else if matches.is_present("STDIN") { from_string( { diff --git a/src/parse/args.rs b/src/parse/args.rs index b2cb7be..8df0436 100644 --- a/src/parse/args.rs +++ b/src/parse/args.rs @@ -232,7 +232,7 @@ impl<'a> Parser<'a> { let pos = *pos; self.toks.next(); - if let Some(Token { kind: '.', pos }) = self.toks.peek().cloned() { + if let Some(Token { kind: '.', pos }) = self.toks.peek().copied() { if !name.is_empty() { return Err(("expected \")\".", pos).into()); } diff --git a/src/parse/control_flow.rs b/src/parse/control_flow.rs index 1504c03..238bc35 100644 --- a/src/parse/control_flow.rs +++ b/src/parse/control_flow.rs @@ -60,7 +60,7 @@ impl<'a> Parser<'a> { loop { self.whitespace_or_comment(); - if let Some(Token { kind: '@', pos }) = self.toks.peek().cloned() { + if let Some(Token { kind: '@', pos }) = self.toks.peek().copied() { self.toks.peek_forward(1); let ident = peek_ident_no_interpolation(self.toks, false, pos)?; if ident.as_str() != "else" { @@ -72,7 +72,7 @@ impl<'a> Parser<'a> { break; } self.whitespace_or_comment(); - if let Some(tok) = self.toks.peek().cloned() { + if let Some(tok) = self.toks.peek().copied() { match tok.kind { 'i' if matches!( self.toks.peek_forward(1), @@ -195,9 +195,9 @@ impl<'a> Parser<'a> { Some(..) | None => false, })?; - let through = if self.scan_identifier("through")? { + let through = if self.scan_identifier("through") { 1 - } else if self.scan_identifier("to")? { + } else if self.scan_identifier("to") { 0 } else { return Err(("Expected \"to\" or \"through\".", self.span_before).into()); diff --git a/src/parse/ident.rs b/src/parse/ident.rs index 07b9645..28f8fc1 100644 --- a/src/parse/ident.rs +++ b/src/parse/ident.rs @@ -59,7 +59,7 @@ impl<'a> Parser<'a> { buf.push_str(&self.escape(false)?); } '#' => { - if let Some(Token { kind: '{', .. }) = self.toks.peek_forward(1).cloned() { + if let Some(Token { kind: '{', .. }) = self.toks.peek_forward(1).copied() { self.toks.next(); self.toks.next(); // TODO: if ident, interpolate literally @@ -136,7 +136,7 @@ impl<'a> Parser<'a> { let Token { kind, pos } = self .toks .peek() - .cloned() + .copied() .ok_or(("Expected identifier.", self.span_before))?; let mut text = String::new(); if kind == '-' { diff --git a/src/parse/keyframes.rs b/src/parse/keyframes.rs index e80ccca..443db16 100644 --- a/src/parse/keyframes.rs +++ b/src/parse/keyframes.rs @@ -34,7 +34,7 @@ impl<'a, 'b> KeyframesSelectorParser<'a, 'b> { fn parse_keyframes_selector(&mut self) -> SassResult> { let mut selectors = Vec::new(); self.parser.whitespace_or_comment(); - while let Some(tok) = self.parser.toks.peek().cloned() { + while let Some(tok) = self.parser.toks.peek().copied() { match tok.kind { 't' | 'T' => { let mut ident = self.parser.parse_identifier()?; @@ -128,7 +128,7 @@ impl<'a> Parser<'a> { span = span.merge(tok.pos()); match tok.kind { '#' => { - if let Some(Token { kind: '{', .. }) = self.toks.peek().cloned() { + if let Some(Token { kind: '{', .. }) = self.toks.peek().copied() { self.toks.next(); string.push_str(&self.parse_interpolation()?.to_css_string(span)?); } else { @@ -154,6 +154,8 @@ impl<'a> Parser<'a> { string.push(' '); } '{' => { + // we must collect here because the parser is not generic over iterator + #[allow(clippy::needless_collect)] let sel_toks: Vec = string.chars().map(|x| Token::new(span, x)).collect(); diff --git a/src/parse/media.rs b/src/parse/media.rs index bbeb9cb..253d6de 100644 --- a/src/parse/media.rs +++ b/src/parse/media.rs @@ -11,20 +11,24 @@ impl<'a> Parser<'a> { /// consume the identifier /// /// This method is case insensitive - pub fn scan_identifier(&mut self, ident: &'static str) -> SassResult { + pub fn scan_identifier(&mut self, ident: &'static str) -> bool { let mut peeked_identifier = match peek_ident_no_interpolation(self.toks, false, self.span_before) { Ok(v) => v.node, - Err(..) => return Ok(false), + Err(..) => return false, }; + peeked_identifier.make_ascii_lowercase(); + if peeked_identifier == ident { self.toks.truncate_iterator_to_cursor(); self.toks.next(); - return Ok(true); + return true; } + self.toks.reset_cursor(); - Ok(false) + + false } pub fn expression_until_comparison(&mut self) -> SassResult> { @@ -94,7 +98,7 @@ impl<'a> Parser<'a> { return Ok(buf); } - let next_tok = self.toks.peek().cloned(); + let next_tok = self.toks.peek().copied(); let is_angle = next_tok.map_or(false, |t| t.kind == '<' || t.kind == '>'); if is_angle || matches!(next_tok, Some(Token { kind: '=', .. })) { buf.push(' '); @@ -140,7 +144,7 @@ impl<'a> Parser<'a> { } else { buf.push_str(&ident); - if self.scan_identifier("and")? { + if self.scan_identifier("and") { self.whitespace_or_comment(); buf.push_str(" and "); } else { @@ -153,7 +157,7 @@ impl<'a> Parser<'a> { self.whitespace_or_comment(); buf.push_str(&self.parse_media_feature()?); self.whitespace_or_comment(); - if !self.scan_identifier("and")? { + if !self.scan_identifier("and") { break; } buf.push_str(" and "); diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 2699c12..a00af5c 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -403,7 +403,7 @@ impl<'a> Parser<'a> { span = span.merge(pos); match kind { '#' => { - if let Some(Token { kind: '{', .. }) = self.toks.peek().cloned() { + if let Some(Token { kind: '{', .. }) = self.toks.peek().copied() { self.toks.next(); string.push_str(&self.parse_interpolation()?.to_css_string(span)?); } else { @@ -447,6 +447,8 @@ impl<'a> Parser<'a> { return Err(("expected \"{\".", span).into()); } + // we must collect here because the parser isn't generic over the iterator + #[allow(clippy::needless_collect)] let sel_toks: Vec = string.chars().map(|x| Token::new(span, x)).collect(); let mut iter = sel_toks.into_iter().peekmore(); @@ -565,7 +567,7 @@ impl<'a> Parser<'a> { /// /// The newline is consumed pub fn read_until_newline(&mut self) { - while let Some(tok) = self.toks.next() { + for tok in &mut self.toks { if tok.kind == '\n' { break; } @@ -585,6 +587,7 @@ impl<'a> Parser<'a> { found_whitespace = true; self.toks.next(); self.toks.next(); + #[allow(clippy::while_let_on_iterator)] while let Some(tok) = self.toks.next() { if tok.kind == '*' { if let Some(Token { kind: '/', .. }) = self.toks.peek() { @@ -885,7 +888,7 @@ impl<'a> Parser<'a> { match tok.kind { '{' => break, '#' => { - if let Some(Token { kind: '{', pos }) = self.toks.peek().cloned() { + if let Some(Token { kind: '{', pos }) = self.toks.peek().copied() { self.toks.next(); self.span_before = pos; let interpolation = self.parse_interpolation()?; diff --git a/src/parse/style.rs b/src/parse/style.rs index f5056fb..dcc6a45 100644 --- a/src/parse/style.rs +++ b/src/parse/style.rs @@ -201,7 +201,7 @@ impl<'a> Parser<'a> { ) -> SassResult> { let mut styles = Vec::new(); self.whitespace(); - while let Some(tok) = self.toks.peek().cloned() { + while let Some(tok) = self.toks.peek().copied() { match tok.kind { '{' => { self.toks.next(); diff --git a/src/parse/throw_away.rs b/src/parse/throw_away.rs index 8285b0f..316cf36 100644 --- a/src/parse/throw_away.rs +++ b/src/parse/throw_away.rs @@ -6,7 +6,7 @@ use super::Parser; impl<'a> Parser<'a> { pub(super) fn throw_away_until_newline(&mut self) { - while let Some(tok) = self.toks.next() { + for tok in &mut self.toks { if tok.kind == '\n' { break; } diff --git a/src/parse/value/parse.rs b/src/parse/value/parse.rs index 0241ca9..210e59a 100644 --- a/src/parse/value/parse.rs +++ b/src/parse/value/parse.rs @@ -488,7 +488,7 @@ impl<'a> Parser<'a> { Unit::from(u.node) } '-' => { - if let Some(Token { kind, .. }) = self.toks.peek_next().cloned() { + if let Some(Token { kind, .. }) = self.toks.peek_next().copied() { self.toks.reset_cursor(); if matches!(kind, 'a'..='z' | 'A'..='Z' | '_' | '\\' | '\u{7f}'..=std::char::MAX) { diff --git a/src/selector/attribute.rs b/src/selector/attribute.rs index b1fc91b..bc7cde9 100644 --- a/src/selector/attribute.rs +++ b/src/selector/attribute.rs @@ -141,7 +141,7 @@ impl Attribute { }; parser.whitespace(); - let modifier = match parser.toks.peek().cloned() { + let modifier = match parser.toks.peek().copied() { Some(Token { kind: c @ 'a'..='z', .. diff --git a/src/selector/extend/mod.rs b/src/selector/extend/mod.rs index cbdf7f8..98c1c1e 100644 --- a/src/selector/extend/mod.rs +++ b/src/selector/extend/mod.rs @@ -461,73 +461,70 @@ impl Extender { // ] let mut first = self.mode != ExtendMode::Replace; - let unified_paths: Vec>> = paths(options) - .into_iter() - .map(|path| { - let complexes: Vec> = if first { - // The first path is always the original selector. We can't just - // return `compound` directly because pseudo selectors may be - // modified, but we don't have to do any unification. - first = false; + let unified_paths = paths(options).into_iter().map(|path| { + let complexes: Vec> = if first { + // The first path is always the original selector. We can't just + // return `compound` directly because pseudo selectors may be + // modified, but we don't have to do any unification. + first = false; - vec![vec![ComplexSelectorComponent::Compound(CompoundSelector { - components: path - .clone() - .into_iter() - .flat_map(|state| { - debug_assert!(state.extender.components.len() == 1); - match state.extender.components.last().cloned() { - Some(ComplexSelectorComponent::Compound(c)) => c.components, - Some(..) | None => unreachable!(), - } - }) - .collect(), - })]] - } else { - let mut to_unify: VecDeque> = VecDeque::new(); - let mut originals: Vec = Vec::new(); - - for state in path.clone() { - if state.is_original { - originals.extend(match state.extender.components.last().cloned() { + vec![vec![ComplexSelectorComponent::Compound(CompoundSelector { + components: path + .clone() + .into_iter() + .flat_map(|state| { + debug_assert!(state.extender.components.len() == 1); + match state.extender.components.last().cloned() { Some(ComplexSelectorComponent::Compound(c)) => c.components, Some(..) | None => unreachable!(), - }); - } else { - to_unify.push_back(state.extender.components.clone()); - } - } - if !originals.is_empty() { - to_unify.push_front(vec![ComplexSelectorComponent::Compound( - CompoundSelector { - components: originals, - }, - )]); - } - - unify_complex(Vec::from(to_unify))? - }; - - let mut line_break = false; - - for state in path { - state.assert_compatible_media_context(media_query_context); - line_break = line_break || state.extender.line_break; - } - - Some( - complexes - .into_iter() - .map(|components| ComplexSelector { - components, - line_break, + } }) .collect(), - ) - }) - .collect(); + })]] + } else { + let mut to_unify: VecDeque> = VecDeque::new(); + let mut originals: Vec = Vec::new(); - Some(unified_paths.into_iter().flatten().flatten().collect()) + for state in path.clone() { + if state.is_original { + originals.extend(match state.extender.components.last().cloned() { + Some(ComplexSelectorComponent::Compound(c)) => c.components, + Some(..) | None => unreachable!(), + }); + } else { + to_unify.push_back(state.extender.components.clone()); + } + } + if !originals.is_empty() { + to_unify.push_front(vec![ComplexSelectorComponent::Compound( + CompoundSelector { + components: originals, + }, + )]); + } + + unify_complex(Vec::from(to_unify))? + }; + + let mut line_break = false; + + for state in path { + state.assert_compatible_media_context(media_query_context); + line_break = line_break || state.extender.line_break; + } + + Some( + complexes + .into_iter() + .map(|components| ComplexSelector { + components, + line_break, + }) + .collect::>(), + ) + }); + + Some(unified_paths.flatten().flatten().collect()) } fn extend_simple( diff --git a/src/selector/simple.rs b/src/selector/simple.rs index ea59de9..ae381d9 100644 --- a/src/selector/simple.rs +++ b/src/selector/simple.rs @@ -477,7 +477,7 @@ impl Pseudo { /// /// This assumes that `pseudo1`'s `selector` argument is not `None`. /// - /// If `parents is passed, it represents the parents of `compound`. This is + /// If `parents` is passed, it represents the parents of `compound`. This is /// relevant for pseudo selectors with selector arguments, where we may need to /// know if the parent selectors in the selector argument match `parents`. pub fn is_super_selector( diff --git a/src/utils/peek_until.rs b/src/utils/peek_until.rs index b35fd86..265934b 100644 --- a/src/utils/peek_until.rs +++ b/src/utils/peek_until.rs @@ -13,7 +13,7 @@ pub(crate) fn peek_until_closing_curly_brace( ) -> SassResult> { let mut t = Vec::new(); let mut nesting = 0; - while let Some(tok) = toks.peek().cloned() { + while let Some(tok) = toks.peek().copied() { match tok.kind { q @ '"' | q @ '\'' => { t.push(tok); @@ -59,7 +59,7 @@ fn peek_until_closing_quote( q: char, ) -> SassResult> { let mut t = Vec::new(); - while let Some(tok) = toks.peek().cloned() { + while let Some(tok) = toks.peek().copied() { match tok.kind { '"' if q == '"' => { t.push(tok); diff --git a/src/value/map.rs b/src/value/map.rs index 6c76661..efca3d2 100644 --- a/src/value/map.rs +++ b/src/value/map.rs @@ -2,7 +2,6 @@ use std::{slice::Iter, vec::IntoIter}; use crate::{ common::{Brackets, ListSeparator}, - error::SassResult, value::Value, }; @@ -42,13 +41,14 @@ impl SassMap { /// save a clone of the value, since the only place this /// should be called is in a builtin function, which throws /// away the map immediately anyway - pub fn get(self, key: &Value) -> SassResult> { + pub fn get(self, key: &Value) -> Option { for (k, v) in self.0 { if &k == key { - return Ok(Some(v)); + return Some(v); } } - Ok(None) + + None } pub fn get_mut(&mut self, key: &Value) -> Option<&mut Value> {