Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clippy suggestions #477

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion numbat/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down
8 changes: 3 additions & 5 deletions numbat/src/ffi/procedures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions numbat/src/html_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ pub struct HtmlWriter {
color: Option<termcolor::ColorSpec>,
}

impl Default for HtmlWriter {
fn default() -> Self {
Self::new()
}
}

impl HtmlWriter {
pub fn new() -> Self {
HtmlWriter {
Expand Down
48 changes: 23 additions & 25 deletions numbat/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
))
}
}

Expand Down
2 changes: 1 addition & 1 deletion numbat/src/typechecker/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 6 additions & 5 deletions numbat/src/typechecker/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ impl Environment {
pub fn iter_relevant_matches(&self) -> impl Iterator<Item = (&Identifier, TypeScheme)> {
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()))
}
Expand All @@ -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,
}
}
Expand Down
26 changes: 12 additions & 14 deletions numbat/src/typechecker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -1023,22 +1023,20 @@ 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() {
for (subsequent_element, type_of_subsequent_element) in
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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) =
Expand Down Expand Up @@ -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()));
Expand Down
2 changes: 1 addition & 1 deletion numbat/src/typechecker/substitutions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion numbat/src/typechecker/type_scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
41 changes: 18 additions & 23 deletions numbat/src/typed_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ impl DType {

pub fn deconstruct_as_single_type_variable(&self) -> Option<TypeVariable> {
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,
}
}
Expand Down Expand Up @@ -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));
}
}
}
Expand All @@ -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())
}
}

Expand Down Expand Up @@ -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),
}
}
}
Expand Down Expand Up @@ -617,36 +616,32 @@ impl Statement {
pub(crate) fn exponents_for(&mut self, tv: &TypeVariable) -> Vec<Exponent> {
// 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<Option<(Span, TypeScheme)>, 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)
}
Expand Down
Loading