Shut up clippy.

This commit is contained in:
Stephen Chung
2022-08-27 16:26:41 +08:00
parent d80184ba14
commit bf5d6ab35a
28 changed files with 313 additions and 205 deletions

View File

@@ -152,14 +152,14 @@ impl Engine {
if let Ok(val) =
self.call_indexer_get(global, caches, lib, target, idx, level)
{
let mut res = val.into();
let mut val = val.into();
// Run the op-assignment
self.eval_op_assignment(
global, caches, lib, op_info, &mut res, root, new_val,
global, caches, lib, op_info, &mut val, root, new_val,
level,
)?;
// Replace new value
new_val = res.take_or_clone();
new_val = val.take_or_clone();
#[cfg(not(feature = "unchecked"))]
self.check_data_size(&new_val, op_info.pos)?;
}
@@ -864,13 +864,16 @@ impl Engine {
map.insert(index.clone().into(), Dynamic::UNIT);
}
if let Some(value) = map.get_mut(index.as_str()) {
Ok(Target::from(value))
} else if self.fail_on_invalid_map_property() {
Err(ERR::ErrorPropertyNotFound(index.to_string(), idx_pos).into())
} else {
Ok(Target::from(Dynamic::UNIT))
}
map.get_mut(index.as_str()).map_or_else(
|| {
if self.fail_on_invalid_map_property() {
Err(ERR::ErrorPropertyNotFound(index.to_string(), idx_pos).into())
} else {
Ok(Target::from(Dynamic::UNIT))
}
},
|value| Ok(Target::from(value)),
)
}
#[cfg(not(feature = "no_index"))]
@@ -970,21 +973,33 @@ impl Engine {
.map_err(|typ| self.make_type_mismatch_err::<crate::INT>(typ, idx_pos))?;
let (ch, offset) = if index >= 0 {
if index >= crate::MAX_USIZE_INT {
return Err(
ERR::ErrorStringBounds(s.chars().count(), index, idx_pos).into()
);
}
let offset = index as usize;
(
s.chars().nth(offset).ok_or_else(|| {
let chars_len = s.chars().count();
ERR::ErrorStringBounds(chars_len, index, idx_pos)
ERR::ErrorStringBounds(s.chars().count(), index, idx_pos)
})?,
offset,
)
} else {
let offset = index.unsigned_abs() as usize;
let abs_index = index.unsigned_abs();
if abs_index as u64 >= usize::MAX as u64 {
return Err(
ERR::ErrorStringBounds(s.chars().count(), index, idx_pos).into()
);
}
let offset = abs_index as usize;
(
// Count from end if negative
s.chars().rev().nth(offset - 1).ok_or_else(|| {
let chars_len = s.chars().count();
ERR::ErrorStringBounds(chars_len, index, idx_pos)
ERR::ErrorStringBounds(s.chars().count(), index, idx_pos)
})?,
offset,
)

View File

@@ -72,7 +72,7 @@ impl Engine {
/// Is there a data size limit set?
#[cfg(not(feature = "unchecked"))]
pub(crate) fn has_data_size_limit(&self) -> bool {
pub(crate) const fn has_data_size_limit(&self) -> bool {
let mut _limited = self.limits.max_string_size.is_some();
#[cfg(not(feature = "no_index"))]

View File

@@ -74,19 +74,22 @@ impl Engine {
(_, namespace, hash_var, var_name) => {
// foo:bar::baz::VARIABLE
if let Some(module) = self.search_imports(global, namespace) {
return if let Some(mut target) = module.get_qualified_var(*hash_var) {
// Module variables are constant
target.set_access_mode(AccessMode::ReadOnly);
Ok((target.into(), *_var_pos))
} else {
let sep = crate::tokenizer::Token::DoubleColon.literal_syntax();
return module.get_qualified_var(*hash_var).map_or_else(
|| {
let sep = crate::tokenizer::Token::DoubleColon.literal_syntax();
Err(ERR::ErrorVariableNotFound(
format!("{namespace}{sep}{var_name}"),
namespace.position(),
)
.into())
};
Err(ERR::ErrorVariableNotFound(
format!("{namespace}{sep}{var_name}"),
namespace.position(),
)
.into())
},
|mut target| {
// Module variables are constant
target.set_access_mode(AccessMode::ReadOnly);
Ok((target.into(), *_var_pos))
},
);
}
// global::VARIABLE
@@ -363,7 +366,7 @@ impl Engine {
#[cfg(not(feature = "no_index"))]
Expr::Array(x, ..) => {
let mut arr = crate::Array::with_capacity(x.len());
let mut array = crate::Array::with_capacity(x.len());
let mut result = Ok(Dynamic::UNIT);
#[cfg(not(feature = "unchecked"))]
@@ -383,7 +386,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
let val_sizes = Self::calc_data_sizes(&value, true);
arr.push(value);
array.push(value);
#[cfg(not(feature = "unchecked"))]
if self.has_data_size_limit() {
@@ -396,7 +399,7 @@ impl Engine {
}
}
result.map(|_| arr.into())
result.map(|_| array.into())
}
#[cfg(not(feature = "no_object"))]

View File

@@ -631,8 +631,12 @@ impl Engine {
for (x, iter_value) in func(iter_obj).enumerate() {
// Increment counter
if counter_index < usize::MAX {
// As the variable increments from 0, this should always work
// since any overflow will first be caught below.
let index_value = x as INT;
#[cfg(not(feature = "unchecked"))]
if x > INT::MAX as usize {
if index_value > crate::MAX_USIZE_INT {
loop_result = Err(ERR::ErrorArithmetic(
format!("for-loop counter overflow: {x}"),
counter.pos,
@@ -641,10 +645,8 @@ impl Engine {
break;
}
let index_value = Dynamic::from(x as INT);
*scope.get_mut_by_index(counter_index).write_lock().unwrap() =
index_value;
Dynamic::from_int(index_value);
}
let value = match iter_value {

View File

@@ -16,7 +16,7 @@ use std::prelude::v1::*;
pub fn calc_offset_len(length: usize, start: crate::INT, len: crate::INT) -> (usize, usize) {
let start = if start < 0 {
length - usize::min(start.unsigned_abs() as usize, length)
} else if start as usize >= length {
} else if start > crate::MAX_USIZE_INT || start as usize >= length {
return (length, 0);
} else {
start as usize
@@ -24,7 +24,7 @@ pub fn calc_offset_len(length: usize, start: crate::INT, len: crate::INT) -> (us
let len = if len <= 0 {
0
} else if len as usize > length - start {
} else if len > crate::MAX_USIZE_INT || len as usize > length - start {
length - start
} else {
len as usize
@@ -59,7 +59,7 @@ pub fn calc_index<E>(
} else {
err()
}
} else if start as usize >= length {
} else if start > crate::MAX_USIZE_INT || start as usize >= length {
err()
} else {
Ok(start as usize)