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

@@ -93,6 +93,7 @@ mod logic_functions {
}
#[cfg(not(feature = "no_float"))]
#[allow(clippy::cast_precision_loss)]
#[export_module]
mod f32_functions {
use crate::INT;
@@ -148,6 +149,7 @@ mod f32_functions {
}
#[cfg(not(feature = "no_float"))]
#[allow(clippy::cast_precision_loss)]
#[export_module]
mod f64_functions {
use crate::INT;

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")]

View File

@@ -247,9 +247,10 @@ mod string_functions {
/// Clear the string, making it empty.
pub fn clear(string: &mut ImmutableString) {
if !string.is_empty() {
match string.get_mut() {
Some(s) => s.clear(),
_ => *string = ImmutableString::new(),
if let Some(s) = string.get_mut() {
s.clear()
} else {
*string = ImmutableString::new()
}
}
}
@@ -273,6 +274,7 @@ mod string_functions {
/// ```
pub fn truncate(string: &mut ImmutableString, len: INT) {
if len > 0 {
#[allow(clippy::cast_sign_loss)]
let len = len.min(MAX_USIZE_INT) as usize;
let chars: StaticVec<_> = string.chars().collect();
let copy = string.make_mut();
@@ -294,20 +296,17 @@ mod string_functions {
/// print(text); // prints "hello"
/// ```
pub fn trim(string: &mut ImmutableString) {
match string.get_mut() {
Some(s) => {
let trimmed = s.trim();
if let Some(s) = string.get_mut() {
let trimmed = s.trim();
if trimmed != s {
*s = trimmed.into();
}
if trimmed != s {
*s = trimmed.into();
}
None => {
let trimmed = string.trim();
} else {
let trimmed = string.trim();
if trimmed != string {
*string = trimmed.into();
}
if trimmed != string {
*string = trimmed.into();
}
}
}