Skip to content

Commit 9d5a796

Browse files
committed
clippy: don't use lossless casts
When casting between numeric types, if the conversion is infallible then we can use `from`. This means that if the types change to make the conversion lossy, we'll get a compiler error rather than the logic silently becoming wrong.
1 parent 53f5610 commit 9d5a796

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ assigning_clones = "warn"
5757
bool_to_int_with_if = "warn"
5858
borrow_as_ptr = "warn"
5959
case_sensitive_file_extension_comparisons = "warn"
60-
cast_lossless = "allow"
60+
cast_lossless = "warn"
6161
cast_precision_loss = "warn"
6262
cast_ptr_alignment = "warn"
6363
checked_conversions = "warn"

src/num.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl fmt::Display for U256 {
299299

300300
// Divide by 10, starting at the most significant bytes
301301
for byte in bytes.iter_mut() {
302-
let value = carry * 256 + *byte as u32;
302+
let value = carry * 256 + u32::from(*byte);
303303
*byte = (value / 10) as u8;
304304
carry = value % 10;
305305

@@ -334,7 +334,7 @@ impl FromStr for U256 {
334334

335335
// Add to the least significant bytes first
336336
for byte in bytes.iter_mut().rev() {
337-
let value = *byte as u32 * 10 + carry;
337+
let value = u32::from(*byte) * 10 + carry;
338338
*byte = (value % 256) as u8;
339339
carry = value / 256;
340340
}

0 commit comments

Comments
 (0)