Simplify code.

This commit is contained in:
Stephen Chung 2023-02-28 22:33:44 +08:00
parent 9a5221f60d
commit 86ccb37029
10 changed files with 28 additions and 78 deletions

View File

@ -189,11 +189,7 @@ impl AST {
.as_mut() .as_mut()
.map(|m| m.set_id(source.clone())); .map(|m| m.set_id(source.clone()));
if source.is_empty() { self.source = (!source.is_empty()).then(|| source);
self.source = None;
} else {
self.source = Some(source);
}
self self
} }
@ -240,12 +236,7 @@ impl AST {
#[inline(always)] #[inline(always)]
pub(crate) fn set_doc(&mut self, doc: impl Into<crate::SmartString>) { pub(crate) fn set_doc(&mut self, doc: impl Into<crate::SmartString>) {
let doc = doc.into(); let doc = doc.into();
self.doc = (!doc.is_empty()).then(|| doc.into());
if doc.is_empty() {
self.doc = None;
} else {
self.doc = Some(doc.into());
}
} }
/// Get the statements. /// Get the statements.
#[cfg(not(feature = "internals"))] #[cfg(not(feature = "internals"))]

View File

@ -248,11 +248,7 @@ impl FnCallExpr {
#[inline] #[inline]
#[must_use] #[must_use]
pub fn constant_args(&self) -> bool { pub fn constant_args(&self) -> bool {
if self.args.is_empty() { self.args.is_empty() || self.args.iter().all(Expr::is_constant)
true
} else {
self.args.iter().all(Expr::is_constant)
}
} }
} }

View File

