Replace unwrap with expect.

This commit is contained in:
Stephen Chung
2021-05-22 19:14:24 +08:00
parent 1545b602a7
commit cc3e2d79a5
18 changed files with 600 additions and 323 deletions

View File

@@ -49,7 +49,12 @@ impl<'a> ArgBackup<'a> {
///
/// This method blindly casts a reference to another lifetime, which saves allocation and string cloning.
///
/// If `restore_first_arg` is called before the end of the scope, the shorter lifetime will not leak.
/// As long as `restore_first_arg` is called before the end of the scope, the shorter lifetime
/// will not leak.
///
/// # Panics
///
/// Panics when `args` is empty.
#[inline(always)]
fn change_first_arg_to_copy(&mut self, args: &mut FnCallArgs<'a>) {
// Clone the original value.
@@ -239,7 +244,8 @@ impl Engine {
FnResolutionCacheEntry { func, source: None }
})
} else {
let (first, second) = args.split_first().unwrap();
let (first, second) = args.split_first()
.expect("never fails because an op-assignment must have two arguments");
get_builtin_op_assignment_fn(fn_name, *first, second[0]).map(
|f| {
@@ -257,7 +263,9 @@ impl Engine {
// Try all permutations with `Dynamic` wildcards
None => {
let hash_params = calc_fn_params_hash(
args.as_ref().unwrap().iter().enumerate().map(|(i, a)| {
args.as_ref().expect("never fails because there are no permutations if there are no arguments")
.iter().enumerate().map(|(i, a)|
{
let mask = 1usize << (num_args - i - 1);
if bitmask & mask != 0 {
// Replace with `Dynamic`
@@ -646,8 +654,12 @@ impl Engine {
crate::engine::KEYWORD_IS_DEF_FN
if args.len() == 2 && args[0].is::<FnPtr>() && args[1].is::<crate::INT>() =>
{
let fn_name = &*args[0].read_lock::<ImmutableString>().unwrap();
let num_params = args[1].as_int().unwrap();
let fn_name = &*args[0]
.read_lock::<ImmutableString>()
.expect("never fails because `args[0]` is `FnPtr`");
let num_params = args[1]
.as_int()
.expect("never fails because `args[1]` is `INT`");
return Ok((
if num_params < 0 {
@@ -704,11 +716,7 @@ impl Engine {
// Scripted function call?
#[cfg(not(feature = "no_function"))]
let hash_script = if hash.is_native_only() {
None
} else {
Some(hash.script_hash())
};
let hash_script = hash.script_hash();
#[cfg(not(feature = "no_function"))]
if let Some(f) = hash_script.and_then(|hash| {
@@ -743,7 +751,9 @@ impl Engine {
let result = if _is_method {
// Method call of script function - map first argument to `this`
let (first, rest) = args.split_first_mut().unwrap();
let (first, rest) = args
.split_first_mut()
.expect("never fails because a method call always has a first parameter");
let orig_source = state.source.take();
state.source = source;
@@ -906,7 +916,9 @@ impl Engine {
let (result, updated) = match fn_name {
KEYWORD_FN_PTR_CALL if obj.is::<FnPtr>() => {
// FnPtr call
let fn_ptr = obj.read_lock::<FnPtr>().unwrap();
let fn_ptr = obj
.read_lock::<FnPtr>()
.expect("never fails because `obj` is `FnPtr`");
// Redirect function name
let fn_name = fn_ptr.fn_name();
let args_len = call_args.len() + fn_ptr.curry().len();
@@ -970,7 +982,9 @@ impl Engine {
));
}
let fn_ptr = obj.read_lock::<FnPtr>().unwrap();
let fn_ptr = obj
.read_lock::<FnPtr>()
.expect("never fails because `obj` is `FnPtr`");
// Curry call
Ok((
@@ -1041,7 +1055,10 @@ impl Engine {
// Propagate the changed value back to the source if necessary
if updated {
target.propagate_changed_value();
target.propagate_changed_value().map_err(|mut err| {
err.set_position(pos);
err
})?;
}
Ok((result, updated))
@@ -1324,9 +1341,8 @@ impl Engine {
} else {
// Turn it into a method call only if the object is not shared and not a simple value
is_ref = true;
once(target.take_ref().unwrap())
.chain(arg_values.iter_mut())
.collect()
let obj_ref = target.take_ref().expect("never fails because `target` is a reference if it is not a value and not shared");
once(obj_ref).chain(arg_values.iter_mut()).collect()
};
} else {
// func(..., ...)
@@ -1357,7 +1373,7 @@ impl Engine {
state: &mut State,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
namespace: Option<&NamespaceRef>,
namespace: &NamespaceRef,
fn_name: &str,
args_expr: &[Expr],
literal_args: &[(Dynamic, Position)],
@@ -1365,7 +1381,6 @@ impl Engine {
pos: Position,
level: usize,
) -> RhaiResult {
let namespace = namespace.unwrap();
let mut arg_values: StaticVec<_>;
let mut first_arg_value = None;
let mut args: StaticVec<_>;
@@ -1410,11 +1425,12 @@ impl Engine {
arg_values[0] = target.take_or_clone().flatten();
args = arg_values.iter_mut().collect();
} else {
let (first, rest) = arg_values.split_first_mut().unwrap();
let (first, rest) = arg_values
.split_first_mut()
.expect("never fails because the arguments list is not empty");
first_arg_value = Some(first);
args = once(target.take_ref().unwrap())
.chain(rest.iter_mut())
.collect();
let obj_ref = target.take_ref().expect("never fails because `target` is a reference if it is not a value and not shared");
args = once(obj_ref).chain(rest.iter_mut()).collect();
}
} else {
// func(..., ...) or func(mod::x, ...)