Skip to content

Commit 48634a8

Browse files
committed
clippy: enable lossless-cast lint
There is a huge list of lints I'd like to enable (currently in rust-elements and a few other crates). As a start, enable the cast_lossless one, which I think is very important: it complaints if you use a cast where usize::from() would suffice. The benefit of the from() syntax is that it will compile iff the cast cannot truncate. So the reader of the code can tell immediately that there is no danger of changing the value, and if the lint is on, conversely the reader can tell that anything that *is* a cast is something that needs further scrutiny.
1 parent 1b43322 commit 48634a8

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,10 @@ match_bool = "allow" # Adds extra indentation and LOC.
5858
match_same_arms = "allow" # Collapses things that are conceptually unrelated to each other.
5959
must_use_candidate = "allow" # Useful for audit but many false positives.
6060
similar_names = "allow" # Too many (subjectively) false positives.
61+
# Cast-related lints
62+
cast_lossless = "warn"
63+
cast_possible_truncation = "allow" # All casts should include a code comment (except test code).
64+
cast_possible_wrap = "allow" # Same as above re code comment.
65+
cast_precision_loss = "warn"
66+
cast_ptr_alignment = "warn"
67+
cast_sign_loss = "allow" # All casts should include a code comment (except in test code).

src/policy/satisfy.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,14 +555,14 @@ mod tests {
555555
let witness = to_witness(&program);
556556
assert_eq!(2, witness.len());
557557

558-
assert_eq!(Value::u1(bit as u8), *witness[0]);
558+
assert_eq!(Value::u1(u8::from(bit)), *witness[0]);
559559
let preimage_bytes = witness[1]
560560
.iter_padded()
561561
.try_collect_bytes()
562562
.expect("to bytes");
563563
let witness_preimage =
564564
Preimage32::try_from(preimage_bytes.as_slice()).expect("to array");
565-
assert_eq!(preimages[bit as usize], witness_preimage);
565+
assert_eq!(preimages[usize::from(bit)], witness_preimage);
566566

567567
execute_successful(program, &env);
568568
};
@@ -663,7 +663,7 @@ mod tests {
663663
],
664664
);
665665

666-
match bit0 as u8 + bit1 as u8 + bit2 as u8 {
666+
match u8::from(bit0) + u8::from(bit1) + u8::from(bit2) {
667667
3 => assert_branches(&policy, &[bit0, bit1, false]),
668668
2 => assert_branches(&policy, &[bit0, bit1, bit2]),
669669
_ => assert!(policy.satisfy(&satisfier, &env).is_err()),

0 commit comments

Comments
 (0)