@ -63,8 +63,8 @@ impl Caches {
#[inline] #[inline]
#[must_use] #[must_use]
pub fn fn_resolution_cache_mut(&mut self) -> &mut FnResolutionCache { 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() { if self.0.is_empty() {
// Push a new function resolution cache if the stack is empty
self.push_fn_resolution_cache(); self.push_fn_resolution_cache();
} }
self.0.last_mut().unwrap() self.0.last_mut().unwrap()

View File

@ -504,9 +504,7 @@ impl Engine {
global, caches, scope, this_ptr, expr, rhs, idx_values, 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"))] #[cfg(not(feature = "no_object"))]

View File

@ -304,12 +304,14 @@ impl Engine {
.as_bool() .as_bool()
.map_err(|typ| self.make_type_mismatch_err::<bool>(typ, expr.position()))?; .map_err(|typ| self.make_type_mismatch_err::<bool>(typ, expr.position()))?;
if guard_val && !if_block.is_empty() { match guard_val {
self.eval_stmt_block(global, caches, scope, this_ptr, if_block, true) true if !if_block.is_empty() => {
} else if !guard_val && !else_block.is_empty() { self.eval_stmt_block(global, caches, scope, this_ptr, if_block, true)
self.eval_stmt_block(global, caches, scope, this_ptr, else_block, true) }
} else { false if !else_block.is_empty() => {
Ok(Dynamic::UNIT) 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(); } auto_restore! { scope => rewind; let orig_scope_len = scope.len(); }
// Add the loop variables // Add the loop variables
let counter_index = if counter.is_empty() { let counter_index = (!counter.is_empty()).then(|| {
usize::MAX
} else {
scope.push(counter.name.clone(), 0 as INT); scope.push(counter.name.clone(), 0 as INT);
scope.len() - 1 scope.len() - 1
}; });
scope.push(var_name.name.clone(), ()); scope.push(var_name.name.clone(), ());
let index = scope.len() - 1; let index = scope.len() - 1;
@ -526,7 +526,7 @@ impl Engine {
for (x, iter_value) in iter_func(iter_obj).enumerate() { for (x, iter_value) in iter_func(iter_obj).enumerate() {
// Increment counter // Increment counter
if counter_index < usize::MAX { if let Some(counter_index) = counter_index {
// As the variable increments from 0, this should always work // As the variable increments from 0, this should always work
// since any overflow will first be caught below. // since any overflow will first be caught below.
let index_value = x as INT; let index_value = x as INT;

View File

@ -816,7 +816,8 @@ impl Engine {
if call_args.is_empty() { if call_args.is_empty() {
let typ = self.map_type_name(target.type_name()); let typ = self.map_type_name(target.type_name());
return Err(self.make_type_mismatch_err::<FnPtr>(typ, fn_call_pos)); return Err(self.make_type_mismatch_err::<FnPtr>(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()); let typ = self.map_type_name(call_args[0].type_name());
return Err(self.make_type_mismatch_err::<FnPtr>(typ, first_arg_pos)); return Err(self.make_type_mismatch_err::<FnPtr>(typ, first_arg_pos));
} }
@ -1257,9 +1258,7 @@ impl Engine {
} }
// Call with blank scope // Call with blank scope
if total_args == 0 && curry.is_empty() { if total_args > 0 || !curry.is_empty() {
// No arguments
} else {
// If the first argument is a variable, and there is no curried arguments, // 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 // convert to method-call style in order to leverage potential &mut first argument and
// avoid cloning the value // avoid cloning the value
@ -1330,9 +1329,7 @@ impl Engine {
let args = &mut FnArgsVec::with_capacity(args_expr.len()); let args = &mut FnArgsVec::with_capacity(args_expr.len());
let mut first_arg_value = None; let mut first_arg_value = None;
if args_expr.is_empty() { if !args_expr.is_empty() {
// No arguments
} else {
// See if the first argument is a variable (not namespace-qualified). // 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 // If so, convert to method-call style in order to leverage potential &mut first argument
// and avoid cloning the value // and avoid cloning the value

View File

@ -361,13 +361,7 @@ impl Module {
#[inline(always)] #[inline(always)]
pub fn set_id(&mut self, id: impl Into<ImmutableString>) -> &mut Self { pub fn set_id(&mut self, id: impl Into<ImmutableString>) -> &mut Self {
let id = id.into(); let id = id.into();
self.id = (!id.is_empty()).then(|| id);
if id.is_empty() {
self.id = None;
} else {
self.id = Some(id);
}
self self
} }
@ -2330,21 +2324,9 @@ impl Module {
self.flags self.flags
.set(ModuleFlags::INDEXED_GLOBAL_FUNCTIONS, has_global_functions); .set(ModuleFlags::INDEXED_GLOBAL_FUNCTIONS, has_global_functions);
self.all_variables = if variables.is_empty() { self.all_variables = (!variables.is_empty()).then(|| variables.into());
None self.all_functions = (!functions.is_empty()).then(|| functions.into());
} else { self.all_type_iterators = (!type_iterators.is_empty()).then(|| type_iterators.into());
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.flags |= ModuleFlags::INDEXED; self.flags |= ModuleFlags::INDEXED;
} }

View File

@ -239,14 +239,7 @@ impl FileModuleResolver {
if !self.cache_enabled { if !self.cache_enabled {
return false; return false;
} }
locked_read(&self.cache).contains_key(path.as_ref())
let cache = locked_read(&self.cache);
if cache.is_empty() {
false
} else {
cache.contains_key(path.as_ref())
}
} }
/// Empty the internal cache. /// Empty the internal cache.
#[inline] #[inline]

View File

@ -73,11 +73,7 @@ impl StaticModuleResolver {
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub fn contains_path(&self, path: &str) -> bool { pub fn contains_path(&self, path: &str) -> bool {
if self.0.is_empty() { self.0.contains_key(path)
false
} else {
self.0.contains_key(path)
}
} }
/// Get an iterator of all the [modules][Module]. /// Get an iterator of all the [modules][Module].
#[inline] #[inline]
@ -123,9 +119,7 @@ impl StaticModuleResolver {
/// Existing modules of the same path name are overwritten. /// Existing modules of the same path name are overwritten.
#[inline] #[inline]
pub fn merge(&mut self, other: Self) -> &mut Self { 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 self
} }
} }

View File

@ -25,7 +25,6 @@ use bitflags::bitflags;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{ use std::{
collections::BTreeMap,
convert::TryFrom, convert::TryFrom,
fmt, fmt,
hash::{Hash, Hasher}, hash::{Hash, Hasher},
@ -979,7 +978,7 @@ impl Engine {
settings.pos = eat_token(input, Token::MapStart); settings.pos = eat_token(input, Token::MapStart);
let mut map = StaticVec::<(Ident, Expr)>::new(); let mut map = StaticVec::<(Ident, Expr)>::new();
let mut template = BTreeMap::<Identifier, crate::Dynamic>::new(); let mut template = std::collections::BTreeMap::<Identifier, crate::Dynamic>::new();
loop { loop {
const MISSING_RBRACE: &str = "to end this object map literal"; const MISSING_RBRACE: &str = "to end this object map literal";