Clean up more clippy.

This commit is contained in:
Stephen Chung
2022-07-27 18:04:59 +08:00
parent 39dee556c4
commit 2f948a784c
47 changed files with 412 additions and 377 deletions

View File

@@ -287,7 +287,7 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option<Fn
return match op {
OP_CONTAINS => Some(|_, args| {
let blob = &*args[0].read_lock::<Blob>().expect(BUILTIN);
let x = (args[1].as_int().expect("`INT`") & 0x000000ff) as u8;
let x = (args[1].as_int().expect("`INT`") & 0x0000_00ff) as u8;
Ok((!blob.is_empty() && blob.contains(&x)).into())
}),
_ => None,
@@ -517,16 +517,14 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option<Fn
let blob1 = &*args[0].read_lock::<Blob>().expect(BUILTIN);
let blob2 = &*args[1].read_lock::<Blob>().expect(BUILTIN);
Ok(Dynamic::from_blob(if !blob2.is_empty() {
if blob1.is_empty() {
blob2.clone()
} else {
let mut blob = blob1.clone();
blob.extend(blob2);
blob
}
} else {
Ok(Dynamic::from_blob(if blob2.is_empty() {
blob1.clone()
} else if blob1.is_empty() {
blob2.clone()
} else {
let mut blob = blob1.clone();
blob.extend(blob2);
blob
}))
}),
"==" => Some(impl_op!(Blob == Blob)),

View File

@@ -140,10 +140,10 @@ impl Engine {
#[cfg(not(feature = "no_module"))]
let (ns, sep) = (
namespace.to_string(),
if !namespace.is_empty() {
crate::tokenizer::Token::DoubleColon.literal_syntax()
} else {
if namespace.is_empty() {
""
} else {
crate::tokenizer::Token::DoubleColon.literal_syntax()
},
);
#[cfg(feature = "no_module")]
@@ -169,7 +169,7 @@ impl Engine {
///
/// Search order:
/// 1) AST - script functions in the AST
/// 2) Global namespace - functions registered via Engine::register_XXX
/// 2) Global namespace - functions registered via `Engine::register_XXX`
/// 3) Global registered modules - packages
/// 4) Imported modules - functions marked with global namespace
/// 5) Static registered modules
@@ -280,16 +280,7 @@ impl Engine {
}
return args.and_then(|args| {
if !is_op_assignment {
get_builtin_binary_op_fn(fn_name, args[0], args[1]).map(|f| {
FnResolutionCacheEntry {
func: CallableFunction::from_method(
Box::new(f) as Box<FnAny>
),
source: None,
}
})
} else {
if is_op_assignment {
let (first_arg, rest_args) = args.split_first().unwrap();
get_builtin_op_assignment_fn(fn_name, *first_arg, rest_args[0]).map(
@@ -300,6 +291,15 @@ impl Engine {
source: None,
},
)
} else {
get_builtin_binary_op_fn(fn_name, args[0], args[1]).map(|f| {
FnResolutionCacheEntry {
func: CallableFunction::from_method(
Box::new(f) as Box<FnAny>
),
source: None,
}
})
}
});
}
@@ -312,11 +312,11 @@ impl Engine {
.enumerate()
.map(|(i, a)| {
let mask = 1usize << (num_args - i - 1);
if bitmask & mask != 0 {
if bitmask & mask == 0 {
a.type_id()
} else {
// Replace with `Dynamic`
TypeId::of::<Dynamic>()
} else {
a.type_id()
}
}),
);
@@ -371,7 +371,7 @@ impl Engine {
);
if func.is_some() {
let is_method = func.map(|f| f.func.is_method()).unwrap_or(false);
let is_method = func.map_or(false, |f| f.func.is_method());
// Push a new call stack frame
#[cfg(feature = "debugging")]
@@ -680,8 +680,7 @@ impl Engine {
&mut global.source,
source
.as_ref()
.map(|s| (**s).clone())
.unwrap_or(crate::Identifier::new_const()),
.map_or(crate::Identifier::new_const(), |s| (**s).clone()),
);
let result = if _is_method_call {
@@ -841,14 +840,12 @@ impl Engine {
)
}
KEYWORD_FN_PTR_CALL => {
if !call_args.is_empty() {
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));
}
} else {
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>() {
let typ = self.map_type_name(call_args[0].type_name());
return Err(self.make_type_mismatch_err::<FnPtr>(typ, first_arg_pos));
}
// FnPtr call on object
@@ -1036,10 +1033,10 @@ impl Engine {
// Recalculate hash
let args_len = total_args + curry.len();
hashes = if !hashes.is_native_only() {
calc_fn_hash(name, args_len).into()
} else {
hashes = if hashes.is_native_only() {
FnCallHashes::from_native(calc_fn_hash(name, args_len))
} else {
calc_fn_hash(name, args_len).into()
};
}
// Handle Fn()
@@ -1236,7 +1233,7 @@ impl Engine {
if target_is_shared || target.is_temp_value() {
arg_values.insert(0, target.take_or_clone().flatten());
args.extend(arg_values.iter_mut())
args.extend(arg_values.iter_mut());
} else {
// Turn it into a method call only if the object is not shared and not a simple value
is_ref_mut = true;
@@ -1370,11 +1367,11 @@ impl Engine {
while bitmask < max_bitmask {
let hash_params = calc_fn_params_hash(args.iter().enumerate().map(|(i, a)| {
let mask = 1usize << (num_args - i - 1);
if bitmask & mask != 0 {
if bitmask & mask == 0 {
a.type_id()
} else {
// Replace with `Dynamic`
TypeId::of::<Dynamic>()
} else {
a.type_id()
}
}));
let hash_qualified_fn = combine_hashes(hash, hash_params);
@@ -1392,7 +1389,7 @@ impl Engine {
}
// Clone first argument if the function is not a method after-all
if !func.map(|f| f.is_method()).unwrap_or(true) {
if !func.map_or(true, CallableFunction::is_method) {
if let Some(first) = first_arg_value {
*first = args[0].clone();
args[0] = first;

View File

@@ -47,7 +47,7 @@ impl Hasher for StraightHasher {
self.0 = u64::from_ne_bytes(key);
if self.0 == 0 {
self.0 = ALT_ZERO_HASH
self.0 = ALT_ZERO_HASH;
}
}
}
@@ -175,7 +175,7 @@ pub fn calc_fn_params_hash(params: impl IntoIterator<Item = TypeId>) -> u64 {
let mut len = 0;
params.into_iter().for_each(|t| {
len += 1;
t.hash(s)
t.hash(s);
});
len.hash(s);

View File

@@ -252,7 +252,7 @@ impl<'a> NativeCallContext<'a> {
/// in reverse order.
#[inline]
pub fn iter_namespaces(&self) -> impl Iterator<Item = &Module> {
self.lib.iter().rev().cloned()
self.lib.iter().rev().copied()
}
/// _(internals)_ The current set of namespaces containing definitions of all script-defined functions.
/// Exported under the `internals` feature only.

View File

@@ -28,7 +28,7 @@ use std::{any::TypeId, mem};
pub struct Mut<T>(T);
//pub struct Ref<T>(T);
/// Dereference into DynamicWriteLock
/// Dereference into [`DynamicWriteLock`]
#[inline(always)]
#[must_use]
pub fn by_ref<T: Variant + Clone>(data: &mut Dynamic) -> DynamicWriteLock<T> {

View File

@@ -128,7 +128,7 @@ impl Engine {
} else {
caches.push_fn_resolution_cache();
lib_merged.push(&**fn_lib);
lib_merged.extend(lib.iter().cloned());
lib_merged.extend(lib.iter().copied());
&lib_merged
},
Some(mem::replace(&mut global.constants, constants.clone())),
@@ -205,7 +205,7 @@ impl Engine {
scope.rewind(orig_scope_len);
} else if !args.is_empty() {
// Remove arguments only, leaving new variables in the scope
scope.remove_range(orig_scope_len, args.len())
scope.remove_range(orig_scope_len, args.len());
}
#[cfg(not(feature = "no_module"))]
global.truncate_imports(orig_imports_len);
@@ -233,7 +233,7 @@ impl Engine {
) -> bool {
let cache = caches.fn_resolution_cache_mut();
if let Some(result) = cache.get(&hash_script).map(|v| v.is_some()) {
if let Some(result) = cache.get(&hash_script).map(Option::is_some) {
return result;
}