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()
.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<crate::SmartString>) {
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"))]

View File

@ -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)
}
}

View File

@ -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()

View File

@ -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"))]

View File

@ -304,12 +304,14 @@ impl Engine {
.as_bool()
.map_err(|typ| self.make_type_mismatch_err::<bool>(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;

View File

@ -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::<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());
return Err(self.make_type_mismatch_err::<FnPtr>(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

View File

@ -361,13 +361,7 @@ impl Module {
#[inline(always)]
pub fn set_id(&mut self, id: impl Into<ImmutableString>) -> &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;
}

View File

@ -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]

View File

@ -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
}
}

View File

@ -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::<Identifier, crate::Dynamic>::new();
let mut template = std::collections::BTreeMap::<Identifier, crate::Dynamic>::new();
loop {
const MISSING_RBRACE: &str = "to end this object map literal";