Browse Source

Merge pull request #3 from aisamanra/gdritter/lint

Address lints and add autoformat check
G. D. Ritter 1 year ago
parent
commit
206d89da08
5 changed files with 14 additions and 14 deletions
  1. 2 0
      .github/workflows/rust.yml
  2. 1 1
      src/ast.rs
  3. 1 6
      src/builtins.rs
  4. 7 5
      src/interp.rs
  5. 3 2
      tools/regenerate.rs

+ 2 - 0
.github/workflows/rust.yml

@@ -20,3 +20,5 @@ jobs:
       run: cargo build --verbose
     - name: Run tests
       run: cargo test --verbose
+    - name: Check formatting
+      run: cargo fmt --check

+ 1 - 1
src/ast.rs

@@ -94,7 +94,7 @@ impl ASTArena {
                 }
             }
         }
-        let end_of_line = end_of_line.unwrap_or_else(|| src.len());
+        let end_of_line = end_of_line.unwrap_or(src.len());
 
         let mut result = format!("{:3} |", line_number);
         result.push_str(&src[start_of_line..end_of_line]);

+ 1 - 6
src/builtins.rs

@@ -14,12 +14,7 @@ pub fn builtins() -> Vec<BuiltinFunc> {
                         let mut buf = String::new();
                         let num = state.eval(*rep, env)?.as_num(&state.ast.borrow())?;
                         for _ in 0..num {
-                            buf.push_str(
-                                &state
-                                    .eval(*expr, env)?
-                                    .as_str(&state.ast.borrow())?
-                                    .to_string(),
-                            );
+                            buf.push_str(state.eval(*expr, env)?.as_str(&state.ast.borrow())?);
                         }
                         Ok(Value::Lit(Literal::Str(buf)))
                     } else {

+ 7 - 5
src/interp.rs

@@ -77,7 +77,7 @@ impl Value {
         match self {
             Value::Nil => f(""),
             Value::Lit(Literal::Str(s)) => f(s),
-            Value::Lit(Literal::Atom(s)) => f(&format!("{}", &ast[s.item])),
+            Value::Lit(Literal::Atom(s)) => f(&ast[s.item].to_string()),
             Value::Lit(Literal::Num(n)) => f(&format!("{}", n)),
             Value::Tup(values) => {
                 let mut buf = String::new();
@@ -101,6 +101,8 @@ impl Value {
     }
 }
 
+type Callback = Box<dyn Fn(&State, &[ExprRef], &Env) -> Result<Value, Error>>;
+
 /// A representation of a builtin function implemented in Rust. This
 /// will be inserted into the global scope under the name provided as
 /// `name`.
@@ -111,7 +113,7 @@ pub struct BuiltinFunc {
     pub name: &'static str,
     /// The callback here is the Rust implementation of the function,
     /// where the provided `ExprRef` is the argument to the function.
-    pub callback: Box<dyn Fn(&State, &[ExprRef], &Env) -> Result<Value, Error>>,
+    pub callback: Callback,
 }
 
 impl fmt::Debug for BuiltinFunc {
@@ -200,7 +202,7 @@ impl State {
         };
         for builtin in crate::builtins::builtins() {
             let idx = s.builtins.len();
-            let sym = s.ast.borrow_mut().add_string(&builtin.name);
+            let sym = s.ast.borrow_mut().add_string(builtin.name);
             s.root_scope
                 .borrow_mut()
                 .insert(sym, Thunk::Builtin(BuiltinRef { idx }));
@@ -222,7 +224,7 @@ impl State {
         };
         for builtin in crate::builtins::builtins() {
             let idx = s.builtins.len();
-            let sym = s.ast.borrow_mut().add_string(&builtin.name);
+            let sym = s.ast.borrow_mut().add_string(builtin.name);
             s.root_scope
                 .borrow_mut()
                 .insert(sym, Thunk::Builtin(BuiltinRef { idx }));
@@ -640,7 +642,7 @@ impl State {
                 continue;
             }
             for (scrut, pat) in scruts.iter_mut().zip(c.pats.iter()) {
-                if !self.match_pat(&pat, scrut, &mut bindings)? {
+                if !self.match_pat(pat, scrut, &mut bindings)? {
                     // if we didn't match, we don't care about any
                     // bindings we've found: simply skip it
                     continue 'cases;

+ 3 - 2
tools/regenerate.rs

@@ -2,6 +2,7 @@ use matzo::grammar;
 use matzo::interp;
 use matzo::lexer;
 
+use std::collections::btree_map::Entry;
 use std::collections::BTreeMap;
 use std::io::Write;
 
@@ -14,8 +15,8 @@ fn generate_runs(source: &str) -> Result<BTreeMap<String, String>, Box<dyn std::
         let mut out = Vec::new();
         state.run_with_writer(source, &mut out)?;
         let out = std::str::from_utf8(&out).unwrap().trim().to_string();
-        if !found_results.contains_key(&out) {
-            let _ = found_results.insert(out, seed);
+        if let Entry::Vacant(e) = found_results.entry(out) {
+            e.insert(seed);
         }
     }
     let output = found_results