Repository: aptos-labs/aptos-core
File: third_party/move/move-compiler-v2/src/env_pipeline/ast_simplifier.rs (Rust)

Code before:
        }
        result
    }

    fn rewrite_call(&mut self, id: NodeId, oper: &Operation, args: &[Exp]) -> Option<Exp> {
        self.constant_folder
            .rewrite_call(id, oper, args)
            .map(|exp| self.expand_tuple(exp))
            .or_else(|| {
                // Not completely a constant.
                if *oper == Operation::Cast && args.len() == 1 {
                    self.try_collapse_cast(id, &args[0])
                } else {
                    // TODO(later): match some more interesting expressions.
                    // e.g., ((x + c1) + c2) -> (x + (c1 + c2))
                    None
                }
            })
    }

    fn rewrite_enter_block_scope(
        &mut self,
        id: NodeId,
        pat: &Pattern,
        binding: &Option<Exp>,
    ) -> Option<Pattern> {
        let mut new_binding = Vec::new();
        if let Some(exp) = binding {
            for (var, opt_new_binding_exp) in pat.vars_and_exprs(exp) {
                if self.possibly_modified_variables.contains(&(var, Some(id))) {
                    // Ignore RHS, mark this variable as possibly modified.
                    new_binding.push((var, None));
                } else {
                    // Try to evaluate opt_new_binding_exp as a constant/var.
                    // If unrepresentable as a Value, returns None.
                    new_binding.push((var, self.exp_to_simple_value(opt_new_binding_exp)));
                }
            }
        } else {
            // Body with no bindings, values are Uninitialized.
            for (_, var) in pat.vars() {
                if self.possibly_modified_variables.contains(&(var, Some(id))) {
                    // Ignore RHS, mark this variable as possibly modified.
                    new_binding.push((var, None));
                } else {
                    new_binding.push((var, Some(SimpleValue::Uninitialized)))
                }
            }
        }
        // Newly bound vars block any prior values
        self.rewrite_enter_scope(id, pat.vars().iter());

Review comment (suggestion):
Maybe some code like below is easier to read?

```Rust
if arms.len() == 2
    && matches!(
        (&arms[0], &arms[1]),
        (
            MatchArm {
                pattern: Pattern::Struct(_, qid, Some(variant), args),
                condition: None,
                body,
                ..
            },
            MatchArm {
                pattern: Pattern::Wildcard(_),
                condition: None,
                body: body2,
                ..
            }
        ) if args.iter().all(|p| matches!(p, Pattern::Wildcard(_)))
            && is_bool(body, true)
            && is_bool(body2, false)
    )
{
    Some(ExpData::Call(
        id,
        Operation::TestVariants(qid.module_id, qid.id, vec![*variant]),
        vec![disc.clone()],
    ).into_exp())
} else {
    None
}
```

Code after:
        }
        result
    }

    fn rewrite_call(&mut self, id: NodeId, oper: &Operation, args: &[Exp]) -> Option<Exp> {
        self.constant_folder
            .rewrite_call(id, oper, args)
            .map(|exp| self.expand_tuple(exp))
            .or_else(|| {
                // Not completely a constant.
                if *oper == Operation::Cast && args.len() == 1 {
                    self.try_collapse_cast(id, &args[0])
                } else {
                    // TODO(later): match some more interesting expressions.
                    // e.g., ((x + c1) + c2) -> (x + (c1 + c2))
                    None
                }
            })
    }

    fn rewrite_match(&mut self, id: NodeId, disc: &Exp, arms: &[MatchArm]) -> Option<Exp> {
        // match (e) { V(_) => true, _ => false } ==> e is V
        let is_bool = |e: &Exp, test: bool| -> bool {
            matches!(e.as_ref(), ExpData::Value(_, Value::Bool(v)) if test == *v)
        };
        if arms.len() == 2
            && let MatchArm {
                loc: _,
                pattern: Pattern::Struct(_, qid, Some(variant), args),
                condition: None,
                body,
            } = &arms[0]
            && args.iter().all(|p| matches!(p, Pattern::Wildcard(_)))
            && is_bool(body, true)
            && let MatchArm {
                loc: _,
                pattern: Pattern::Wildcard(_),
                condition: None,
                body,
            } = &arms[1]
            && is_bool(body, false)
        {
            Some(
                ExpData::Call(
                    id,
                    Operation::TestVariants(qid.module_id, qid.id, vec![*variant]),
                    vec![disc.clone()],
                )
                .into_exp(),
            )
        } else {