From 780c36e675824e446411faf6df31770dda3a3b31 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 12 Dec 2021 12:33:22 +0800 Subject: [PATCH] Simplify if let. --- CHANGELOG.md | 2 +- src/func/call.rs | 11 ++++++----- src/optimizer.rs | 8 +++----- src/parser.rs | 40 +++++++++++++++++++--------------------- src/tokenizer.rs | 7 +++---- 5 files changed, 32 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eacb2b1d..4192ccc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/func/call.rs b/src/func/call.rs index a4f679e9..7db44596 100644 --- a/src/func/call.rs +++ b/src/func/call.rs @@ -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 { diff --git a/src/optimizer.rs b/src/optimizer.rs index 3dbbc75b..0e8040f7 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -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(_)] diff --git a/src/parser.rs b/src/parser.rs index 92e36a2b..25bc5272 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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)); diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 79018371..c039a8dc 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -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))