What changed, and why it matters
This commit is a routine code cleanup that fixes compiler lint warnings. It rewrites some pattern-matching to use shorter syntax, adds missing semicolons, converts a manual panic into an assertion, and reorganizes imports and test code behind feature flags. There is no functional change that affects security.
No security action required. Treat as normal maintenance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff addresses clippy/style lints: matches! now uses Self::Variant instead of the enum name; a missing semicolon is added in check_encode; a manual if ... panic! is replaced with assert!; and test/transaction code is reorganized so imports and items are gated by the alloc feature. No logic, API, or behavior changes are introduced.
Changed components
consensus_encoding/src/encode/mod.rsconsensus_encoding/tests/composition.rsprimitives/src/transaction.rsInspect captured patch +21 / −11
diff --git a/consensus_encoding/src/encode/mod.rs b/consensus_encoding/src/encode/mod.rs
index 750fdafa..044a5111 100644
--- a/consensus_encoding/src/encode/mod.rs
+++ b/consensus_encoding/src/encode/mod.rs
@@ -122,12 +122,12 @@ pub enum EncoderStatus {
impl EncoderStatus {
/// Returns `true` if `self` is `HasMore`, `false` otherwise.
pub fn has_more(&self) -> bool {
- matches!(self, EncoderStatus::HasMore)
+ matches!(self, Self::HasMore)
}
/// Returns `true` if `self` is `Finished`, `false` otherwise.
pub fn has_finished(&self) -> bool {
- matches!(self, EncoderStatus::Finished)
+ matches!(self, Self::Finished)
}
}
@@ -397,7 +397,7 @@ where
/// If the bytes yielded from the encoder of `value` don't match the bytes in `expected`.
#[track_caller]
pub fn check_encode<T: Encode + ?Sized>(value: &T, expected: &[u8]) {
- check_encoder(&mut value.encoder(), expected)
+ check_encoder(&mut value.encoder(), expected);
}
/// Checks that the given `encoder` yields `expected`, panicking if it doesn't.
@@ -420,9 +420,7 @@ pub fn check_encoder<T: Encoder + ?Sized>(encoder: &mut T, mut expected: &[u8])
loop {
let chunk = encoder.current_chunk();
- if chunk.len() > expected.len() {
- panic!("encoder yielded more bytes ({}) than expected ({})", bytes_processed + chunk.len(), orig_expected_len);
- }
+ assert!(chunk.len() <= expected.len(), "encoder yielded more bytes ({}) than expected ({})", bytes_processed + chunk.len(), orig_expected_len);
if let Some((i, _)) = chunk.iter().zip(&expected[..chunk.len()]).enumerate().find(|&(_, (a, b))| a != b) {
panic!("encoder did not yield expected bytes - difference in chunk #{}, after {} bytes", chunk_number, bytes_processed + i);
}
diff --git a/consensus_encoding/tests/composition.rs b/consensus_encoding/tests/composition.rs
index a6345ec5..f9a7087f 100644
--- a/consensus_encoding/tests/composition.rs
+++ b/consensus_encoding/tests/composition.rs
@@ -3,21 +3,27 @@
//! Test composition of encoders and decoders.
use bitcoin_consensus_encoding::{
- ArrayDecoder, ArrayEncoder, BytesEncoder, check_encoder, Decode, Decoder, Decoder2,
- Decoder2Error, Decoder6, Encode, Encoder, Encoder2, Encoder3, Encoder6, UnexpectedEofError,
+ ArrayDecoder, BytesEncoder, check_encoder, Decoder, Decoder2, Decoder2Error, Decoder6, Encoder3,
+ UnexpectedEofError,
};
#[cfg(feature = "alloc")]
-use bitcoin_consensus_encoding::{drain_to_vec, encode_to_vec};
+use bitcoin_consensus_encoding::{
+ ArrayEncoder, Decode, Encode, Encoder2,
+ drain_to_vec, encode_to_vec, Encoder6,
+};
+#[cfg(feature = "alloc")]
const EMPTY: &[u8] = &[];
// A simple composite type that encodes as [4 bytes] + [2 bytes].
+#[cfg(feature = "alloc")]
#[derive(Debug, PartialEq, Eq)]
struct CompositeData {
first: [u8; 4],
second: [u8; 2],
}
+#[cfg(feature = "alloc")]
impl Encode for CompositeData {
type Encoder<'e> = Encoder2<ArrayEncoder<4>, ArrayEncoder<2>>;
@@ -30,15 +36,18 @@ impl Encode for CompositeData {
}
/// A unified error type for [`CompositeDataDecoder`].
+#[cfg(feature = "alloc")]
#[derive(Debug, Clone, PartialEq, Eq)]
enum CompositeError {
Eof(UnexpectedEofError),
}
+#[cfg(feature = "alloc")]
impl From<UnexpectedEofError> for CompositeError {
fn from(eof: UnexpectedEofError) -> Self { Self::Eof(eof) }
}
+#[cfg(feature = "alloc")]
impl core::fmt::Display for CompositeError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
@@ -48,11 +57,13 @@ impl core::fmt::Display for CompositeError {
}
/// A wrapper decoder that converts the tuple output to [`CompositeData`].
+#[cfg(feature = "alloc")]
#[derive(Default)]
struct CompositeDataDecoder {
inner: Decoder2<ArrayDecoder<4>, ArrayDecoder<2>>,
}
+#[cfg(feature = "alloc")]
impl Decoder for CompositeDataDecoder {
type Output = CompositeData;
type Error = CompositeError;
@@ -73,6 +84,7 @@ impl Decoder for CompositeDataDecoder {
fn read_limit(&self) -> usize { self.inner.read_limit() }
}
+#[cfg(feature = "alloc")]
impl Decode for CompositeData {
type Decoder = CompositeDataDecoder;
}
diff --git a/primitives/src/transaction.rs b/primitives/src/transaction.rs
index b4d1c9b0..2ad2f8ea 100644
--- a/primitives/src/transaction.rs
+++ b/primitives/src/transaction.rs
@@ -16,10 +16,10 @@ use core::{cmp, mem};
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
-use encoding::{ArrayEncoder, BytesEncoder, Encoder2, EncoderStatus};
+use encoding::{ArrayEncoder, BytesEncoder, Encoder2};
#[cfg(feature = "alloc")]
use encoding::{
- CompactSizeEncoder, Decoder2, Decoder3, Encode as _, Encoder3, Encoder6, SliceEncoder,
+ CompactSizeEncoder, Decoder2, Decoder3, Encode as _, Encoder3, Encoder6, EncoderStatus, SliceEncoder,
VecDecoder,
};
#[cfg(feature = "alloc")]
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.