Automatically apply clippy to various util modules

This commit is contained in:
Greg Shuflin 2021-10-19 21:57:14 -07:00
parent c9c65b050c
commit 36f06b38de
4 changed files with 32 additions and 32 deletions

View File

@ -39,12 +39,12 @@ fn declaration<V: ASTVisitor>(v: &mut V, decl: &Declaration) {
use Declaration::*; use Declaration::*;
match decl { match decl {
FuncSig(sig) => { FuncSig(sig) => {
v.signature(&sig); v.signature(sig);
signature(v, &sig); signature(v, sig);
}, },
FuncDecl(sig, block) => { FuncDecl(sig, block) => {
v.signature(&sig); v.signature(sig);
v.block(&block); v.block(block);
walk_block(v, block); walk_block(v, block);
}, },
TypeDecl { name, body, mutable } => v.type_declaration(name, body, *mutable), TypeDecl { name, body, mutable } => v.type_declaration(name, body, *mutable),
@ -52,8 +52,8 @@ fn declaration<V: ASTVisitor>(v: &mut V, decl: &Declaration) {
Binding { name, constant, type_anno, expr } => { Binding { name, constant, type_anno, expr } => {
v.binding(name, *constant, type_anno.as_ref(), expr); v.binding(name, *constant, type_anno.as_ref(), expr);
v.type_annotation(type_anno.as_ref()); v.type_annotation(type_anno.as_ref());
v.expression(&expr); v.expression(expr);
expression(v, &expr); expression(v, expr);
}, },
Impl { type_name, interface_name, block } => { Impl { type_name, interface_name, block } => {
v.implemention(type_name, interface_name.as_ref(), block); v.implemention(type_name, interface_name.as_ref(), block);
@ -172,7 +172,7 @@ fn expression_kind<V: ASTVisitor>(v: &mut V, expression_kind: &ExpressionKind) {
}, },
IfExpression { discriminator, body } => { IfExpression { discriminator, body } => {
v.if_expression(deref_optional_box(discriminator), body); v.if_expression(deref_optional_box(discriminator), body);
discriminator.as_ref().map(|d| expression(v, d)); if let Some(d) = discriminator.as_ref() { expression(v, d) }
if_expression_body(v, body); if_expression_body(v, body);
}, },
WhileExpression { condition, body } => v.while_expression(deref_optional_box(condition), body), WhileExpression { condition, body } => v.while_expression(deref_optional_box(condition), body),
@ -195,13 +195,13 @@ fn if_expression_body<V: ASTVisitor>(v: &mut V, body: &IfExpressionBody) {
match body { match body {
SimpleConditional { then_case, else_case } => { SimpleConditional { then_case, else_case } => {
walk_block(v, then_case); walk_block(v, then_case);
else_case.as_ref().map(|block| walk_block(v, block)); if let Some(block) = else_case.as_ref() { walk_block(v, block) }
}, },
SimplePatternMatch { pattern, then_case, else_case } => { SimplePatternMatch { pattern, then_case, else_case } => {
v.pattern(pattern); v.pattern(pattern);
walk_pattern(v, pattern); walk_pattern(v, pattern);
walk_block(v, then_case); walk_block(v, then_case);
else_case.as_ref().map(|block| walk_block(v, block)); if let Some(block) = else_case.as_ref() { walk_block(v, block) }
}, },
CondList(arms) => { CondList(arms) => {
for arm in arms { for arm in arms {

View File

@ -13,15 +13,15 @@ fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> {
let reduced = reduce(&ast, &symbol_table); let reduced = reduce(&ast, &symbol_table);
let mut state = State::new(); let mut state = State::new();
let all_output = state.evaluate(reduced, true);
all_output state.evaluate(reduced, true)
} }
macro_rules! test_in_fresh_env { macro_rules! test_in_fresh_env {
($string:expr, $correct:expr) => { ($string:expr, $correct:expr) => {
{ {
let all_output = evaluate_all_outputs($string); let all_output = evaluate_all_outputs($string);
let ref output = all_output.last().unwrap(); let output = &all_output.last().unwrap();
assert_eq!(**output, Ok($correct.to_string())); assert_eq!(**output, Ok($correct.to_string()));
} }
} }

View File

@ -83,13 +83,13 @@ impl TypeConst {
pub fn to_string(&self) -> String { pub fn to_string(&self) -> String {
use self::TypeConst::*; use self::TypeConst::*;
match self { match self {
Unit => format!("()"), Unit => "()".to_string(),
Nat => format!("Nat"), Nat => "Nat".to_string(),
Int => format!("Int"), Int => "Int".to_string(),
Float => format!("Float"), Float => "Float".to_string(),
StringT => format!("String"), StringT => "String".to_string(),
Bool => format!("Bool"), Bool => "Bool".to_string(),
Ordering => format!("Ordering"), Ordering => "Ordering".to_string(),
} }
} }
} }
@ -117,7 +117,7 @@ impl Type {
Const(c) => c.to_string(), Const(c) => c.to_string(),
Var(v) => format!("t_{}", v.0), Var(v) => format!("t_{}", v.0),
Arrow { params, box ref ret } => { Arrow { params, box ref ret } => {
if params.len() == 0 { if params.is_empty() {
format!("-> {}", ret.to_string()) format!("-> {}", ret.to_string())
} else { } else {
let mut buf = String::new(); let mut buf = String::new();
@ -128,7 +128,7 @@ impl Type {
buf buf
} }
}, },
Compound { .. } => format!("<some compound type>") Compound { .. } => "<some compound type>".to_string()
} }
} }
@ -256,7 +256,7 @@ impl<'a> TypeContext<'a> {
use self::TypeIdentifier::*; use self::TypeIdentifier::*;
Ok(match name { Ok(match name {
Singleton(TypeSingletonName { name,.. }) => { Singleton(TypeSingletonName { name,.. }) => {
match Type::from_string(&name) { match Type::from_string(name) {
Some(ty) => ty, Some(ty) => ty,
None => return TypeError::new(format!("Unknown type name: {}", name)) None => return TypeError::new(format!("Unknown type name: {}", name))
} }
@ -279,7 +279,7 @@ impl<'a> TypeContext<'a> {
fn statement(&mut self, statement: &Statement) -> InferResult<Type> { fn statement(&mut self, statement: &Statement) -> InferResult<Type> {
match &statement.kind { match &statement.kind {
StatementKind::Expression(e) => self.expr(e), StatementKind::Expression(e) => self.expr(e),
StatementKind::Declaration(decl) => self.decl(&decl), StatementKind::Declaration(decl) => self.decl(decl),
StatementKind::Import(_) => Ok(ty!(Unit)), StatementKind::Import(_) => Ok(ty!(Unit)),
StatementKind::Module(_) => Ok(ty!(Unit)), StatementKind::Module(_) => Ok(ty!(Unit)),
} }
@ -361,7 +361,7 @@ impl<'a> TypeContext<'a> {
use self::IfExpressionBody::*; use self::IfExpressionBody::*;
match (discriminator, body) { match (discriminator, body) {
(Some(expr), SimpleConditional{ then_case, else_case }) => self.handle_simple_if(expr, then_case, else_case), (Some(expr), SimpleConditional{ then_case, else_case }) => self.handle_simple_if(expr, then_case, else_case),
_ => TypeError::new(format!("Complex conditionals not supported")) _ => TypeError::new("Complex conditionals not supported".to_string())
} }
} }
@ -410,7 +410,7 @@ impl<'a> TypeContext<'a> {
t_ret.clone() t_ret.clone()
}, },
Type::Arrow { .. } => return TypeError::new("Wrong length"), Type::Arrow { .. } => return TypeError::new("Wrong length"),
_ => return TypeError::new(format!("Not a function")) _ => return TypeError::new("Not a function".to_string())
}) })
} }
@ -438,26 +438,26 @@ impl<'a> TypeContext<'a> {
(Const(ref c1), Const(ref c2)) if c1 == c2 => Ok(Const(c1.clone())), //choice of c1 is arbitrary I *think* (Const(ref c1), Const(ref c2)) if c1 == c2 => Ok(Const(c1.clone())), //choice of c1 is arbitrary I *think*
(a @ Var(_), b @ Const(_)) => self.unify(b, a), (a @ Var(_), b @ Const(_)) => self.unify(b, a),
(Const(ref c1), Var(ref v2)) => { (Const(ref c1), Var(ref v2)) => {
self.unification_table.unify_var_value(v2.clone(), Some(c1.clone())) self.unification_table.unify_var_value(*v2, Some(c1.clone()))
.or_else(|_| TypeError::new(format!("Couldn't unify {:?} and {:?}", Const(c1.clone()), Var(*v2))))?; .or_else(|_| TypeError::new(format!("Couldn't unify {:?} and {:?}", Const(c1.clone()), Var(*v2))))?;
Ok(Const(c1.clone())) Ok(Const(c1.clone()))
}, },
(Var(v1), Var(v2)) => { (Var(v1), Var(v2)) => {
//TODO add occurs check //TODO add occurs check
self.unification_table.unify_var_var(v1.clone(), v2.clone()) self.unification_table.unify_var_var(v1, v2)
.or_else(|e| { .or_else(|e| {
println!("Unify error: {:?}", e); println!("Unify error: {:?}", e);
TypeError::new(format!("Two type variables {:?} and {:?} couldn't unify", v1, v2)) TypeError::new(format!("Two type variables {:?} and {:?} couldn't unify", v1, v2))
})?; })?;
Ok(Var(v1.clone())) //arbitrary decision I think Ok(Var(v1)) //arbitrary decision I think
}, },
(a, b) => TypeError::new(format!("{:?} and {:?} do not unify", a, b)), (a, b) => TypeError::new(format!("{:?} and {:?} do not unify", a, b)),
} }
} }
fn fresh_type_variable(&mut self) -> TypeVar { fn fresh_type_variable(&mut self) -> TypeVar {
let new_type_var = self.unification_table.new_key(None);
new_type_var self.unification_table.new_key(None)
} }
} }
@ -468,7 +468,7 @@ mod typechecking_tests {
macro_rules! assert_type_in_fresh_context { macro_rules! assert_type_in_fresh_context {
($string:expr, $type:expr) => { ($string:expr, $type:expr) => {
let mut tc = TypeContext::new(); let mut tc = TypeContext::new();
let ref ast = crate::util::quick_ast($string); let ast = &crate::util::quick_ast($string);
let ty = tc.typecheck(ast).unwrap(); let ty = tc.typecheck(ast).unwrap();
assert_eq!(ty, $type) assert_eq!(ty, $type)
} }

View File

@ -4,7 +4,7 @@ use std::cmp::Eq;
use std::ops::Deref; use std::ops::Deref;
pub fn deref_optional_box<T>(x: &Option<Box<T>>) -> Option<&T> { pub fn deref_optional_box<T>(x: &Option<Box<T>>) -> Option<&T> {
x.as_ref().map(|b: &Box<T>| Deref::deref(b)) x.as_ref().map(Deref::deref)
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]