Satisfy clippy.

This commit is contained in:
Stephen Chung
2022-12-22 17:34:58 +08:00
parent bbd94dbffb
commit 80ccd75514
40 changed files with 346 additions and 196 deletions

View File

@@ -276,6 +276,7 @@ gen_signed_functions!(signed_num_128 => i128);
#[export_module]
mod f32_functions {
#[cfg(not(feature = "f32_float"))]
#[allow(clippy::cast_precision_loss)]
pub mod basic_arithmetic {
#[rhai_fn(name = "+")]
pub fn add(x: f32, y: f32) -> f32 {
@@ -381,6 +382,7 @@ mod f32_functions {
"Number raised to too large an index: {x} ** {y}"
)))
} else {
#[allow(clippy::cast_possible_truncation)]
Ok(x.powi(y as i32))
}
}

View File

@@ -220,14 +220,18 @@ pub mod array_functions {
len: INT,
item: Dynamic,
) -> RhaiResultOf<()> {
let len = len.min(MAX_USIZE_INT);
if len <= 0 {
return Ok(());
}
if len <= 0 || (len as usize) <= array.len() {
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let len = len.min(MAX_USIZE_INT) as usize;
if len <= array.len() {
return Ok(());
}
let _ctx = ctx;
let len = len as usize;
// Check if array will be over max size limit
#[cfg(not(feature = "unchecked"))]
@@ -373,11 +377,16 @@ pub mod array_functions {
/// print(x); // prints "[1, 2, 3]"
/// ```
pub fn truncate(array: &mut Array, len: INT) {
if len <= 0 {
array.clear();
return;
}
if !array.is_empty() {
let len = len.min(MAX_USIZE_INT);
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let len = len.min(MAX_USIZE_INT) as usize;
if len > 0 {
array.truncate(len as usize);
array.truncate(len);
} else {
array.clear();
}
@@ -402,13 +411,18 @@ pub mod array_functions {
/// print(x); // prints "[3, 4, 5]"
/// ```
pub fn chop(array: &mut Array, len: INT) {
if len <= 0 {
array.clear();
return;
}
if !array.is_empty() {
let len = len.min(MAX_USIZE_INT);
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let len = len.min(MAX_USIZE_INT) as usize;
if len <= 0 {
array.clear();
} else if (len as usize) < array.len() {
array.drain(0..array.len() - len as usize);
} else if len < array.len() {
array.drain(0..array.len() - len);
}
}
}
@@ -1178,7 +1192,7 @@ pub mod array_functions {
/// ```
pub fn dedup(ctx: NativeCallContext, array: &mut Array) {
let comparer = FnPtr::new_unchecked(OP_EQUALS, StaticVec::new_const());
dedup_by_comparer(ctx, array, comparer)
dedup_by_comparer(ctx, array, comparer);
}
/// Remove duplicated _consecutive_ elements from the array that return `true` when applied the
/// `comparer` function.

View File

@@ -132,6 +132,7 @@ mod bit_field_functions {
ERR::ErrorBitFieldBounds(INT_BITS, start, Position::NONE).into()
})?;
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let bits = if bit + bits as usize > INT_BITS {
INT_BITS - bit
} else {
@@ -143,6 +144,7 @@ mod bit_field_functions {
}
// 2^bits - 1
#[allow(clippy::cast_possible_truncation)]
let mask = ((2 as UNSIGNED_INT).pow(bits as u32) - 1) as INT;
Ok(((value & (mask << bit)) >> bit) & mask)
@@ -218,6 +220,7 @@ mod bit_field_functions {
ERR::ErrorBitFieldBounds(INT_BITS, bit, Position::NONE).into()
})?;
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let bits = if bit + bits as usize > INT_BITS {
INT_BITS - bit
} else {
@@ -230,6 +233,7 @@ mod bit_field_functions {
}
// 2^bits - 1
#[allow(clippy::cast_possible_truncation)]
let mask = ((2 as UNSIGNED_INT).pow(bits as u32) - 1) as INT;
*value &= !(mask << bit);

View File

@@ -75,7 +75,8 @@ pub mod blob_functions {
len: INT,
value: INT,
) -> RhaiResultOf<Blob> {
let len = len.min(MAX_USIZE_INT).max(0) as usize;
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let len = len.clamp(0, MAX_USIZE_INT) as usize;
let _ctx = ctx;
// Check if blob will be over max size limit
@@ -83,6 +84,7 @@ pub mod blob_functions {
_ctx.engine().throw_on_size((len, 0, 0))?;
let mut blob = Blob::new();
#[allow(clippy::cast_sign_loss)]
blob.resize(len, (value & 0x0000_00ff) as u8);
Ok(blob)
}
@@ -155,6 +157,7 @@ pub mod blob_functions {
/// ```
#[rhai_fn(name = "contains")]
pub fn contains(blob: &mut Blob, value: INT) -> bool {
#[allow(clippy::cast_sign_loss)]
blob.contains(&((value & 0x0000_00ff) as u8))
}
/// Get the byte value at the `index` position in the BLOB.
@@ -221,6 +224,7 @@ pub mod blob_functions {
let (index, ..) = calc_offset_len(blob.len(), index, 0);
#[allow(clippy::cast_sign_loss)]
if index < blob.len() {
blob[index] = (value & 0x0000_00ff) as u8;
}
@@ -240,6 +244,7 @@ pub mod blob_functions {
/// ```
#[rhai_fn(name = "push", name = "append")]
pub fn push(blob: &mut Blob, value: INT) {
#[allow(clippy::cast_sign_loss)]
blob.push((value & 0x0000_00ff) as u8);
}
/// Add another BLOB to the end of the BLOB.
@@ -315,6 +320,7 @@ pub mod blob_functions {
/// print(b); // prints "[4242184242]"
/// ```
pub fn insert(blob: &mut Blob, index: INT, value: INT) {
#[allow(clippy::cast_sign_loss)]
let value = (value & 0x0000_00ff) as u8;
if blob.is_empty() {
@@ -354,8 +360,10 @@ pub mod blob_functions {
if len <= 0 {
return Ok(());
}
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let len = len.min(MAX_USIZE_INT) as usize;
#[allow(clippy::cast_sign_loss)]
let value = (value & 0x0000_00ff) as u8;
let _ctx = ctx;
@@ -470,11 +478,16 @@ pub mod blob_functions {
/// print(b); // prints "[010203]"
/// ```
pub fn truncate(blob: &mut Blob, len: INT) {
if len <= 0 {
blob.clear();
return;
}
if !blob.is_empty() {
let len = len.min(MAX_USIZE_INT);
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let len = len.min(MAX_USIZE_INT) as usize;
if len > 0 {
blob.truncate(len as usize);
blob.truncate(len);
} else {
blob.clear();
}
@@ -500,6 +513,7 @@ pub mod blob_functions {
///
/// print(b); // prints "[030405]"
/// ```
#[allow(clippy::cast_sign_loss, clippy::needless_pass_by_value)]
pub fn chop(blob: &mut Blob, len: INT) {
if !blob.is_empty() {
if len <= 0 {

View File

@@ -22,6 +22,7 @@ use rust_decimal::Decimal;
#[cfg(not(feature = "unchecked"))]
#[inline(always)]
#[allow(clippy::needless_pass_by_value)]
fn std_add<T>(x: T, y: T) -> Option<T>
where
T: num_traits::CheckedAdd<Output = T>,
@@ -30,6 +31,7 @@ where
}
#[inline(always)]
#[allow(dead_code)]
#[allow(clippy::unnecessary_wraps, clippy::needless_pass_by_value)]
fn regular_add<T>(x: T, y: T) -> Option<T>
where
T: std::ops::Add<Output = T>,
@@ -123,6 +125,7 @@ impl<T: Copy + PartialOrd> FusedIterator for StepRange<T> {}
pub struct BitRange(INT, usize);
impl BitRange {
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
pub fn new(value: INT, from: INT, len: INT) -> RhaiResultOf<Self> {
let from = calc_index(INT_BITS, from, true, || {
ERR::ErrorBitFieldBounds(INT_BITS, from, Position::NONE).into()
@@ -174,6 +177,7 @@ impl ExactSizeIterator for BitRange {
pub struct CharsStream(Vec<char>, usize);
impl CharsStream {
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
pub fn new(string: &str, from: INT, len: INT) -> Self {
if len <= 0 || from > MAX_USIZE_INT {
return Self(Vec::new(), 0);

View File

@@ -153,8 +153,9 @@ mod reflection_functions {
/// Return an array of object maps containing metadata of all script-defined functions
/// matching the specified name and arity (number of parameters).
#[rhai_fn(name = "get_fn_metadata_list")]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
pub fn get_fn_metadata2(ctx: NativeCallContext, name: &str, params: INT) -> Array {
if params < 0 || params > crate::MAX_USIZE_INT {
if !(0..=crate::MAX_USIZE_INT).contains(&params) {
Array::new()
} else {
collect_fn_metadata(ctx, |_, _, n, p, _| p == (params as usize) && n == name)

View File

@@ -477,7 +477,7 @@ mod decimal_functions {
}
}
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
Ok(x.round_dp(digits as u32))
}
/// Round the decimal number to the specified number of `digits` after the decimal point and return it.
@@ -495,7 +495,7 @@ mod decimal_functions {
}
}
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
Ok(x.round_dp_with_strategy(digits as u32, RoundingStrategy::AwayFromZero))
}
/// Round the decimal number to the specified number of `digits` after the decimal point and return it.
@@ -513,7 +513,7 @@ mod decimal_functions {
}
}
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
Ok(x.round_dp_with_strategy(digits as u32, RoundingStrategy::ToZero))
}
/// Round the decimal number to the specified number of `digits` after the decimal point and return it.
@@ -531,7 +531,7 @@ mod decimal_functions {
}
}
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
Ok(x.round_dp_with_strategy(digits as u32, RoundingStrategy::MidpointAwayFromZero))
}
/// Round the decimal number to the specified number of `digits` after the decimal point and return it.
@@ -549,33 +549,34 @@ mod decimal_functions {
}
}
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
Ok(x.round_dp_with_strategy(digits as u32, RoundingStrategy::MidpointTowardZero))
}
/// Convert the decimal number into an integer.
#[rhai_fn(return_raw)]
pub fn to_int(x: Decimal) -> RhaiResultOf<INT> {
let n = x.to_i64().and_then(|n| {
#[cfg(feature = "only_i32")]
return if n > (INT::MAX as i64) || n < (INT::MIN as i64) {
None
} else {
Some(n as i32)
};
x.to_i64()
.and_then(|n| {
#[cfg(feature = "only_i32")]
return if n > (INT::MAX as i64) || n < (INT::MIN as i64) {
None
} else {
Some(n as i32)
};
#[cfg(not(feature = "only_i32"))]
return Some(n);
});
n.map_or_else(
|| {
Err(
ERR::ErrorArithmetic(format!("Integer overflow: to_int({x})"), Position::NONE)
.into(),
)
},
Ok,
)
#[cfg(not(feature = "only_i32"))]
return Some(n);
})
.map_or_else(
|| {
Err(ERR::ErrorArithmetic(
format!("Integer overflow: to_int({x})"),
Position::NONE,
)
.into())
},
Ok,
)
}
/// Return the integral part of the decimal number.
#[rhai_fn(name = "int", get = "int")]

View File

@@ -95,11 +95,11 @@ mod string_functions {
#[rhai_fn(name = "+=")]
pub fn add_assign_append_str(string1: &mut ImmutableString, string2: ImmutableString) {
*string1 += string2
*string1 += string2;
}
#[rhai_fn(name = "+=", pure)]
pub fn add_assign_append_char(string: &mut ImmutableString, character: char) {
*string += character
*string += character;
}
#[rhai_fn(name = "+=")]
pub fn add_assign_append_unit(string: &mut ImmutableString, item: ()) {
@@ -246,9 +246,9 @@ mod string_functions {
pub fn clear(string: &mut ImmutableString) {
if !string.is_empty() {
if let Some(s) = string.get_mut() {
s.clear()
s.clear();
} else {
*string = ImmutableString::new()
*string = ImmutableString::new();
}
}
}
@@ -272,7 +272,7 @@ mod string_functions {
/// ```
pub fn truncate(string: &mut ImmutableString, len: INT) {
if len > 0 {
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let len = len.min(MAX_USIZE_INT) as usize;
let chars: StaticVec<_> = string.chars().collect();
let copy = string.make_mut();
@@ -355,6 +355,7 @@ mod string_functions {
if string.is_empty() || len <= 0 {
return ctx.engine().const_empty_string();
}
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let len = len.min(MAX_USIZE_INT) as usize;
let mut chars = StaticVec::<char>::with_capacity(len);
@@ -598,6 +599,7 @@ mod string_functions {
return -1;
}
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let start = if start < 0 {
let abs_start = start.unsigned_abs();
@@ -608,6 +610,7 @@ mod string_functions {
let abs_start = abs_start as usize;
let chars: Vec<_> = string.chars().collect();
let num_chars = chars.len();
if abs_start > num_chars {
0
} else {
@@ -680,6 +683,7 @@ mod string_functions {
return -1;
}
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let start = if start < 0 {
let abs_start = start.unsigned_abs();
@@ -690,6 +694,7 @@ mod string_functions {
let abs_start = abs_start as usize;
let chars = string.chars().collect::<Vec<_>>();
let num_chars = chars.len();
if abs_start > num_chars {
0
} else {
@@ -763,9 +768,12 @@ mod string_functions {
return Dynamic::UNIT;
}
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let index = index as usize;
string
.chars()
.nth(index as usize)
.nth(index)
.map_or_else(|| Dynamic::UNIT, Into::into)
} else {
// Count from end if negative
@@ -775,10 +783,13 @@ mod string_functions {
return Dynamic::UNIT;
}
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let abs_index = abs_index as usize;
string
.chars()
.rev()
.nth((abs_index as usize) - 1)
.nth(abs_index - 1)
.map_or_else(|| Dynamic::UNIT, Into::into)
}
}
@@ -811,6 +822,7 @@ mod string_functions {
return;
}
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let index = index as usize;
*string = string
@@ -825,6 +837,7 @@ mod string_functions {
return;
}
#[allow(clippy::cast_possible_truncation)]
let abs_index = abs_index as usize;
let string_len = string.chars().count();
@@ -894,13 +907,14 @@ mod string_functions {
///
/// print(text.sub_string(-8, 3)); // prints ", w"
/// ```
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
pub fn sub_string(
ctx: NativeCallContext,
string: &str,
start: INT,
len: INT,
) -> ImmutableString {
if string.is_empty() {
if string.is_empty() || len <= 0 {
return ctx.engine().const_empty_string();
}
@@ -915,8 +929,11 @@ mod string_functions {
return ctx.engine().const_empty_string();
}
#[allow(clippy::cast_possible_truncation)]
let abs_start = abs_start as usize;
chars.extend(string.chars());
if abs_start > chars.len() {
0
} else {
@@ -932,10 +949,12 @@ mod string_functions {
chars.extend(string.chars());
}
let len = if offset + len as usize > chars.len() {
let len = len.min(MAX_USIZE_INT) as usize;
let len = if offset + len > chars.len() {
chars.len() - offset
} else {
len as usize
len
};
chars
@@ -988,10 +1007,10 @@ mod string_functions {
/// print(text); // prints "llo, w"
/// ```
#[rhai_fn(name = "crop")]
pub fn crop_range(string: &mut ImmutableString, range: ExclusiveRange) {
pub fn crop_range(ctx: NativeCallContext, 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(ctx, string, start, end - start);
}
/// Remove all characters from the string except those within an inclusive `range`.
///
@@ -1005,10 +1024,14 @@ mod string_functions {
/// print(text); // prints "llo, wo"
/// ```
#[rhai_fn(name = "crop")]
pub fn crop_inclusive_range(string: &mut ImmutableString, range: InclusiveRange) {
pub fn crop_inclusive_range(
ctx: NativeCallContext,
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(ctx, string, start, end - start + 1);
}
/// Remove all characters from the string except those within a range.
@@ -1033,10 +1056,15 @@ mod string_functions {
/// print(text); // prints ", w"
/// ```
#[rhai_fn(name = "crop")]
pub fn crop(string: &mut ImmutableString, start: INT, len: INT) {
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
pub fn crop(ctx: NativeCallContext, string: &mut ImmutableString, start: INT, len: INT) {
if string.is_empty() {
return;
}
if len <= 0 {
*string = ctx.engine().const_empty_string();
return;
}
let mut chars = StaticVec::with_capacity(string.len());
@@ -1051,7 +1079,9 @@ mod string_functions {
}
let abs_start = abs_start as usize;
chars.extend(string.chars());
if abs_start > chars.len() {
0
} else {
@@ -1068,10 +1098,12 @@ mod string_functions {
chars.extend(string.chars());
}
let len = if offset + len as usize > chars.len() {
let len = len.min(MAX_USIZE_INT) as usize;
let len = if offset + len > chars.len() {
chars.len() - offset
} else {
len as usize
len
};
let copy = string.make_mut();
@@ -1098,8 +1130,12 @@ mod string_functions {
/// print(text); // prints "ld!"
/// ```
#[rhai_fn(name = "crop")]
pub fn crop_string_starting_from(string: &mut ImmutableString, start: INT) {
crop(string, start, string.len() as INT);
pub fn crop_string_starting_from(
ctx: NativeCallContext,
string: &mut ImmutableString,
start: INT,
) {
crop(ctx, string, start, string.len() as INT);
}
/// Replace all occurrences of the specified sub-string in the string with another string.
@@ -1219,6 +1255,7 @@ mod string_functions {
if len <= 0 {
return Ok(());
}
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let len = len.min(MAX_USIZE_INT) as usize;
let _ctx = ctx;
@@ -1275,6 +1312,7 @@ mod string_functions {
if len <= 0 {
return Ok(());
}
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let len = len.min(MAX_USIZE_INT) as usize;
let _ctx = ctx;
@@ -1339,6 +1377,7 @@ mod string_functions {
/// print(text.split(-99)); // prints ["", "hello, world!"]
/// ```
#[rhai_fn(name = "split")]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
pub fn split_at(ctx: NativeCallContext, string: &mut ImmutableString, index: INT) -> Array {
if index <= 0 {
let abs_index = index.unsigned_abs();
@@ -1349,9 +1388,9 @@ mod string_functions {
string.as_str().into(),
];
}
let abs_index = abs_index as usize;
let num_chars = string.chars().count();
if abs_index > num_chars {
vec![
ctx.engine().const_empty_string().into(),
@@ -1432,7 +1471,11 @@ mod string_functions {
/// print(text.split("ll", 2)); // prints ["he", "o, world! hello, foo!"]
/// ```
#[rhai_fn(name = "split")]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
pub fn splitn(string: &str, delimiter: &str, segments: INT) -> Array {
if segments < 1 {
return [string.into()].into();
}
let segments = segments.min(MAX_USIZE_INT) as usize;
let pieces: usize = if segments < 1 { 1 } else { segments };
string.splitn(pieces, delimiter).map(Into::into).collect()
@@ -1463,7 +1506,11 @@ mod string_functions {
/// print(text.split('l', 3)); // prints ["he", "", "o, world! hello, foo!"]
/// ```
#[rhai_fn(name = "split")]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
pub fn splitn_char(string: &str, delimiter: char, segments: INT) -> Array {
if segments < 1 {
return [string.into()].into();
}
let segments = segments.min(MAX_USIZE_INT) as usize;
let pieces: usize = if segments < 1 { 1 } else { segments };
string.splitn(pieces, delimiter).map(Into::into).collect()
@@ -1495,7 +1542,11 @@ mod string_functions {
/// print(text.split_rev("ll", 2)); // prints ["o, foo!", "hello, world! he"]
/// ```
#[rhai_fn(name = "split_rev")]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
pub fn rsplitn(string: &str, delimiter: &str, segments: INT) -> Array {
if segments < 1 {
return [string.into()].into();
}
let segments = segments.min(MAX_USIZE_INT) as usize;
let pieces: usize = if segments < 1 { 1 } else { segments };
string.rsplitn(pieces, delimiter).map(Into::into).collect()
@@ -1527,7 +1578,11 @@ mod string_functions {
/// print(text.split('l', 3)); // prints ["o, foo!", "", "hello, world! he"
/// ```
#[rhai_fn(name = "split_rev")]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
pub fn rsplitn_char(string: &str, delimiter: char, segments: INT) -> Array {
if segments < 1 {
return [string.into()].into();
}
let segments = segments.min(MAX_USIZE_INT) as usize;
let pieces: usize = if segments < 1 { 1 } else { segments };
string.rsplitn(pieces, delimiter).map(Into::into).collect()

View File

@@ -114,11 +114,12 @@ mod time_functions {
#[cfg(not(feature = "no_float"))]
pub mod float_functions {
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
fn add_impl(timestamp: Instant, seconds: FLOAT) -> RhaiResultOf<Instant> {
if seconds < 0.0 {
subtract_impl(timestamp, -seconds)
} else if cfg!(not(feature = "unchecked")) {
if seconds > (INT::MAX as FLOAT) {
if seconds > (INT::MAX as FLOAT).min(u64::MAX as FLOAT) {
Err(make_arithmetic_err(format!(
"Integer overflow for timestamp add: {seconds}"
)))
@@ -135,20 +136,21 @@ mod time_functions {
Ok(timestamp + Duration::from_millis((seconds * 1000.0) as u64))
}
}
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
fn subtract_impl(timestamp: Instant, seconds: FLOAT) -> RhaiResultOf<Instant> {
if seconds < 0.0 {
add_impl(timestamp, -seconds)
} else if cfg!(not(feature = "unchecked")) {
if seconds > (INT::MAX as FLOAT) {
if seconds > (INT::MAX as FLOAT).min(u64::MAX as FLOAT) {
Err(make_arithmetic_err(format!(
"Integer overflow for timestamp add: {seconds}"
"Integer overflow for timestamp subtract: {seconds}"
)))
} else {
timestamp
.checked_sub(Duration::from_millis((seconds * 1000.0) as u64))
.ok_or_else(|| {
make_arithmetic_err(format!(
"Timestamp overflow when adding {seconds} second(s)"
"Timestamp overflow when subtracting {seconds} second(s)"
))
})
}
@@ -182,6 +184,7 @@ mod time_functions {
}
fn add_impl(timestamp: Instant, seconds: INT) -> RhaiResultOf<Instant> {
#[allow(clippy::cast_sign_loss)]
if seconds < 0 {
subtract_impl(timestamp, -seconds)
} else if cfg!(not(feature = "unchecked")) {
@@ -197,6 +200,7 @@ mod time_functions {
}
}
fn subtract_impl(timestamp: Instant, seconds: INT) -> RhaiResultOf<Instant> {
#[allow(clippy::cast_sign_loss)]
if seconds < 0 {
add_impl(timestamp, -seconds)
} else if cfg!(not(feature = "unchecked")) {
@@ -204,7 +208,7 @@ mod time_functions {
.checked_sub(Duration::from_secs(seconds as u64))
.ok_or_else(|| {
make_arithmetic_err(format!(
"Timestamp overflow when adding {seconds} second(s)"
"Timestamp overflow when subtracting {seconds} second(s)"
))
})
} else {