diff --git a/numbat/src/ast.rs b/numbat/src/ast.rs index 5dcbc252..bc3d40a7 100644 --- a/numbat/src/ast.rs +++ b/numbat/src/ast.rs @@ -662,7 +662,7 @@ impl ReplaceSpans for Statement { struct_name_span: Span::dummy(), struct_name: struct_name.clone(), fields: fields - .into_iter() + .iter() .map(|(_span, name, type_)| { (Span::dummy(), name.clone(), type_.replace_spans()) }) diff --git a/numbat/src/ffi/procedures.rs b/numbat/src/ffi/procedures.rs index 1b1da2ef..e3325034 100644 --- a/numbat/src/ffi/procedures.rs +++ b/numbat/src/ffi/procedures.rs @@ -92,12 +92,10 @@ fn assert_eq(_: &mut ExecutionContext, args: &[Value]) -> ControlFlow { } else { error } + } else if lhs == rhs { + ControlFlow::Continue(()) } else { - if lhs == rhs { - ControlFlow::Continue(()) - } else { - error - } + error } } else { let lhs = args[0].unsafe_as_quantity(); diff --git a/numbat/src/html_formatter.rs b/numbat/src/html_formatter.rs index f08aa190..33945aa9 100644 --- a/numbat/src/html_formatter.rs +++ b/numbat/src/html_formatter.rs @@ -47,6 +47,12 @@ pub struct HtmlWriter { color: Option, } +impl Default for HtmlWriter { + fn default() -> Self { + Self::new() + } +} + impl HtmlWriter { pub fn new() -> Self { HtmlWriter { diff --git a/numbat/src/parser.rs b/numbat/src/parser.rs index 19d685fa..59c32780 100644 --- a/numbat/src/parser.rs +++ b/numbat/src/parser.rs @@ -1391,32 +1391,30 @@ impl<'a> Parser<'a> { ParseErrorKind::InlineProcedureUsage, self.peek().span, )) - } else { - if self - .last() - .map(|t| { - matches!( - t.kind, - TokenKind::StringInterpolationStart | TokenKind::StringInterpolationMiddle - ) - }) - .unwrap_or(false) - { - let full_interpolation_end_span = self.peek().span; - let closing_brace_span = full_interpolation_end_span - .start - .single_character_span(full_interpolation_end_span.code_source_id); + } else if self + .last() + .map(|t| { + matches!( + t.kind, + TokenKind::StringInterpolationStart | TokenKind::StringInterpolationMiddle + ) + }) + .unwrap_or(false) + { + let full_interpolation_end_span = self.peek().span; + let closing_brace_span = full_interpolation_end_span + .start + .single_character_span(full_interpolation_end_span.code_source_id); - Err(ParseError::new( - ParseErrorKind::EmptyStringInterpolation, - closing_brace_span, - )) - } else { - Err(ParseError::new( - ParseErrorKind::ExpectedPrimary, - self.peek().span, - )) - } + Err(ParseError::new( + ParseErrorKind::EmptyStringInterpolation, + closing_brace_span, + )) + } else { + Err(ParseError::new( + ParseErrorKind::ExpectedPrimary, + self.peek().span, + )) } } diff --git a/numbat/src/typechecker/constraints.rs b/numbat/src/typechecker/constraints.rs index e770c085..ce0dfa43 100644 --- a/numbat/src/typechecker/constraints.rs +++ b/numbat/src/typechecker/constraints.rs @@ -168,7 +168,7 @@ impl TrivialResultion { /// Ignore the result of the trivial resolution. This is a helper to prevent the /// `must_use` attribute from being triggered. - pub(crate) fn ok(&self) -> () {} + pub(crate) fn ok(&self) {} } /// A type checker constraint can be one of three things: diff --git a/numbat/src/typechecker/environment.rs b/numbat/src/typechecker/environment.rs index a1052e42..3a182657 100644 --- a/numbat/src/typechecker/environment.rs +++ b/numbat/src/typechecker/environment.rs @@ -91,10 +91,11 @@ impl Environment { pub fn iter_relevant_matches(&self) -> impl Iterator { self.identifiers .iter() - .filter(|(_, kind)| match kind { - IdentifierKind::Normal(_, _, true) => false, - IdentifierKind::Predefined(..) => false, - _ => true, + .filter(|(_, kind)| { + !matches!( + kind, + IdentifierKind::Normal(_, _, true) | IdentifierKind::Predefined(..) + ) }) .map(|(id, kind)| (id, kind.get_type())) } @@ -104,7 +105,7 @@ impl Environment { name: &str, ) -> Option<(&FunctionSignature, &FunctionMetadata)> { match self.identifiers.get(name) { - Some(IdentifierKind::Function(signature, metadata)) => Some((signature, &metadata)), + Some(IdentifierKind::Function(signature, metadata)) => Some((signature, metadata)), _ => None, } } diff --git a/numbat/src/typechecker/mod.rs b/numbat/src/typechecker/mod.rs index 788424c6..e0e12288 100644 --- a/numbat/src/typechecker/mod.rs +++ b/numbat/src/typechecker/mod.rs @@ -930,7 +930,7 @@ impl TypeChecker { let found_type = &expr.get_type(); if self - .add_equal_constraint(&found_type, &expected_type) + .add_equal_constraint(found_type, expected_type) .is_trivially_violated() { return Err(TypeCheckError::IncompatibleTypesForStructField( @@ -1023,14 +1023,12 @@ impl TypeChecker { let result_element_type = if element_types.is_empty() { self.fresh_type_variable() + } else if element_types[0].is_closed() { + element_types[0].clone() } else { - if element_types[0].is_closed() { - element_types[0].clone() - } else { - let type_ = self.fresh_type_variable(); - self.add_equal_constraint(&element_types[0], &type_).ok(); - type_ - } + let type_ = self.fresh_type_variable(); + self.add_equal_constraint(&element_types[0], &type_).ok(); + type_ }; if !element_types.is_empty() { @@ -1038,7 +1036,7 @@ impl TypeChecker { elements_checked.iter().zip(element_types.iter()).skip(1) { if self - .add_equal_constraint(&result_element_type, &type_of_subsequent_element) + .add_equal_constraint(&result_element_type, type_of_subsequent_element) .is_trivially_violated() { return Err(TypeCheckError::IncompatibleTypesInList( @@ -1121,7 +1119,7 @@ impl TypeChecker { } (deduced, annotated) => { if self - .add_equal_constraint(&deduced, &annotated) + .add_equal_constraint(deduced, annotated) .is_trivially_violated() { return Err(TypeCheckError::IncompatibleTypesInAnnotation( @@ -1247,7 +1245,7 @@ impl TypeChecker { } (deduced, annotated) => { if self - .add_equal_constraint(&deduced, &annotated) + .add_equal_constraint(deduced, annotated) .is_trivially_violated() { return Err(TypeCheckError::IncompatibleTypesInAnnotation( @@ -1399,7 +1397,7 @@ impl TypeChecker { let body_checked = body .as_ref() - .map(|expr| typechecker_fn.elaborate_expression(&expr)) + .map(|expr| typechecker_fn.elaborate_expression(expr)) .transpose()?; let return_type_inferred = if let Some(ref expr) = body_checked { @@ -1684,7 +1682,7 @@ impl TypeChecker { // Elaborate the program/statement: turn the AST into a typed AST, possibly // with unification variables, i.e. type variables that will only later be // filled in after the constraints have been solved. - let mut elaborated_statement = self.elaborate_statement(&statement)?; + let mut elaborated_statement = self.elaborate_statement(statement)?; // Solve constraints let (substitution, dtype_variables) = @@ -1745,7 +1743,7 @@ impl TypeChecker { // multiple of the denominators of the exponents. For example, this will turn // T0^(1/3) -> T0^(1/5) -> T0 into T0^5 -> T0^3 -> T0^15. for tv in &dtype_variables { - let exponents = elaborated_statement.exponents_for(&tv); + let exponents = elaborated_statement.exponents_for(tv); let lcm = exponents .iter() .fold(1, |acc, e| num_integer::lcm(acc, *e.denom())); diff --git a/numbat/src/typechecker/substitutions.rs b/numbat/src/typechecker/substitutions.rs index 353616e5..31d0c280 100644 --- a/numbat/src/typechecker/substitutions.rs +++ b/numbat/src/typechecker/substitutions.rs @@ -95,7 +95,7 @@ impl ApplySubstitution for DType { for (f, power) in &self.factors { match f { DTypeFactor::TVar(tv) => { - if let Some(type_) = substitution.lookup(&tv) { + if let Some(type_) = substitution.lookup(tv) { let dtype = match type_ { Type::Dimension(dt) => dt.clone(), Type::TVar(tv) => DType::from_type_variable(tv.clone()), diff --git a/numbat/src/typechecker/type_scheme.rs b/numbat/src/typechecker/type_scheme.rs index ef0dc96f..f337fee0 100644 --- a/numbat/src/typechecker/type_scheme.rs +++ b/numbat/src/typechecker/type_scheme.rs @@ -34,7 +34,7 @@ impl TypeScheme { if let TypeScheme::Quantified(n_gen, qt) = &self { assert!(n_gen == &new_type_variables.len()); - qt.instantiate(&new_type_variables) + qt.instantiate(new_type_variables) } else { unreachable!("Tried to instantiate concrete type: {:#?}", self); } diff --git a/numbat/src/typed_ast.rs b/numbat/src/typed_ast.rs index bdaa4a3b..22cf04e5 100644 --- a/numbat/src/typed_ast.rs +++ b/numbat/src/typed_ast.rs @@ -97,10 +97,9 @@ impl DType { pub fn deconstruct_as_single_type_variable(&self) -> Option { match &self.factors[..] { - [(factor, exponent)] if exponent == &Exponent::from_integer(1) => match factor { - DTypeFactor::TVar(v) => Some(v.clone()), - _ => None, - }, + [(DTypeFactor::TVar(v), exponent)] if exponent == &Exponent::from_integer(1) => { + Some(v.clone()) + } _ => None, } } @@ -227,16 +226,16 @@ impl DType { for (f, n) in &self.factors { match f { DTypeFactor::BaseDimension(name) => { - factors.push(BaseRepresentationFactor(name.clone(), n.clone())); + factors.push(BaseRepresentationFactor(name.clone(), *n)); } DTypeFactor::TVar(TypeVariable::Named(name)) => { - factors.push(BaseRepresentationFactor(name.clone(), n.clone())); + factors.push(BaseRepresentationFactor(name.clone(), *n)); } DTypeFactor::TVar(TypeVariable::Quantified(_)) => { unreachable!("Unexpected quantified type") } DTypeFactor::TPar(name) => { - factors.push(BaseRepresentationFactor(name.clone(), n.clone())); + factors.push(BaseRepresentationFactor(name.clone(), *n)); } } } @@ -252,7 +251,7 @@ impl PrettyPrint for DType { impl std::fmt::Display for DType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.pretty_print().to_string()) + write!(f, "{}", self.pretty_print()) } } @@ -315,7 +314,7 @@ impl std::fmt::Display for Type { .join(", ") ) } - Type::List(element_type) => write!(f, "List<{}>", element_type.to_string()), + Type::List(element_type) => write!(f, "List<{}>", element_type), } } } @@ -617,36 +616,32 @@ impl Statement { pub(crate) fn exponents_for(&mut self, tv: &TypeVariable) -> Vec { // TODO: things to not need to be mutable in this function let mut exponents = vec![]; - self.for_all_type_schemes( - &mut |type_: &mut TypeScheme| match type_.unsafe_as_concrete() { - Type::Dimension(dtype) => { - for (factor, exp) in dtype.factors { - if factor == DTypeFactor::TVar(tv.clone()) { - exponents.push(exp) - } + self.for_all_type_schemes(&mut |type_: &mut TypeScheme| { + if let Type::Dimension(dtype) = type_.unsafe_as_concrete() { + for (factor, exp) in dtype.factors { + if factor == DTypeFactor::TVar(tv.clone()) { + exponents.push(exp) } } - _ => {} - }, - ); + } + }); exponents } pub(crate) fn find_typed_hole(&self) -> Result, TypeCheckError> { let mut hole = None; let mut found_multiple_holes = false; - self.for_all_expressions(&mut |expr| match expr { - Expression::TypedHole(span, type_) => { + self.for_all_expressions(&mut |expr| { + if let Expression::TypedHole(span, type_) = expr { if hole.is_some() { found_multiple_holes = true; } hole = Some((*span, type_.clone())) } - _ => {} }); if found_multiple_holes { - return Err(TypeCheckError::MultipleTypedHoles(hole.unwrap().0)); + Err(TypeCheckError::MultipleTypedHoles(hole.unwrap().0)) } else { Ok(hole) }