Use then/then_some to simplify.

This commit is contained in:
Stephen Chung 2023-02-21 16:36:57 +08:00
parent 00f2b07d38
commit 129a5c6e86
6 changed files with 31 additions and 63 deletions

View File

@ -139,12 +139,8 @@ impl Engine {
// Built-in found
auto_restore! { let orig_level = global.level; global.level += 1 }
let context = if need_context {
let source = global.source();
Some((self, op_x_str, source, &*global, pos).into())
} else {
None
};
let context = need_context
.then_some((self, op_x_str, global.source(), &*global, pos).into());
return func(context, args).map(|_| ());
}
}

View File

@ -398,11 +398,9 @@ impl Engine {
let is_method = func.is_method();
let src = source.as_ref().map(|s| s.as_str());
let context = if func.has_context() {
Some((self, name, src, &*global, pos).into())
} else {
None
};
let context = func
.has_context()
.then_some((self, name, src, &*global, pos).into());
let mut _result = if let Some(f) = func.get_plugin_fn() {
if !f.is_pure() && !args.is_empty() && args[0].is_read_only() {
@ -1460,11 +1458,9 @@ impl Engine {
Some(f) if f.is_plugin_fn() => {
let f = f.get_plugin_fn().expect("plugin function");
let context = if f.has_context() {
Some((self, fn_name, module.id(), &*global, pos).into())
} else {
None
};
let context = f
.has_context()
.then_some((self, fn_name, module.id(), &*global, pos).into());
if !f.is_pure() && !args.is_empty() && args[0].is_read_only() {
Err(ERR::ErrorNonPureMethodCallOnConstant(fn_name.to_string(), pos).into())
} else {
@ -1475,18 +1471,16 @@ impl Engine {
Some(f) if f.is_native() => {
let func = f.get_native_fn().expect("native function");
let context = if f.has_context() {
Some((self, fn_name, module.id(), &*global, pos).into())
} else {
None
};
let context = f
.has_context()
.then_some((self, fn_name, module.id(), &*global, pos).into());
func(context, args).and_then(|r| self.check_data_size(r, pos))
}
Some(f) => unreachable!("unknown function type: {:?}", f),
None => {
let sig = if namespace.is_empty() {
None => Err(ERR::ErrorFunctionNotFound(
if namespace.is_empty() {
self.gen_fn_call_signature(fn_name, args)
} else {
format!(
@ -1494,10 +1488,10 @@ impl Engine {
crate::engine::NAMESPACE_SEPARATOR,
self.gen_fn_call_signature(fn_name, args)
)
};
Err(ERR::ErrorFunctionNotFound(sig, pos).into())
}
},
pos,
)
.into()),
}
}
@ -1605,11 +1599,8 @@ impl Engine {
// Built-in found
auto_restore! { let orig_level = global.level; global.level += 1 }
let context = if need_context {
Some((self, name.as_str(), None, &*global, pos).into())
} else {
None
};
let context =
need_context.then_some((self, name.as_str(), None, &*global, pos).into());
return func(context, operands);
}

View File

@ -86,7 +86,7 @@ impl Engine {
let orig_fn_resolution_caches_len = caches.fn_resolution_caches_len();
#[cfg(not(feature = "no_module"))]
let orig_constants = if let Some(environ) = _environ {
let orig_constants = _environ.map(|environ| {
let EncapsulatedEnviron {
lib,
imports,
@ -100,10 +100,8 @@ impl Engine {
global.lib.push(lib.clone());
Some(mem::replace(&mut global.constants, constants.clone()))
} else {
None
};
mem::replace(&mut global.constants, constants.clone())
});
#[cfg(feature = "debugging")]
if self.is_debugger_registered() {

View File

@ -1137,11 +1137,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
_ if x.args.len() == 2 && x.op_token.is_some() && (state.engine.fast_operators() || !state.engine.has_native_fn_override(x.hashes.native(), &arg_types)) => {
if let Some(result) = get_builtin_binary_op_fn(x.op_token.as_ref().unwrap(), &arg_values[0], &arg_values[1])
.and_then(|(f, ctx)| {
let context = if ctx {
Some((state.engine, x.name.as_str(), None, &state.global, *pos).into())
} else {
None
};
let context = ctx.then_some((state.engine, x.name.as_str(), None, &state.global, *pos).into());
let (first, second) = arg_values.split_first_mut().unwrap();
(f)(context, &mut [ first, &mut second[0] ]).ok()
}) {

View File

@ -215,11 +215,7 @@ impl<'e, 's> ParseState<'e, 's> {
self.allow_capture = true;
}
let index = if hit_barrier {
None
} else {
NonZeroUsize::new(index)
};
let index = (!hit_barrier).then(|| NonZeroUsize::new(index)).flatten();
(index, is_func_name)
}
@ -2357,12 +2353,8 @@ impl Engine {
let op = op_token.to_string();
let hash = calc_fn_hash(None, &op, 2);
let is_valid_script_function = is_valid_function_name(&op);
let operator_token = if is_valid_script_function {
None
} else {
Some(op_token.clone())
};
let native_only = !is_valid_function_name(&op);
let operator_token = native_only.then_some(op_token.clone());
let mut args = FnArgsVec::new_const();
args.push(root);
@ -2446,10 +2438,10 @@ impl Engine {
.and_then(|m| m.get(s.as_str()))
.map_or(false, Option::is_some) =>
{
op_base.hashes = if is_valid_script_function {
FnCallHashes::from_hash(calc_fn_hash(None, &s, 2))
} else {
op_base.hashes = if native_only {
FnCallHashes::from_native_only(calc_fn_hash(None, &s, 2))
} else {
FnCallHashes::from_hash(calc_fn_hash(None, &s, 2))
};
op_base.into_fn_call_expr(pos)
}
@ -3085,8 +3077,7 @@ impl Engine {
let (id, id_pos) = parse_var_name(input)?;
let (alias, alias_pos) = if match_token(input, Token::As).0 {
let (name, pos) = parse_var_name(input)?;
(Some(name), pos)
parse_var_name(input).map(|(name, pos)| (Some(name), pos))?
} else {
(None, Position::NONE)
};

View File

@ -1229,11 +1229,7 @@ fn get_next_token_inner(
// Still inside a comment?
if state.comment_level > 0 {
let start_pos = *pos;
let mut comment = if state.include_comments {
Some(String::new())
} else {
None
};
let mut comment = state.include_comments.then_some(String::new());
state.comment_level =
scan_block_comment(stream, state.comment_level, pos, comment.as_mut());