From 86ccb370297e1efd0f1645d634e3c5a9fc1bc7db Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 28 Feb 2023 22:33:44 +0800 Subject: [PATCH] Simplify code. --- src/ast/ast.rs | 13 ++----------- src/ast/expr.rs | 6 +----- src/eval/cache.rs | 2 +- src/eval/chaining.rs | 4 +--- src/eval/stmt.rs | 22 +++++++++++----------- src/func/call.rs | 11 ++++------- src/module/mod.rs | 26 ++++---------------------- src/module/resolvers/file.rs | 9 +-------- src/module/resolvers/stat.rs | 10 ++-------- src/parser.rs | 3 +-- 10 files changed, 28 insertions(+), 78 deletions(-) diff --git a/src/ast/ast.rs b/src/ast/ast.rs index 84057e6a..139f5634 100644 --- a/src/ast/ast.rs +++ b/src/ast/ast.rs @@ -189,11 +189,7 @@ impl AST { .as_mut() .map(|m| m.set_id(source.clone())); - if source.is_empty() { - self.source = None; - } else { - self.source = Some(source); - } + self.source = (!source.is_empty()).then(|| source); self } @@ -240,12 +236,7 @@ impl AST { #[inline(always)] pub(crate) fn set_doc(&mut self, doc: impl Into) { let doc = doc.into(); - - if doc.is_empty() { - self.doc = None; - } else { - self.doc = Some(doc.into()); - } + self.doc = (!doc.is_empty()).then(|| doc.into()); } /// Get the statements. #[cfg(not(feature = "internals"))] diff --git a/src/ast/expr.rs b/src/ast/expr.rs index 0152b72a..6add6903 100644 --- a/src/ast/expr.rs +++ b/src/ast/expr.rs @@ -248,11 +248,7 @@ impl FnCallExpr { #[inline] #[must_use] pub fn constant_args(&self) -> bool { - if self.args.is_empty() { - true - } else { - self.args.iter().all(Expr::is_constant) - } + self.args.is_empty() || self.args.iter().all(Expr::is_constant) } } diff --git a/src/eval/cache.rs b/src/eval/cache.rs index affe2b69..3e044574 100644 --- a/src/eval/cache.rs +++ b/src/eval/cache.rs @@ -63,8 +63,8 @@ impl Caches { #[inline] #[must_use] pub fn fn_resolution_cache_mut(&mut self) -> &mut FnResolutionCache { + // Push a new function resolution cache if the stack is empty if self.0.is_empty() { - // Push a new function resolution cache if the stack is empty self.push_fn_resolution_cache(); } self.0.last_mut().unwrap() diff --git a/src/eval/chaining.rs b/src/eval/chaining.rs index 09066c8c..22e8ae46 100644 --- a/src/eval/chaining.rs +++ b/src/eval/chaining.rs @@ -504,9 +504,7 @@ impl Engine { global, caches, scope, this_ptr, expr, rhs, idx_values, )?; - if !_arg_values.is_empty() { - idx_values.extend(_arg_values); - } + idx_values.extend(_arg_values); } #[cfg(not(feature = "no_object"))] diff --git a/src/eval/stmt.rs b/src/eval/stmt.rs index 0ed2bf37..9a70177d 100644 --- a/src/eval/stmt.rs +++ b/src/eval/stmt.rs @@ -304,12 +304,14 @@ impl Engine { .as_bool() .map_err(|typ| self.make_type_mismatch_err::(typ, expr.position()))?; - if guard_val && !if_block.is_empty() { - self.eval_stmt_block(global, caches, scope, this_ptr, if_block, true) - } else if !guard_val && !else_block.is_empty() { - self.eval_stmt_block(global, caches, scope, this_ptr, else_block, true) - } else { - Ok(Dynamic::UNIT) + match guard_val { + true if !if_block.is_empty() => { + self.eval_stmt_block(global, caches, scope, this_ptr, if_block, true) + } + false if !else_block.is_empty() => { + self.eval_stmt_block(global, caches, scope, this_ptr, else_block, true) + } + _ => Ok(Dynamic::UNIT), } } @@ -512,12 +514,10 @@ impl Engine { auto_restore! { scope => rewind; let orig_scope_len = scope.len(); } // Add the loop variables - let counter_index = if counter.is_empty() { - usize::MAX - } else { + let counter_index = (!counter.is_empty()).then(|| { scope.push(counter.name.clone(), 0 as INT); scope.len() - 1 - }; + }); scope.push(var_name.name.clone(), ()); let index = scope.len() - 1; @@ -526,7 +526,7 @@ impl Engine { for (x, iter_value) in iter_func(iter_obj).enumerate() { // Increment counter - if counter_index < usize::MAX { + if let Some(counter_index) = counter_index { // As the variable increments from 0, this should always work // since any overflow will first be caught below. let index_value = x as INT; diff --git a/src/func/call.rs b/src/func/call.rs index 131089fa..a47c3495 100644 --- a/src/func/call.rs +++ b/src/func/call.rs @@ -816,7 +816,8 @@ impl Engine { if call_args.is_empty() { let typ = self.map_type_name(target.type_name()); return Err(self.make_type_mismatch_err::(typ, fn_call_pos)); - } else if !call_args[0].is_fnptr() { + } + if !call_args[0].is_fnptr() { let typ = self.map_type_name(call_args[0].type_name()); return Err(self.make_type_mismatch_err::(typ, first_arg_pos)); } @@ -1257,9 +1258,7 @@ impl Engine { } // Call with blank scope - if total_args == 0 && curry.is_empty() { - // No arguments - } else { + if total_args > 0 || !curry.is_empty() { // If the first argument is a variable, and there is no curried arguments, // convert to method-call style in order to leverage potential &mut first argument and // avoid cloning the value @@ -1330,9 +1329,7 @@ impl Engine { let args = &mut FnArgsVec::with_capacity(args_expr.len()); let mut first_arg_value = None; - if args_expr.is_empty() { - // No arguments - } else { + if !args_expr.is_empty() { // See if the first argument is a variable (not namespace-qualified). // If so, convert to method-call style in order to leverage potential &mut first argument // and avoid cloning the value diff --git a/src/module/mod.rs b/src/module/mod.rs index 0bed49c0..89ef6152 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -361,13 +361,7 @@ impl Module { #[inline(always)] pub fn set_id(&mut self, id: impl Into) -> &mut Self { let id = id.into(); - - if id.is_empty() { - self.id = None; - } else { - self.id = Some(id); - } - + self.id = (!id.is_empty()).then(|| id); self } @@ -2330,21 +2324,9 @@ impl Module { self.flags .set(ModuleFlags::INDEXED_GLOBAL_FUNCTIONS, has_global_functions); - self.all_variables = if variables.is_empty() { - None - } else { - Some(variables.into()) - }; - self.all_functions = if functions.is_empty() { - None - } else { - Some(functions.into()) - }; - self.all_type_iterators = if type_iterators.is_empty() { - None - } else { - Some(type_iterators.into()) - }; + self.all_variables = (!variables.is_empty()).then(|| variables.into()); + self.all_functions = (!functions.is_empty()).then(|| functions.into()); + self.all_type_iterators = (!type_iterators.is_empty()).then(|| type_iterators.into()); self.flags |= ModuleFlags::INDEXED; } diff --git a/src/module/resolvers/file.rs b/src/module/resolvers/file.rs index ea62c5fb..4ac9a47f 100644 --- a/src/module/resolvers/file.rs +++ b/src/module/resolvers/file.rs @@ -239,14 +239,7 @@ impl FileModuleResolver { if !self.cache_enabled { return false; } - - let cache = locked_read(&self.cache); - - if cache.is_empty() { - false - } else { - cache.contains_key(path.as_ref()) - } + locked_read(&self.cache).contains_key(path.as_ref()) } /// Empty the internal cache. #[inline] diff --git a/src/module/resolvers/stat.rs b/src/module/resolvers/stat.rs index bd2b2638..04449584 100644 --- a/src/module/resolvers/stat.rs +++ b/src/module/resolvers/stat.rs @@ -73,11 +73,7 @@ impl StaticModuleResolver { #[inline(always)] #[must_use] pub fn contains_path(&self, path: &str) -> bool { - if self.0.is_empty() { - false - } else { - self.0.contains_key(path) - } + self.0.contains_key(path) } /// Get an iterator of all the [modules][Module]. #[inline] @@ -123,9 +119,7 @@ impl StaticModuleResolver { /// Existing modules of the same path name are overwritten. #[inline] pub fn merge(&mut self, other: Self) -> &mut Self { - if !other.is_empty() { - self.0.extend(other.0.into_iter()); - } + self.0.extend(other.0.into_iter()); self } } diff --git a/src/parser.rs b/src/parser.rs index 300d44f0..182adc61 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -25,7 +25,6 @@ use bitflags::bitflags; #[cfg(feature = "no_std")] use std::prelude::v1::*; use std::{ - collections::BTreeMap, convert::TryFrom, fmt, hash::{Hash, Hasher}, @@ -979,7 +978,7 @@ impl Engine { settings.pos = eat_token(input, Token::MapStart); let mut map = StaticVec::<(Ident, Expr)>::new(); - let mut template = BTreeMap::::new(); + let mut template = std::collections::BTreeMap::::new(); loop { const MISSING_RBRACE: &str = "to end this object map literal";