Simplify if let.

This commit is contained in:
Stephen Chung 2021-12-12 12:33:22 +08:00
parent 3a5495a65c
commit 780c36e675
5 changed files with 32 additions and 36 deletions

View File

@ -28,7 +28,7 @@ Enhancements
* Added `Engine::register_type_with_name_raw` to register a custom type based on a fully-qualified type path.
* Added `into_array` and `into_typed_array` for `Dynamic`.
* Added `FnPtr::call` and `FnPtr::call_within_context` to simplify calling a function pointer.
* A function's hashes are included in its JSON metadata to assist in debugging. Each function's `hashBase` field in the JSON object should map directly to the pre-calculated hash in the function call.
* A function's hashes are included in its JSON metadata to assist in debugging. Each function's `baseHash` field in the JSON object should map directly to the pre-calculated hash in the function call.
* `Expression` now derefs to `Expr`.
Deprecated and Gated API's

View File

@ -577,11 +577,12 @@ impl Engine {
}
let mut empty_scope;
let scope = if let Some(scope) = _scope {
scope
} else {
empty_scope = Scope::new();
&mut empty_scope
let scope = match _scope {
Some(scope) => scope,
None => {
empty_scope = Scope::new();
&mut empty_scope
}
};
let result = if _is_method_call {

View File

@ -304,11 +304,9 @@ fn optimize_stmt_block(
if reduce_return && !options.contains(AST_OPTION_BREAK_OUT) =>
{
state.set_dirty();
*statements.last_mut().expect(">= 2 elements") = if let Some(expr) = expr {
Stmt::Expr(mem::take(expr))
} else {
Stmt::Noop(pos)
};
*statements.last_mut().expect(">= 2 elements") = expr
.as_mut()
.map_or_else(|| Stmt::Noop(pos), |e| Stmt::Expr(mem::take(e)));
}
// { ...; stmt; noop } -> done
[.., ref second_last_stmt, Stmt::Noop(_)]

View File

@ -1048,19 +1048,18 @@ fn parse_switch(
};
let hash = if let Some(expr) = expr {
if let Some(value) = expr.get_literal_value() {
let hasher = &mut get_hasher();
value.hash(hasher);
let hash = hasher.finish();
let value = expr.get_literal_value().ok_or_else(|| {
PERR::ExprExpected("a literal".to_string()).into_err(expr.position())
})?;
let hasher = &mut get_hasher();
value.hash(hasher);
let hash = hasher.finish();
if table.contains_key(&hash) {
return Err(PERR::DuplicatedSwitchCase.into_err(expr.position()));
}
Some(hash)
} else {
return Err(PERR::ExprExpected("a literal".to_string()).into_err(expr.position()));
if table.contains_key(&hash) {
return Err(PERR::DuplicatedSwitchCase.into_err(expr.position()));
}
Some(hash)
} else {
None
};
@ -1081,11 +1080,12 @@ fn parse_switch(
let need_comma = !stmt.is_self_terminated();
def_stmt = if let Some(hash) = hash {
table.insert(hash, (condition, stmt.into()).into());
None
} else {
Some(stmt.into())
def_stmt = match hash {
Some(hash) => {
table.insert(hash, (condition, stmt.into()).into());
None
}
None => Some(stmt.into()),
};
match input.peek().expect(NEVER_ENDS) {
@ -2461,14 +2461,12 @@ fn parse_for(
let prev_stack_len = state.stack.len();
let counter_var = if let Some(name) = counter_name {
let counter_var = counter_name.map(|name| {
let name = state.get_identifier(name);
let pos = counter_pos.expect("`Some`");
state.stack.push((name.clone(), AccessMode::ReadWrite));
Some(Ident { name, pos })
} else {
None
};
Ident { name, pos }
});
let loop_var = state.get_identifier(name);
state.stack.push((loop_var.clone(), AccessMode::ReadWrite));

View File

@ -2223,10 +2223,9 @@ impl<'a> Iterator for TokenIterator<'a> {
};
// Run the mapper, if any
let token = if let Some(map_func) = self.token_mapper {
map_func(token, pos, &self.state)
} else {
token
let token = match self.token_mapper {
Some(map_func) => map_func(token, pos, &self.state),
None => token,
};
Some((token, pos))