units: refactor variable initialization location
What changed, and why it matters
This is a minor code cleanup in the rust-bitcoin library. It moves where a variable is first given a value so that the Rust compiler stops warning about unnecessary late initialization. There is no change to program behavior, no bug fix, and no security relevance.
No security action needed. Treat as a normal code-quality refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors fmt_satoshi_in in units/src/amount/mod.rs to initialize trailing_decimal_zeros directly from a match expression instead of assigning it inside each match arm. This suppresses a compiler lint about late initialization while preserving identical logic and values.
Changed components
units/src/amount/mod.rsInspect captured patch +5 / −6
diff --git a/units/src/amount/mod.rs b/units/src/amount/mod.rs
index 7c2809f8..bc74520f 100644
--- a/units/src/amount/mod.rs
+++ b/units/src/amount/mod.rs
@@ -461,15 +461,14 @@ fn fmt_satoshi_in(
let mut num_after_decimal_point = 0;
let mut norm_nb_decimals = 0;
let mut num_before_decimal_point = satoshi;
- let trailing_decimal_zeros;
let mut exp = 0;
- match precision.cmp(&0) {
+ let trailing_decimal_zeros = match precision.cmp(&0) {
// We add the number of zeroes to the end
Ordering::Greater => {
if satoshi > 0 {
exp = precision as usize; // Cast ok, checked not negative above.
}
- trailing_decimal_zeros = options.precision.unwrap_or(0);
+ options.precision.unwrap_or(0)
}
Ordering::Less => {
let precision = precision.unsigned_abs();
@@ -502,10 +501,10 @@ fn fmt_satoshi_in(
}
// compute requested precision
let opt_precision = options.precision.unwrap_or(0);
- trailing_decimal_zeros = opt_precision.saturating_sub(norm_nb_decimals);
+ opt_precision.saturating_sub(norm_nb_decimals)
}
- Ordering::Equal => trailing_decimal_zeros = options.precision.unwrap_or(0),
- }
+ Ordering::Equal => options.precision.unwrap_or(0),
+ };
let total_decimals = norm_nb_decimals + trailing_decimal_zeros;
// Compute expected width of the number
let mut num_width = if total_decimals > 0 {
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.