From 36f06b38dea14ada382d05a83d1b56c3a0fc2be3 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Tue, 19 Oct 2021 21:57:14 -0700 Subject: [PATCH] Automatically apply clippy to various util modules --- schala-lang/language/src/ast/walker.rs | 18 +++++------ schala-lang/language/src/eval/test.rs | 6 ++-- schala-lang/language/src/typechecking.rs | 38 ++++++++++++------------ schala-lang/language/src/util.rs | 2 +- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/schala-lang/language/src/ast/walker.rs b/schala-lang/language/src/ast/walker.rs index 1125b38..e818565 100644 --- a/schala-lang/language/src/ast/walker.rs +++ b/schala-lang/language/src/ast/walker.rs @@ -39,12 +39,12 @@ fn declaration(v: &mut V, decl: &Declaration) { use Declaration::*; match decl { FuncSig(sig) => { - v.signature(&sig); - signature(v, &sig); + v.signature(sig); + signature(v, sig); }, FuncDecl(sig, block) => { - v.signature(&sig); - v.block(&block); + v.signature(sig); + v.block(block); walk_block(v, block); }, TypeDecl { name, body, mutable } => v.type_declaration(name, body, *mutable), @@ -52,8 +52,8 @@ fn declaration(v: &mut V, decl: &Declaration) { Binding { name, constant, type_anno, expr } => { v.binding(name, *constant, type_anno.as_ref(), expr); v.type_annotation(type_anno.as_ref()); - v.expression(&expr); - expression(v, &expr); + v.expression(expr); + expression(v, expr); }, Impl { type_name, interface_name, block } => { v.implemention(type_name, interface_name.as_ref(), block); @@ -172,7 +172,7 @@ fn expression_kind(v: &mut V, expression_kind: &ExpressionKind) { }, IfExpression { 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); }, WhileExpression { condition, body } => v.while_expression(deref_optional_box(condition), body), @@ -195,13 +195,13 @@ fn if_expression_body(v: &mut V, body: &IfExpressionBody) { match body { SimpleConditional { then_case, else_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 } => { v.pattern(pattern); walk_pattern(v, pattern); 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) => { for arm in arms { diff --git a/schala-lang/language/src/eval/test.rs b/schala-lang/language/src/eval/test.rs index 802bea1..41bfa18 100644 --- a/schala-lang/language/src/eval/test.rs +++ b/schala-lang/language/src/eval/test.rs @@ -13,15 +13,15 @@ fn evaluate_all_outputs(input: &str) -> Vec> { let reduced = reduce(&ast, &symbol_table); let mut state = State::new(); - let all_output = state.evaluate(reduced, true); - all_output + + state.evaluate(reduced, true) } macro_rules! test_in_fresh_env { ($string:expr, $correct:expr) => { { 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())); } } diff --git a/schala-lang/language/src/typechecking.rs b/schala-lang/language/src/typechecking.rs index 3f8fa49..d743ded 100644 --- a/schala-lang/language/src/typechecking.rs +++ b/schala-lang/language/src/typechecking.rs @@ -83,13 +83,13 @@ impl TypeConst { pub fn to_string(&self) -> String { use self::TypeConst::*; match self { - Unit => format!("()"), - Nat => format!("Nat"), - Int => format!("Int"), - Float => format!("Float"), - StringT => format!("String"), - Bool => format!("Bool"), - Ordering => format!("Ordering"), + Unit => "()".to_string(), + Nat => "Nat".to_string(), + Int => "Int".to_string(), + Float => "Float".to_string(), + StringT => "String".to_string(), + Bool => "Bool".to_string(), + Ordering => "Ordering".to_string(), } } } @@ -117,7 +117,7 @@ impl Type { Const(c) => c.to_string(), Var(v) => format!("t_{}", v.0), Arrow { params, box ref ret } => { - if params.len() == 0 { + if params.is_empty() { format!("-> {}", ret.to_string()) } else { let mut buf = String::new(); @@ -128,7 +128,7 @@ impl Type { buf } }, - Compound { .. } => format!("") + Compound { .. } => "".to_string() } } @@ -256,7 +256,7 @@ impl<'a> TypeContext<'a> { use self::TypeIdentifier::*; Ok(match name { Singleton(TypeSingletonName { name,.. }) => { - match Type::from_string(&name) { + match Type::from_string(name) { Some(ty) => ty, None => return TypeError::new(format!("Unknown type name: {}", name)) } @@ -279,7 +279,7 @@ impl<'a> TypeContext<'a> { fn statement(&mut self, statement: &Statement) -> InferResult { match &statement.kind { StatementKind::Expression(e) => self.expr(e), - StatementKind::Declaration(decl) => self.decl(&decl), + StatementKind::Declaration(decl) => self.decl(decl), StatementKind::Import(_) => Ok(ty!(Unit)), StatementKind::Module(_) => Ok(ty!(Unit)), } @@ -361,7 +361,7 @@ impl<'a> TypeContext<'a> { use self::IfExpressionBody::*; match (discriminator, body) { (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() }, 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* (a @ Var(_), b @ Const(_)) => self.unify(b, a), (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))))?; Ok(Const(c1.clone())) }, (Var(v1), Var(v2)) => { //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| { println!("Unify error: {:?}", e); 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)), } } 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 { ($string:expr, $type:expr) => { 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(); assert_eq!(ty, $type) } diff --git a/schala-lang/language/src/util.rs b/schala-lang/language/src/util.rs index 855c87a..0543759 100644 --- a/schala-lang/language/src/util.rs +++ b/schala-lang/language/src/util.rs @@ -4,7 +4,7 @@ use std::cmp::Eq; use std::ops::Deref; pub fn deref_optional_box(x: &Option>) -> Option<&T> { - x.as_ref().map(|b: &Box| Deref::deref(b)) + x.as_ref().map(Deref::deref) } #[derive(Default, Debug)]