Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ indexing_slicing = "allow" # Too many false positives ... would be cool though
match_bool = "allow" # Adds extra indentation and LOC.
match_same_arms = "allow" # Collapses things that are conceptually unrelated to each other.
must_use_candidate = "allow" # Useful for audit but many false positives.
# Whitelist the cast lints because sometimes casts are unavoidable. But
# every cast should contain a code comment!
cast_possible_truncation = "allow"
cast_possible_wrap = "allow"
cast_sign_loss = "allow"
# Casts are sometimes unavoidable, but every cast must be justified at the
# site with an `#[allow]` and a code comment explaining why it cannot lose
# information. Keep these enabled so that requirement is enforced rather
# than aspirational.
cast_possible_truncation = "warn"
cast_possible_wrap = "warn"
cast_sign_loss = "warn"
# Exhaustive list of pedantic clippy lints
assigning_clones = "warn"
bool_to_int_with_if = "warn"
Expand Down
3 changes: 3 additions & 0 deletions codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ fn write_jet<W: io::Write>(jet: Elements, w: &mut W) -> io::Result<()> {
write!(w, "pub fn {jet}(")?;
let parameters = simplicityhl::jet::source_type(&jet);
for (i, ty) in parameters.iter().enumerate() {
// Jets take a handful of parameters at most, so the index stays well
// inside `u8` (and inside the a-z range this names them from).
#[allow(clippy::cast_possible_truncation)]
let identifier = (b'a' + i as u8) as char;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 724f851:

We should use u8::try_from and unwrap if we really believe this is impossible.

if i == parameters.len() - 1 {
write!(w, "{identifier}: {ty}")?;
Expand Down
16 changes: 14 additions & 2 deletions src/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,22 @@ impl fmt::Display for U256 {
// Divide by 10, starting at the most significant bytes
for byte in &mut bytes {
let value = carry * 256 + u32::from(*byte);
*byte = (value / 10) as u8;
// `carry` is the previous iteration's `value % 10`, so it is at
// most 9 and `value` is at most 9 * 256 + 255 = 2559. The
// quotient is therefore at most 255 and fits in a `u8`.
#[allow(clippy::cast_possible_truncation)]
{
*byte = (value / 10) as u8;
}
carry = value % 10;

if *byte != 0 {
is_zero = false;
}
}

// `carry` is a remainder modulo 10, so it is at most 9.
#[allow(clippy::cast_possible_truncation)]
digits.push(carry as u8);
}

Expand Down Expand Up @@ -335,7 +343,11 @@ impl FromStr for U256 {
// Add to the least significant bytes first
for byte in bytes.iter_mut().rev() {
let value = u32::from(*byte) * 10 + carry;
*byte = (value % 256) as u8;
// A remainder modulo 256 is at most 255, so it fits in a `u8`.
#[allow(clippy::cast_possible_truncation)]
{
*byte = (value % 256) as u8;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 724f851:

This is just confusing. We should write *byte = value as u8 and whitelist the lint with "truncation is deliberate".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think let me fix these and then I'll undraft it

}
carry = value / 256;
}
if 0 < carry {
Expand Down
Loading