1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

uucore/num_format: replace saturating_sub by regular subtraction

Using `saturating_sub()` before converting to `usize` gives a wrong
feeling of security as it looks like it ensures that the value will
never go negative. However, since it is applied to `i32`, it can, and
converting it to `usize` would go horribly wrong anyway.

By following the code flow, `exponent` cannot be greater than
`precision`, or the `else` block would not have been taken.  A plain
subtraction will give the same result and will at least panic in debug
mode.
This commit is contained in:
Samuel Tardieu 2024-01-05 14:44:09 +01:00
parent 4c5326ffa3
commit f5179290a6

View file

@ -425,7 +425,7 @@ fn format_float_shortest(
// - The precision works differently and specifies the total number // - The precision works differently and specifies the total number
// of digits instead of the digits in the fractional part. // of digits instead of the digits in the fractional part.
// - If we don't force the decimal, '0' and `.` are trimmed. // - If we don't force the decimal, '0' and `.` are trimmed.
let decimal_places = (precision as i32).saturating_sub(exponent) as usize; let decimal_places = (precision as i32 - exponent) as usize;
let mut formatted = if decimal_places == 0 && force_decimal == ForceDecimal::Yes { let mut formatted = if decimal_places == 0 && force_decimal == ForceDecimal::Yes {
format!("{f:.0}.") format!("{f:.0}.")
} else { } else {