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 `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 `into_array` and `into_typed_array` for `Dynamic`.
* Added `FnPtr::call` and `FnPtr::call_within_context` to simplify calling a function pointer. * 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`. * `Expression` now derefs to `Expr`.
Deprecated and Gated API's Deprecated and Gated API's

View File

@ -577,11 +577,12 @@ impl Engine {
} }
let mut empty_scope; let mut empty_scope;
let scope = if let Some(scope) = _scope { let scope = match _scope {
scope Some(scope) => scope,
} else { None => {
empty_scope = Scope::new(); empty_scope = Scope::new();
&mut empty_scope &mut empty_scope
}
}; };
let result = if _is_method_call { 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) => if reduce_return && !options.contains(AST_OPTION_BREAK_OUT) =>
{ {
state.set_dirty(); state.set_dirty();
*statements.last_mut().expect(">= 2 elements") = if let Some(expr) = expr { *statements.last_mut().expect(">= 2 elements") = expr
Stmt::Expr(mem::take(expr)) .as_mut()
} else { .map_or_else(|| Stmt::Noop(pos), |e| Stmt::Expr(mem::take(e)));
Stmt::Noop(pos)
};
} }
// { ...; stmt; noop } -> done // { ...; stmt; noop } -> done
[.., ref second_last_stmt, Stmt::Noop(_)] [.., ref second_last_stmt, Stmt::Noop(_)]

View File

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