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

@@ -145,16 +145,14 @@ pub mod array_functions {
/// ```
#[rhai_fn(name = "+")]
pub fn concat(array1: Array, array2: Array) -> Array {
if !array2.is_empty() {
if array1.is_empty() {
array2
} else {
let mut array = array1;
array.extend(array2);
array
}
} else {
if array2.is_empty() {
array1
} else if array1.is_empty() {
array2
} else {
let mut array = array1;
array.extend(array2);
array
}
}
/// Add a new element into the array at a particular `index` position.
@@ -432,7 +430,7 @@ pub mod array_functions {
pub fn splice_range(array: &mut Array, range: ExclusiveRange, replace: Array) {
let start = INT::max(range.start, 0);
let end = INT::max(range.end, start);
splice(array, start, end - start, replace)
splice(array, start, end - start, replace);
}
/// Replace an inclusive range of the array with another array.
///
@@ -450,7 +448,7 @@ pub mod array_functions {
pub fn splice_inclusive_range(array: &mut Array, range: InclusiveRange, replace: Array) {
let start = INT::max(*range.start(), 0);
let end = INT::max(*range.end(), start);
splice(array, start, end - start + 1, replace)
splice(array, start, end - start + 1, replace);
}
/// Replace a portion of the array with another array.
///
@@ -1739,13 +1737,15 @@ pub mod array_functions {
.call_raw(&ctx, None, [x.clone(), y.clone()])
.ok()
.and_then(|v| v.as_int().ok())
.map(|v| match v {
v if v > 0 => Ordering::Greater,
v if v < 0 => Ordering::Less,
0 => Ordering::Equal,
_ => unreachable!("v is {}", v),
})
.unwrap_or_else(|| x.type_id().cmp(&y.type_id()))
.map_or_else(
|| x.type_id().cmp(&y.type_id()),
|v| match v {
v if v > 0 => Ordering::Greater,
v if v < 0 => Ordering::Less,
0 => Ordering::Equal,
_ => unreachable!("v is {}", v),
},
)
});
Ok(())
@@ -2119,7 +2119,7 @@ pub mod array_functions {
let mut x = 0;
while x < array.len() {
if !filter
if filter
.call_raw(&ctx, None, [array[x].clone()])
.or_else(|err| match *err {
ERR::ErrorFunctionNotFound(fn_sig, ..)
@@ -2140,9 +2140,9 @@ pub mod array_functions {
.as_bool()
.unwrap_or(false)
{
drained.push(array.remove(x));
} else {
x += 1;
} else {
drained.push(array.remove(x));
}
i += 1;

View File

@@ -86,7 +86,7 @@ pub mod blob_functions {
}
let mut blob = Blob::new();
blob.resize(len, (value & 0x000000ff) as u8);
blob.resize(len, (value & 0x0000_00ff) as u8);
Ok(blob)
}
/// Convert the BLOB into an array of integers.
@@ -205,7 +205,7 @@ pub mod blob_functions {
let (index, ..) = calc_offset_len(blob.len(), index, 0);
if index < blob.len() {
blob[index] = (value & 0x000000ff) as u8;
blob[index] = (value & 0x0000_00ff) as u8;
}
}
/// Add a new byte `value` to the end of the BLOB.
@@ -223,7 +223,7 @@ pub mod blob_functions {
/// ```
#[rhai_fn(name = "push", name = "append")]
pub fn push(blob: &mut Blob, value: INT) {
blob.push((value & 0x000000ff) as u8);
blob.push((value & 0x0000_00ff) as u8);
}
/// Add another BLOB to the end of the BLOB.
///
@@ -298,7 +298,7 @@ pub mod blob_functions {
/// print(b); // prints "[4242184242]"
/// ```
pub fn insert(blob: &mut Blob, index: INT, value: INT) {
let value = (value & 0x000000ff) as u8;
let value = (value & 0x0000_00ff) as u8;
if blob.is_empty() {
blob.push(value);
@@ -338,7 +338,7 @@ pub mod blob_functions {
return Ok(());
}
let value = (value & 0x000000ff) as u8;
let value = (value & 0x0000_00ff) as u8;
let _ctx = ctx;
// Check if blob will be over max size limit
@@ -528,7 +528,7 @@ pub mod blob_functions {
pub fn splice_range(blob: &mut Blob, range: ExclusiveRange, replace: Blob) {
let start = INT::max(range.start, 0);
let end = INT::max(range.end, start);
splice(blob, start, end - start, replace)
splice(blob, start, end - start, replace);
}
/// Replace an inclusive `range` of the BLOB with another BLOB.
///
@@ -546,7 +546,7 @@ pub mod blob_functions {
pub fn splice_range_inclusive(blob: &mut Blob, range: InclusiveRange, replace: Blob) {
let start = INT::max(*range.start(), 0);
let end = INT::max(*range.end(), start);
splice(blob, start, end - start + 1, replace)
splice(blob, start, end - start + 1, replace);
}
/// Replace a portion of the BLOB with another BLOB.
///
@@ -1220,7 +1220,7 @@ mod write_int_functions {
pub fn write_le_int_range(blob: &mut Blob, range: ExclusiveRange, value: INT) {
let start = INT::max(range.start, 0);
let end = INT::max(range.end, start);
write_le_int(blob, start, end - start, value)
write_le_int(blob, start, end - start, value);
}
/// Write an `INT` value to the bytes within an inclusive `range` in the BLOB
/// in little-endian byte order.
@@ -1239,7 +1239,7 @@ mod write_int_functions {
pub fn write_le_int_range_inclusive(blob: &mut Blob, range: InclusiveRange, value: INT) {
let start = INT::max(*range.start(), 0);
let end = INT::max(*range.end(), start);
write_le_int(blob, start, end - start + 1, value)
write_le_int(blob, start, end - start + 1, value);
}
/// Write an `INT` value to the bytes beginning at the `start` position in the BLOB
/// in little-endian byte order.
@@ -1262,7 +1262,7 @@ mod write_int_functions {
/// ```
#[rhai_fn(name = "write_le")]
pub fn write_le_int(blob: &mut Blob, start: INT, len: INT, value: INT) {
write_int(blob, start, len, value, true)
write_int(blob, start, len, value, true);
}
/// Write an `INT` value to the bytes within an exclusive `range` in the BLOB
/// in big-endian byte order.
@@ -1281,7 +1281,7 @@ mod write_int_functions {
pub fn write_be_int_range(blob: &mut Blob, range: ExclusiveRange, value: INT) {
let start = INT::max(range.start, 0);
let end = INT::max(range.end, start);
write_be_int(blob, start, end - start, value)
write_be_int(blob, start, end - start, value);
}
/// Write an `INT` value to the bytes within an inclusive `range` in the BLOB
/// in big-endian byte order.
@@ -1300,7 +1300,7 @@ mod write_int_functions {
pub fn write_be_int_range_inclusive(blob: &mut Blob, range: InclusiveRange, value: INT) {
let start = INT::max(*range.start(), 0);
let end = INT::max(*range.end(), start);
write_be_int(blob, start, end - start + 1, value)
write_be_int(blob, start, end - start + 1, value);
}
/// Write an `INT` value to the bytes beginning at the `start` position in the BLOB
/// in big-endian byte order.
@@ -1323,7 +1323,7 @@ mod write_int_functions {
/// ```
#[rhai_fn(name = "write_be")]
pub fn write_be_int(blob: &mut Blob, start: INT, len: INT, value: INT) {
write_int(blob, start, len, value, false)
write_int(blob, start, len, value, false);
}
}
@@ -1360,7 +1360,7 @@ mod write_float_functions {
pub fn write_le_float_range(blob: &mut Blob, range: ExclusiveRange, value: FLOAT) {
let start = INT::max(range.start, 0);
let end = INT::max(range.end, start);
write_le_float(blob, start, end - start, value)
write_le_float(blob, start, end - start, value);
}
/// Write a `FLOAT` value to the bytes within an inclusive `range` in the BLOB
/// in little-endian byte order.
@@ -1371,7 +1371,7 @@ mod write_float_functions {
pub fn write_le_float_range_inclusive(blob: &mut Blob, range: InclusiveRange, value: FLOAT) {
let start = INT::max(*range.start(), 0);
let end = INT::max(*range.end(), start);
write_le_float(blob, start, end - start + 1, value)
write_le_float(blob, start, end - start + 1, value);
}
/// Write a `FLOAT` value to the bytes beginning at the `start` position in the BLOB
/// in little-endian byte order.
@@ -1386,7 +1386,7 @@ mod write_float_functions {
/// * If number of bytes in `range` > number of bytes for `FLOAT`, extra bytes in `range` are not modified.
#[rhai_fn(name = "write_le")]
pub fn write_le_float(blob: &mut Blob, start: INT, len: INT, value: FLOAT) {
write_float(blob, start, len, value, true)
write_float(blob, start, len, value, true);
}
/// Write a `FLOAT` value to the bytes within an exclusive `range` in the BLOB
/// in big-endian byte order.
@@ -1397,7 +1397,7 @@ mod write_float_functions {
pub fn write_be_float_range(blob: &mut Blob, range: ExclusiveRange, value: FLOAT) {
let start = INT::max(range.start, 0);
let end = INT::max(range.end, start);
write_be_float(blob, start, end - start, value)
write_be_float(blob, start, end - start, value);
}
/// Write a `FLOAT` value to the bytes within an inclusive `range` in the BLOB
/// in big-endian byte order.
@@ -1408,7 +1408,7 @@ mod write_float_functions {
pub fn write_be_float_range_inclusive(blob: &mut Blob, range: InclusiveRange, value: FLOAT) {
let start = INT::max(*range.start(), 0);
let end = INT::max(*range.end(), start);
write_be_float(blob, start, end - start + 1, value)
write_be_float(blob, start, end - start + 1, value);
}
/// Write a `FLOAT` value to the bytes beginning at the `start` position in the BLOB
/// in big-endian byte order.
@@ -1423,7 +1423,7 @@ mod write_float_functions {
/// * If number of bytes in `range` > number of bytes for `FLOAT`, extra bytes in `range` are not modified.
#[rhai_fn(name = "write_be")]
pub fn write_be_float(blob: &mut Blob, start: INT, len: INT, value: FLOAT) {
write_float(blob, start, len, value, false)
write_float(blob, start, len, value, false);
}
}
@@ -1471,7 +1471,7 @@ mod write_string_functions {
pub fn write_utf8_string_range(blob: &mut Blob, range: ExclusiveRange, string: &str) {
let start = INT::max(range.start, 0);
let end = INT::max(range.end, start);
write_string(blob, start, end - start, string, false)
write_string(blob, start, end - start, string, false);
}
/// Write a string to the bytes within an inclusive `range` in the BLOB in UTF-8 encoding.
///
@@ -1489,7 +1489,7 @@ mod write_string_functions {
pub fn write_utf8_string_range_inclusive(blob: &mut Blob, range: InclusiveRange, string: &str) {
let start = INT::max(*range.start(), 0);
let end = INT::max(*range.end(), start);
write_string(blob, start, end - start + 1, string, false)
write_string(blob, start, end - start + 1, string, false);
}
/// Write a string to the bytes within an inclusive `range` in the BLOB in UTF-8 encoding.
///
@@ -1511,7 +1511,7 @@ mod write_string_functions {
/// ```
#[rhai_fn(name = "write_utf8")]
pub fn write_utf8_string(blob: &mut Blob, start: INT, len: INT, string: &str) {
write_string(blob, start, len, string, false)
write_string(blob, start, len, string, false);
}
/// Write an ASCII string to the bytes within an exclusive `range` in the BLOB.
///
@@ -1532,7 +1532,7 @@ mod write_string_functions {
pub fn write_ascii_string_range(blob: &mut Blob, range: ExclusiveRange, string: &str) {
let start = INT::max(range.start, 0);
let end = INT::max(range.end, start);
write_string(blob, start, end - start, string, true)
write_string(blob, start, end - start, string, true);
}
/// Write an ASCII string to the bytes within an inclusive `range` in the BLOB.
///
@@ -1557,7 +1557,7 @@ mod write_string_functions {
) {
let start = INT::max(*range.start(), 0);
let end = INT::max(*range.end(), start);
write_string(blob, start, end - start + 1, string, true)
write_string(blob, start, end - start + 1, string, true);
}
/// Write an ASCII string to the bytes within an exclusive `range` in the BLOB.
///
@@ -1579,6 +1579,6 @@ mod write_string_functions {
/// ```
#[rhai_fn(name = "write_ascii")]
pub fn write_ascii_string(blob: &mut Blob, start: INT, len: INT, string: &str) {
write_string(blob, start, len, string, true)
write_string(blob, start, len, string, true);
}
}

View File

@@ -28,7 +28,7 @@ where
}
// Range iterator with step
#[derive(Clone, Copy, Hash, Eq, PartialEq)]
#[derive(Clone, Hash, Eq, PartialEq)]
pub struct StepRange<T: Debug + Copy + PartialOrd> {
pub from: T,
pub to: T,
@@ -110,7 +110,7 @@ impl<T: Debug + Copy + PartialOrd> Iterator for StepRange<T> {
impl<T: Debug + Copy + PartialOrd> FusedIterator for StepRange<T> {}
// Bit-field iterator with step
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct BitRange(INT, INT, usize);
impl BitRange {

View File

@@ -220,7 +220,7 @@ fn collect_fn_metadata(
f,
)
.into(),
)
);
});
ctx.engine()
@@ -237,7 +237,7 @@ fn collect_fn_metadata(
f,
)
.into(),
)
);
});
#[cfg(not(feature = "no_module"))]
@@ -255,7 +255,7 @@ fn collect_fn_metadata(
f,
)
.into(),
)
);
});
#[cfg(not(feature = "no_module"))]
@@ -264,7 +264,7 @@ fn collect_fn_metadata(
fn scan_module(
list: &mut Array,
dict: &BTreeSet<Identifier>,
namespace: Identifier,
namespace: &str,
module: &Module,
filter: impl Fn(
FnNamespace,
@@ -278,7 +278,7 @@ fn collect_fn_metadata(
module
.iter_script_fn()
.filter(|(s, a, n, p, f)| filter(*s, *a, n, *p, f))
.for_each(|(.., f)| list.push(make_metadata(dict, namespace.clone(), f).into()));
.for_each(|(.., f)| list.push(make_metadata(dict, namespace.into(), f).into()));
for (ns, m) in module.iter_sub_modules() {
let ns = format!(
"{}{}{}",
@@ -286,12 +286,12 @@ fn collect_fn_metadata(
crate::tokenizer::Token::DoubleColon.literal_syntax(),
ns
);
scan_module(list, dict, ns.into(), &**m, filter)
scan_module(list, dict, &ns, &**m, filter);
}
}
for (ns, m) in ctx.iter_imports_raw() {
scan_module(&mut list, &dict, ns.clone(), &**m, filter)
scan_module(&mut list, &dict, ns, &**m, filter);
}
}

View File

@@ -92,10 +92,10 @@ mod map_functions {
/// print(m); // prints "#{a:1, c:3}"
/// ```
pub fn remove(map: &mut Map, property: &str) -> Dynamic {
if !map.is_empty() {
map.remove(property).unwrap_or(Dynamic::UNIT)
} else {
if map.is_empty() {
Dynamic::UNIT
} else {
map.remove(property).unwrap_or(Dynamic::UNIT)
}
}
/// Add all property values of another object map into the object map.
@@ -160,9 +160,9 @@ mod map_functions {
if map.is_empty() {
*map = map2;
} else {
map2.into_iter().for_each(|(key, value)| {
for (key, value) in map2 {
map.entry(key).or_insert(value);
});
}
}
}
}

View File

@@ -98,6 +98,7 @@ macro_rules! def_package {
impl $package {
#[doc=concat!("Create a new `", stringify!($package), "`")]
#[must_use]
pub fn new() -> Self {
let mut module = $crate::Module::new();
<Self as $crate::packages::Package>::init(&mut module);

View File

@@ -114,7 +114,7 @@ mod string_functions {
}
}
#[rhai_fn(name = "+")]
pub fn add_prepend(utf8: Blob, string: ImmutableString) -> ImmutableString {
pub fn add_prepend(utf8: Blob, string: &str) -> ImmutableString {
let s = String::from_utf8_lossy(&utf8);
let mut s = match s {
std::borrow::Cow::Borrowed(_) => String::from_utf8(utf8).unwrap(),
@@ -122,7 +122,7 @@ mod string_functions {
};
if !string.is_empty() {
s.push_str(&string);
s.push_str(string);
}
s.into()
@@ -440,7 +440,7 @@ mod string_functions {
/// ```
#[rhai_fn(name = "make_upper")]
pub fn make_upper_char(character: &mut char) {
*character = to_upper_char(*character)
*character = to_upper_char(*character);
}
/// Convert the character to lower-case and return it as a new character.
///
@@ -476,7 +476,7 @@ mod string_functions {
/// ```
#[rhai_fn(name = "make_lower")]
pub fn make_lower_char(character: &mut char) {
*character = to_lower_char(*character)
*character = to_lower_char(*character);
}
/// Return `true` if the string starts with a specified string.
@@ -558,10 +558,9 @@ mod string_functions {
.len()
};
string[start..]
.find(character)
.map(|index| string[0..start + index].chars().count() as INT)
.unwrap_or(-1 as INT)
string[start..].find(character).map_or(-1 as INT, |index| {
string[0..start + index].chars().count() as INT
})
}
/// Find the specified `character` in the string and return the first index where it is found.
/// If the `character` is not found, `-1` is returned.
@@ -582,8 +581,7 @@ mod string_functions {
} else {
string
.find(character)
.map(|index| string[0..index].chars().count() as INT)
.unwrap_or(-1 as INT)
.map_or(-1 as INT, |index| string[0..index].chars().count() as INT)
}
}
/// Find the specified sub-string in the string, starting from the specified `start` position,
@@ -638,8 +636,9 @@ mod string_functions {
string[start..]
.find(find_string)
.map(|index| string[0..start + index].chars().count() as INT)
.unwrap_or(-1 as INT)
.map_or(-1 as INT, |index| {
string[0..start + index].chars().count() as INT
})
}
/// Find the specified `character` in the string and return the first index where it is found.
/// If the `character` is not found, `-1` is returned.
@@ -660,8 +659,7 @@ mod string_functions {
} else {
string
.find(find_string)
.map(|index| string[0..index].chars().count() as INT)
.unwrap_or(-1 as INT)
.map_or(-1 as INT, |index| string[0..index].chars().count() as INT)
}
}
@@ -840,7 +838,7 @@ mod string_functions {
.iter()
.skip(offset)
.take(len)
.cloned()
.copied()
.collect::<String>()
.into()
}
@@ -889,7 +887,7 @@ mod string_functions {
pub fn crop_range(string: &mut ImmutableString, range: ExclusiveRange) {
let start = INT::max(range.start, 0);
let end = INT::max(range.end, start);
crop(string, start, end - start)
crop(string, start, end - start);
}
/// Remove all characters from the string except those within an inclusive `range`.
///
@@ -906,7 +904,7 @@ mod string_functions {
pub fn crop_inclusive_range(string: &mut ImmutableString, range: InclusiveRange) {
let start = INT::max(*range.start(), 0);
let end = INT::max(*range.end(), start);
crop(string, start, end - start + 1)
crop(string, start, end - start + 1);
}
/// Remove all characters from the string except those within a range.