Satisfy more clippy.

This commit is contained in:
Stephen Chung
2022-11-23 16:14:11 +08:00
parent 9f5b68549a
commit 3e7408511e
20 changed files with 150 additions and 146 deletions

View File

@@ -145,6 +145,7 @@ mod int_functions {
);
}
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
INT::from_str_radix(string.trim(), radix as u32).map_err(|err| {
ERR::ErrorArithmetic(
format!("Error parsing integer number '{string}': {err}"),
@@ -313,6 +314,7 @@ mod float_functions {
/// Convert the floating-point number into an integer.
#[rhai_fn(name = "to_int", return_raw)]
pub fn f32_to_int(x: f32) -> RhaiResultOf<INT> {
#[allow(clippy::cast_precision_loss)]
if cfg!(not(feature = "unchecked")) && (x > (INT::MAX as f32) || x < (INT::MIN as f32)) {
Err(
ERR::ErrorArithmetic(format!("Integer overflow: to_int({x})"), Position::NONE)
@@ -325,6 +327,7 @@ mod float_functions {
/// Convert the floating-point number into an integer.
#[rhai_fn(name = "to_int", return_raw)]
pub fn f64_to_int(x: f64) -> RhaiResultOf<INT> {
#[allow(clippy::cast_precision_loss)]
if cfg!(not(feature = "unchecked")) && (x > (INT::MAX as f64) || x < (INT::MIN as f64)) {
Err(
ERR::ErrorArithmetic(format!("Integer overflow: to_int({x})"), Position::NONE)
@@ -357,7 +360,7 @@ mod float_functions {
#[cfg(not(feature = "f32_float"))]
#[rhai_fn(name = "to_float")]
pub fn f32_to_f64(x: f32) -> f64 {
x as f64
x.into()
}
}
@@ -478,6 +481,7 @@ mod decimal_functions {
}
}
#[allow(clippy::cast_sign_loss)]
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,6 +499,7 @@ mod decimal_functions {
}
}
#[allow(clippy::cast_sign_loss)]
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.
@@ -512,6 +517,7 @@ mod decimal_functions {
}
}
#[allow(clippy::cast_sign_loss)]
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.
@@ -529,6 +535,7 @@ mod decimal_functions {
}
}
#[allow(clippy::cast_sign_loss)]
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.
@@ -546,6 +553,7 @@ mod decimal_functions {
}
}
#[allow(clippy::cast_sign_loss)]
Ok(x.round_dp_with_strategy(digits as u32, RoundingStrategy::MidpointTowardZero))
}
/// Convert the decimal number into an integer.
@@ -563,14 +571,15 @@ mod decimal_functions {
return Some(n);
});
match n {
Some(n) => Ok(n),
_ => Err(ERR::ErrorArithmetic(
format!("Integer overflow: to_int({x})"),
Position::NONE,
)
.into()),
}
n.map_or_else(
|| {
Err(
ERR::ErrorArithmetic(format!("Integer overflow: to_int({x})"), Position::NONE)
.into(),
)
},
|n| Ok(n),
)
}
/// Return the integral part of the decimal number.
#[rhai_fn(name = "int", get = "int")]