resolve clippy lints

This commit is contained in:
ConnorSkees 2020-06-07 23:11:43 -04:00
parent a48d2b97ce
commit b135b87a69
9 changed files with 81 additions and 106 deletions

View File

@ -1,5 +1,3 @@
#![allow(unused_variables, unused_mut)]
use super::{Builtin, GlobalFunctionMap}; use super::{Builtin, GlobalFunctionMap};
use crate::args::CallArgs; use crate::args::CallArgs;
@ -107,11 +105,7 @@ fn selector_nest(args: CallArgs, scope: &Scope, super_selector: &Selector) -> Sa
.into_value()) .into_value())
} }
fn selector_append( fn selector_append(args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
let span = args.span(); let span = args.span();
let selectors = args.get_variadic(scope, super_selector)?; let selectors = args.get_variadic(scope, super_selector)?;
if selectors.is_empty() { if selectors.is_empty() {

View File

@ -59,6 +59,9 @@ grass input.scss
clippy::filter_map, clippy::filter_map,
clippy::else_if_without_else, clippy::else_if_without_else,
clippy::new_ret_no_self, clippy::new_ret_no_self,
renamed_and_removed_lints,
clippy::unknown_clippy_lints,
clippy::replace_consts,
// temporarily allowed while under heavy development. // temporarily allowed while under heavy development.
// eventually these allows should be refactored away // eventually these allows should be refactored away

View File

@ -214,7 +214,7 @@ impl CompoundSelector {
Self { components } Self { components }
} }
simple => { _ => {
let mut components = vec![SimpleSelector::Parent(None)]; let mut components = vec![SimpleSelector::Parent(None)];
components.append(&mut self.components); components.append(&mut self.components);
Self { components } Self { components }

View File

@ -35,7 +35,7 @@ pub(super) struct Extension {
impl Extension { impl Extension {
pub fn one_off(extender: ComplexSelector, specificity: Option<i32>, is_original: bool) -> Self { pub fn one_off(extender: ComplexSelector, specificity: Option<i32>, is_original: bool) -> Self {
Self { Self {
specificity: specificity.unwrap_or(extender.max_specificity()), specificity: specificity.unwrap_or_else(|| extender.max_specificity()),
extender, extender,
target: None, target: None,
span: None, span: None,

View File

@ -729,11 +729,11 @@ fn complex_is_parent_superselector(
/// ///
/// For example, given `[[1, 2], [3, 4], [5]]`, this returns: /// For example, given `[[1, 2], [3, 4], [5]]`, this returns:
/// ///
/// ```norun /// ```no_run
/// [[1, 3, 5], /// [[1, 3, 5],
/// [2, 3, 5], /// [2, 3, 5],
/// [1, 4, 5], /// [1, 4, 5],
/// [2, 4, 5]] /// [2, 4, 5]];
/// ``` /// ```
pub(crate) fn paths<T: Clone>(choices: Vec<Vec<T>>) -> Vec<Vec<T>> { pub(crate) fn paths<T: Clone>(choices: Vec<Vec<T>>) -> Vec<Vec<T>> {
choices.into_iter().fold(vec![vec![]], |paths, choice| { choices.into_iter().fold(vec![vec![]], |paths, choice| {

View File

@ -38,7 +38,13 @@ enum ExtendMode {
AllTargets, AllTargets,
} }
#[derive(Clone, Debug, Eq, PartialEq)] impl Default for ExtendMode {
fn default() -> Self {
Self::Normal
}
}
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub(crate) struct Extender { pub(crate) struct Extender {
/// A map from all simple selectors in the stylesheet to the selector lists /// A map from all simple selectors in the stylesheet to the selector lists
/// that contain them. /// that contain them.
@ -88,6 +94,7 @@ pub(crate) struct Extender {
impl Extender { impl Extender {
/// An `Extender` that contains no extensions and can have no extensions added. /// An `Extender` that contains no extensions and can have no extensions added.
// TODO: empty extender // TODO: empty extender
#[allow(dead_code)]
const EMPTY: () = (); const EMPTY: () = ();
pub fn extend( pub fn extend(
@ -112,7 +119,7 @@ impl Extender {
targets: SelectorList, targets: SelectorList,
mode: ExtendMode, mode: ExtendMode,
) -> SelectorList { ) -> SelectorList {
let mut extenders: IndexMap<ComplexSelector, Extension> = source let extenders: IndexMap<ComplexSelector, Extension> = source
.components .components
.clone() .clone()
.into_iter() .into_iter()
@ -149,7 +156,7 @@ impl Extender {
.extend(selector.components.clone().into_iter()); .extend(selector.components.clone().into_iter());
} }
selector = extender.extend_list(selector, extensions, None); selector = extender.extend_list(selector, &extensions, &None);
} }
selector selector
@ -158,12 +165,7 @@ impl Extender {
fn with_mode(mode: ExtendMode) -> Self { fn with_mode(mode: ExtendMode) -> Self {
Self { Self {
mode, mode,
selectors: Default::default(), ..Extender::default()
extensions: Default::default(),
extensions_by_extender: Default::default(),
media_contexts: Default::default(),
source_specificity: Default::default(),
originals: Default::default(),
} }
} }
@ -171,8 +173,8 @@ impl Extender {
fn extend_list( fn extend_list(
&mut self, &mut self,
list: SelectorList, list: SelectorList,
extensions: HashMap<SimpleSelector, IndexMap<ComplexSelector, Extension>>, extensions: &HashMap<SimpleSelector, IndexMap<ComplexSelector, Extension>>,
media_query_context: Option<Vec<CssMediaQuery>>, media_query_context: &Option<Vec<CssMediaQuery>>,
) -> SelectorList { ) -> SelectorList {
// This could be written more simply using Vec<Vec<T>>, but we want to avoid // This could be written more simply using Vec<Vec<T>>, but we want to avoid
// any allocations in the common case where no extends apply. // any allocations in the common case where no extends apply.
@ -180,23 +182,17 @@ impl Extender {
for i in 0..list.components.len() { for i in 0..list.components.len() {
let complex = list.components.get(i).unwrap().clone(); let complex = list.components.get(i).unwrap().clone();
if let Some(result) = self.extend_complex( if let Some(result) =
complex.clone(), self.extend_complex(complex.clone(), extensions, media_query_context)
extensions.clone(), {
media_query_context.clone(), if extended.is_empty() && i != 0 {
) {
if extended.is_empty() {
if i != 0 {
extended = list.components[0..i].to_vec(); extended = list.components[0..i].to_vec();
} }
}
extended.extend(result.into_iter()); extended.extend(result.into_iter());
} else { } else if !extended.is_empty() {
if !extended.is_empty() {
extended.push(complex); extended.push(complex);
} }
} }
}
if extended.is_empty() { if extended.is_empty() {
return list; return list;
@ -212,8 +208,8 @@ impl Extender {
fn extend_complex( fn extend_complex(
&mut self, &mut self,
complex: ComplexSelector, complex: ComplexSelector,
extensions: HashMap<SimpleSelector, IndexMap<ComplexSelector, Extension>>, extensions: &HashMap<SimpleSelector, IndexMap<ComplexSelector, Extension>>,
media_query_context: Option<Vec<CssMediaQuery>>, media_query_context: &Option<Vec<CssMediaQuery>>,
) -> Option<Vec<ComplexSelector>> { ) -> Option<Vec<ComplexSelector>> {
// The complex selectors that each compound selector in `complex.components` // The complex selectors that each compound selector in `complex.components`
// can expand to. // can expand to.
@ -236,16 +232,11 @@ impl Extender {
let complex_has_line_break = complex.line_break; let complex_has_line_break = complex.line_break;
let is_original = self.originals.contains(&complex);
for i in 0..complex.components.len() { for i in 0..complex.components.len() {
if let Some(ComplexSelectorComponent::Compound(component)) = complex.components.get(i) { if let Some(ComplexSelectorComponent::Compound(component)) = complex.components.get(i) {
if let Some(extended) = self.extend_compound( if let Some(extended) =
component.clone(), self.extend_compound(component, extensions, media_query_context)
extensions.clone(), {
media_query_context.clone(),
is_original,
) {
if extended_not_expanded.is_empty() { if extended_not_expanded.is_empty() {
extended_not_expanded = complex extended_not_expanded = complex
.components .components
@ -311,15 +302,11 @@ impl Extender {
/// Extends `compound` using `extensions`, and returns the contents of a /// Extends `compound` using `extensions`, and returns the contents of a
/// `SelectorList`. /// `SelectorList`.
///
/// The `in_original` parameter indicates whether this is in an original
/// complex selector, meaning that `compound` should not be trimmed out.
fn extend_compound( fn extend_compound(
&mut self, &mut self,
compound: CompoundSelector, compound: &CompoundSelector,
extensions: HashMap<SimpleSelector, IndexMap<ComplexSelector, Extension>>, extensions: &HashMap<SimpleSelector, IndexMap<ComplexSelector, Extension>>,
media_query_context: Option<Vec<CssMediaQuery>>, media_query_context: &Option<Vec<CssMediaQuery>>,
in_original: bool,
) -> Option<Vec<ComplexSelector>> { ) -> Option<Vec<ComplexSelector>> {
// If there's more than one target and they all need to match, we track // If there's more than one target and they all need to match, we track
// which targets are actually extended. // which targets are actually extended.
@ -332,17 +319,15 @@ impl Extender {
if let Some(extended) = self.extend_simple( if let Some(extended) = self.extend_simple(
simple.clone(), simple.clone(),
extensions.clone(), extensions,
media_query_context.clone(), media_query_context,
&mut targets_used, &mut targets_used,
) { ) {
if options.is_empty() { if options.is_empty() && i != 0 {
if i != 0 {
options.push(vec![self.extension_for_compound( options.push(vec![self.extension_for_compound(
compound.components.clone().into_iter().take(i).collect(), compound.components.clone().into_iter().take(i).collect(),
)]); )]);
} }
}
options.extend(extended.into_iter()); options.extend(extended.into_iter());
} else { } else {
@ -356,7 +341,7 @@ impl Extender {
// If `self.mode` isn't `ExtendMode::Normal` and we didn't use all the targets in // If `self.mode` isn't `ExtendMode::Normal` and we didn't use all the targets in
// `extensions`, extension fails for `compound`. // `extensions`, extension fails for `compound`.
if targets_used.len() > 0 && targets_used.len() != extensions.len() { if !targets_used.is_empty() && targets_used.len() != extensions.len() {
return None; return None;
} }
@ -369,7 +354,7 @@ impl Extender {
.clone() .clone()
.into_iter() .into_iter()
.map(|state| { .map(|state| {
state.assert_compatible_media_context(&media_query_context); state.assert_compatible_media_context(media_query_context);
state.extender state.extender
}) })
.collect(), .collect(),
@ -405,15 +390,13 @@ impl Extender {
let unified_paths: Vec<Option<Vec<ComplexSelector>>> = paths(options) let unified_paths: Vec<Option<Vec<ComplexSelector>>> = paths(options)
.into_iter() .into_iter()
.map(|path| { .map(|path| {
let complexes: Vec<Vec<ComplexSelectorComponent>>; let complexes: Vec<Vec<ComplexSelectorComponent>> = if first {
if first {
// The first path is always the original selector. We can't just // The first path is always the original selector. We can't just
// return `compound` directly because pseudo selectors may be // return `compound` directly because pseudo selectors may be
// modified, but we don't have to do any unification. // modified, but we don't have to do any unification.
first = false; first = false;
complexes = vec![vec![ComplexSelectorComponent::Compound(CompoundSelector { vec![vec![ComplexSelectorComponent::Compound(CompoundSelector {
components: path components: path
.clone() .clone()
.into_iter() .into_iter()
@ -425,7 +408,7 @@ impl Extender {
} }
}) })
.collect(), .collect(),
})]]; })]]
} else { } else {
let mut to_unify: VecDeque<Vec<ComplexSelectorComponent>> = VecDeque::new(); let mut to_unify: VecDeque<Vec<ComplexSelectorComponent>> = VecDeque::new();
let mut originals: Vec<SimpleSelector> = Vec::new(); let mut originals: Vec<SimpleSelector> = Vec::new();
@ -448,13 +431,13 @@ impl Extender {
)]); )]);
} }
complexes = unify_complex(Vec::from(to_unify))?; unify_complex(Vec::from(to_unify))?
} };
let mut line_break = false; let mut line_break = false;
for state in path { for state in path {
state.assert_compatible_media_context(&media_query_context); state.assert_compatible_media_context(media_query_context);
line_break = line_break || state.extender.line_break; line_break = line_break || state.extender.line_break;
} }
@ -482,8 +465,8 @@ impl Extender {
fn extend_simple( fn extend_simple(
&mut self, &mut self,
simple: SimpleSelector, simple: SimpleSelector,
extensions: HashMap<SimpleSelector, IndexMap<ComplexSelector, Extension>>, extensions: &HashMap<SimpleSelector, IndexMap<ComplexSelector, Extension>>,
media_query_context: Option<Vec<CssMediaQuery>>, media_query_context: &Option<Vec<CssMediaQuery>>,
targets_used: &mut HashSet<SimpleSelector>, targets_used: &mut HashSet<SimpleSelector>,
) -> Option<Vec<Vec<Extension>>> { ) -> Option<Vec<Vec<Extension>>> {
if let SimpleSelector::Pseudo( if let SimpleSelector::Pseudo(
@ -492,16 +475,14 @@ impl Extender {
}, },
) = simple.clone() ) = simple.clone()
{ {
if let Some(extended) = if let Some(extended) = self.extend_pseudo(simple, extensions, media_query_context) {
self.extend_pseudo(simple, extensions.clone(), media_query_context)
{
return Some( return Some(
extended extended
.into_iter() .into_iter()
.map(move |pseudo| { .map(move |pseudo| {
self.without_pseudo( self.without_pseudo(
SimpleSelector::Pseudo(pseudo.clone()), SimpleSelector::Pseudo(pseudo.clone()),
extensions.clone(), extensions,
targets_used, targets_used,
self.mode, self.mode,
) )
@ -523,14 +504,11 @@ impl Extender {
fn extend_pseudo( fn extend_pseudo(
&mut self, &mut self,
pseudo: Pseudo, pseudo: Pseudo,
extensions: HashMap<SimpleSelector, IndexMap<ComplexSelector, Extension>>, extensions: &HashMap<SimpleSelector, IndexMap<ComplexSelector, Extension>>,
media_query_context: Option<Vec<CssMediaQuery>>, media_query_context: &Option<Vec<CssMediaQuery>>,
) -> Option<Vec<Pseudo>> { ) -> Option<Vec<Pseudo>> {
let extended = self.extend_list( let extended = self.extend_list(
pseudo pseudo.selector.clone().unwrap_or_else(SelectorList::new),
.selector
.clone()
.unwrap_or_else(|| SelectorList::new()),
extensions, extensions,
media_query_context, media_query_context,
); );
@ -544,8 +522,7 @@ impl Extender {
// writing. We can keep them if either the original selector had a complex // writing. We can keep them if either the original selector had a complex
// selector, or the result of extending has only complex selectors, because // selector, or the result of extending has only complex selectors, because
// either way we aren't breaking anything that isn't already broken. // either way we aren't breaking anything that isn't already broken.
let mut complexes = extended.components.clone(); let mut complexes = if pseudo.normalized_name == "not"
if pseudo.normalized_name == "not"
&& !pseudo && !pseudo
.selector .selector
.clone() .clone()
@ -558,13 +535,14 @@ impl Extender {
.iter() .iter()
.any(|complex| complex.components.len() == 1) .any(|complex| complex.components.len() == 1)
{ {
complexes = extended extended
.components .components
.clone()
.into_iter() .into_iter()
.filter(|complex| complex.components.len() <= 1) .filter(|complex| complex.components.len() <= 1)
.collect(); .collect()
} } else {
extended.components
};
complexes = complexes complexes = complexes
.into_iter() .into_iter()
@ -598,10 +576,10 @@ impl Extender {
// become `.foo:not(.bar)`. However, this is a narrow edge case and // become `.foo:not(.bar)`. However, this is a narrow edge case and
// supporting it properly would make this code and the code calling it // supporting it properly would make this code and the code calling it
// a lot more complicated, so it's not supported for now. // a lot more complicated, so it's not supported for now.
if inner_pseudo.normalized_name != "matches" { if inner_pseudo.normalized_name == "matches" {
Vec::new()
} else {
inner_pseudo.selector.clone().unwrap().components inner_pseudo.selector.clone().unwrap().components
} else {
Vec::new()
} }
} }
"matches" | "any" | "current" | "nth-child" | "nth-last-child" => { "matches" | "any" | "current" | "nth-child" | "nth-last-child" => {
@ -657,7 +635,7 @@ impl Extender {
fn without_pseudo( fn without_pseudo(
&self, &self,
simple: SimpleSelector, simple: SimpleSelector,
extensions: HashMap<SimpleSelector, IndexMap<ComplexSelector, Extension>>, extensions: &HashMap<SimpleSelector, IndexMap<ComplexSelector, Extension>>,
targets_used: &mut HashSet<SimpleSelector>, targets_used: &mut HashSet<SimpleSelector>,
mode: ExtendMode, mode: ExtendMode,
) -> Option<Vec<Extension>> { ) -> Option<Vec<Extension>> {
@ -782,17 +760,19 @@ impl Extender {
// ensures that we aren't comparing against a selector that's already been // ensures that we aren't comparing against a selector that's already been
// trimmed, and thus that if there are two identical selectors only one is // trimmed, and thus that if there are two identical selectors only one is
// trimmed. // trimmed.
if result.iter().any(|complex2| { let should_continue = result.iter().any(|complex2| {
complex2.min_specificity() >= max_specificity complex2.min_specificity() >= max_specificity
&& complex2.is_super_selector(complex1) && complex2.is_super_selector(complex1)
}) { });
if should_continue {
continue; continue;
} }
if selectors.iter().take(i).any(|complex2| { let should_continue = selectors.iter().take(i).any(|complex2| {
complex2.min_specificity() >= max_specificity complex2.min_specificity() >= max_specificity
&& complex2.is_super_selector(complex1) && complex2.is_super_selector(complex1)
}) { });
if should_continue {
continue; continue;
} }
@ -800,12 +780,11 @@ impl Extender {
} }
if should_break_to_outer { if should_break_to_outer {
continue; continue;
} else { }
break; break;
} }
}
return Vec::from(result); Vec::from(result)
} }
} }

View File

@ -1,5 +1,3 @@
#![allow(dead_code, unused_variables, unused_mut)]
use std::fmt::{self, Display}; use std::fmt::{self, Display};
use peekmore::{PeekMore, PeekMoreIterator}; use peekmore::{PeekMore, PeekMoreIterator};

View File

@ -335,7 +335,7 @@ impl<'a, I: Iterator<Item = Token>> SelectorParser<'a, I> {
devour_whitespace(self.toks); devour_whitespace(self.toks);
self.expect_closing_paren()?; self.expect_closing_paren()?;
} else { } else {
argument = Some(self.declaration_value(true)?); argument = Some(self.declaration_value()?);
} }
} else if SELECTOR_PSEUDO_CLASSES.contains(&unvendored) { } else if SELECTOR_PSEUDO_CLASSES.contains(&unvendored) {
selector = Some(self.parse_selector_list()?); selector = Some(self.parse_selector_list()?);
@ -358,7 +358,7 @@ impl<'a, I: Iterator<Item = Token>> SelectorParser<'a, I> {
self.expect_closing_paren()?; self.expect_closing_paren()?;
argument = Some(this_arg); argument = Some(this_arg);
} else { } else {
argument = Some(self.declaration_value(true)?.trim_end().to_string()); argument = Some(self.declaration_value()?.trim_end().to_string());
} }
Ok(SimpleSelector::Pseudo(Pseudo { Ok(SimpleSelector::Pseudo(Pseudo {
@ -533,7 +533,7 @@ impl<'a, I: Iterator<Item = Token>> SelectorParser<'a, I> {
Ok(buf) Ok(buf)
} }
fn declaration_value(&mut self, allow_empty: bool) -> SassResult<String> { fn declaration_value(&mut self) -> SassResult<String> {
// todo: this consumes the closing paren // todo: this consumes the closing paren
let mut tmp = read_until_closing_paren(self.toks)?; let mut tmp = read_until_closing_paren(self.toks)?;
if let Some(Token { kind: ')', .. }) = tmp.pop() { if let Some(Token { kind: ')', .. }) = tmp.pop() {

View File

@ -567,6 +567,7 @@ impl Pseudo {
} }
} }
#[allow(clippy::missing_const_for_fn)]
pub fn with_selector(self, selector: Option<SelectorList>) -> Self { pub fn with_selector(self, selector: Option<SelectorList>) -> Self {
Self { selector, ..self } Self { selector, ..self }
} }
@ -601,7 +602,7 @@ impl Pseudo {
min = min.max(complex.min_specificity()); min = min.max(complex.min_specificity());
max = max.max(complex.max_specificity()); max = max.max(complex.max_specificity());
} }
return Specificity { min, max }; Specificity { min, max }
} else { } else {
// This is higher than any selector's specificity can actually be. // This is higher than any selector's specificity can actually be.
let mut min = BASE_SPECIFICITY.pow(3_u32); let mut min = BASE_SPECIFICITY.pow(3_u32);
@ -610,7 +611,7 @@ impl Pseudo {
min = min.min(complex.min_specificity()); min = min.min(complex.min_specificity());
max = max.max(complex.max_specificity()); max = max.max(complex.max_specificity());
} }
return Specificity { min, max }; Specificity { min, max }
} }
} }
} }