primitives: Add explicit None return impl for std::error::Error
What changed, and why it matters
This commit is a straightforward code clarity improvement. It rewrites several Rust error-type definitions so that they explicitly state they have no underlying cause, instead of silently relying on a default behavior. There is no functional change and no security impact.
No action required. This is a non-functional refactor improving code readability.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch replaces empty default implementations of std::error::Error (impl std::error::Error for Type {}) with explicit source() methods that return None. For an enum error, each match arm returns None. This is semantically identical to the default trait implementation and does not alter program behavior, API surface, or error handling.
Changed components
primitives/src/block.rsprimitives/src/hash_types/script_hash.rsprimitives/src/hash_types/witness_script_hash.rsprimitives/src/witness.rsInspect captured patch +19 / −4
diff --git a/primitives/src/block.rs b/primitives/src/block.rs
index 4f119ebd..da514d72 100644
--- a/primitives/src/block.rs
+++ b/primitives/src/block.rs
@@ -920,7 +920,16 @@ pub mod error {
#[cfg(feature = "alloc")]
#[cfg(feature = "std")]
- impl std::error::Error for InvalidBlockError {}
+ impl std::error::Error for InvalidBlockError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ match self {
+ Self::InvalidMerkleRoot => None,
+ Self::InvalidWitnessCommitment => None,
+ Self::NoTransactions => None,
+ Self::InvalidCoinbase => None,
+ }
+ }
+ }
/// An error that occurs during parsing of a [`Header`] from a hex string.
#[cfg(feature = "hex")]
diff --git a/primitives/src/hash_types/script_hash.rs b/primitives/src/hash_types/script_hash.rs
index c80c4211..b9f9d0ce 100644
--- a/primitives/src/hash_types/script_hash.rs
+++ b/primitives/src/hash_types/script_hash.rs
@@ -88,7 +88,9 @@ impl fmt::Display for RedeemScriptSizeError {
}
#[cfg(feature = "std")]
-impl std::error::Error for RedeemScriptSizeError {}
+impl std::error::Error for RedeemScriptSizeError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
+}
// The new hash wrapper type.
type HashType = ScriptHash;
diff --git a/primitives/src/hash_types/witness_script_hash.rs b/primitives/src/hash_types/witness_script_hash.rs
index 59a5aee7..e281e80e 100644
--- a/primitives/src/hash_types/witness_script_hash.rs
+++ b/primitives/src/hash_types/witness_script_hash.rs
@@ -83,7 +83,9 @@ impl fmt::Display for WitnessScriptSizeError {
}
#[cfg(feature = "std")]
-impl std::error::Error for WitnessScriptSizeError {}
+impl std::error::Error for WitnessScriptSizeError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
+}
include!("./generic.rs");
diff --git a/primitives/src/witness.rs b/primitives/src/witness.rs
index 98409928..47bf5382 100644
--- a/primitives/src/witness.rs
+++ b/primitives/src/witness.rs
@@ -993,7 +993,9 @@ pub mod error {
}
#[cfg(feature = "std")]
- impl std::error::Error for UnexpectedEofError {}
+ impl std::error::Error for UnexpectedEofError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
+ }
}
#[cfg(test)]
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.