diff --git a/src/main.rs b/src/main.rs index e467b2f..720c13a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -506,12 +506,12 @@ type Matches<'src> = HashMap; pub fn matches<'src>(vars: &[Symbol], lhs: &Sexp, expr: &'src Sexp) -> Option> { match (lhs, expr) { (Sexp::Atom(a), Sexp::Atom(b)) => { - if a == b { - Some(HashMap::with_capacity(0)) - } else if vars.contains(a) { + if vars.contains(a) { let mut out = HashMap::with_capacity(1); out.insert(*a, expr); Some(out) + } else if a == b { + Some(HashMap::with_capacity(0)) } else { None } @@ -527,7 +527,7 @@ pub fn matches<'src>(vars: &[Symbol], lhs: &Sexp, expr: &'src Sexp) -> Option None, (Sexp::List(xs), Sexp::List(ys)) => { - if xs == ys { + if xs.is_empty() && ys.is_empty() { Some(HashMap::with_capacity(0)) } else if xs.len() == ys.len() { xs.iter() @@ -535,7 +535,7 @@ pub fn matches<'src>(vars: &[Symbol], lhs: &Sexp, expr: &'src Sexp) -> Option None, - (Some(a), Some(b)) => merge_matches(vars, a, b), + (Some(a), Some(b)) => merge_matches(vars, &a, &b), }) .unwrap_or(None) } else { @@ -545,10 +545,11 @@ pub fn matches<'src>(vars: &[Symbol], lhs: &Sexp, expr: &'src Sexp) -> Option( vars: &[Symbol], - a: Matches<'src>, - b: Matches<'src>, + a: &Matches<'src>, + b: &Matches<'src>, ) -> Option> { let mut out = HashMap::with_capacity(vars.len()); @@ -579,6 +580,7 @@ pub fn merge_matches<'src>( // } // } +#[must_use] pub fn rw(rule: &Rule, sexp: &Sexp) -> Sexp { if rule.lhs() == rule.rhs() { return sexp.clone();