chacha20_poly1305: add clippy::use_self lint
What changed, and why it matters
This commit only changes style: it replaces explicit type names like Key or Nonce with the Rust shorthand Self inside the same type's implementation block, and adds a Clippy lint to warn about this style in the future. There is no change to program behavior, no bug fix, and no security relevance.
No security action needed; treat as a normal code-style/linting commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff is a pure refactoring adding #![warn(clippy::use_self)] to chacha20_poly1305/Cargo.toml and mechanically replacing struct literal names (Key, Nonce, U32x4, State, ChaCha20, ChaCha20Poly1305, Poly1305, Error) with Self in impl blocks. These substitutions are semantically identical in Rust and do not alter constants, algorithms, memory layout, or public APIs.
Changed components
chacha20_poly1305/Cargo.tomlchacha20_poly1305/src/chacha20.rschacha20_poly1305/src/lib.rschacha20_poly1305/src/poly1305.rsInspect captured patch +21 / −18
diff --git a/chacha20_poly1305/Cargo.toml b/chacha20_poly1305/Cargo.toml
index 57b19bd2..5b1f15cf 100644
--- a/chacha20_poly1305/Cargo.toml
+++ b/chacha20_poly1305/Cargo.toml
@@ -26,3 +26,6 @@ rustdoc-args = ["--cfg", "docsrs"]
[lints.rust]
unexpected_cfgs = { level = "deny", check-cfg = ['cfg(bench)'] }
+
+[lints.clippy]
+use_self = "warn"
diff --git a/chacha20_poly1305/src/chacha20.rs b/chacha20_poly1305/src/chacha20.rs
index 16664f70..13c4f5f4 100644
--- a/chacha20_poly1305/src/chacha20.rs
+++ b/chacha20_poly1305/src/chacha20.rs
@@ -19,7 +19,7 @@ pub struct Key([u8; 32]);
impl Key {
/// Constructs a new key.
- pub const fn new(key: [u8; 32]) -> Self { Key(key) }
+ pub const fn new(key: [u8; 32]) -> Self { Self(key) }
}
/// A 96-bit initialization vector (IV), or nonce.
@@ -28,7 +28,7 @@ pub struct Nonce([u8; 12]);
impl Nonce {
/// Constructs a new nonce.
- pub const fn new(nonce: [u8; 12]) -> Self { Nonce(nonce) }
+ pub const fn new(nonce: [u8; 12]) -> Self { Self(nonce) }
}
// Const validation trait for compile time check with max of 3.
@@ -74,7 +74,7 @@ impl U32x4 {
(0..4).for_each(|i| {
result[i] = self.0[i].wrapping_add(rhs.0[i]);
});
- U32x4(result)
+ Self(result)
}
#[inline(always)]
@@ -83,7 +83,7 @@ impl U32x4 {
(0..4).for_each(|i| {
result[i] = self.0[i].rotate_left(n);
});
- U32x4(result)
+ Self(result)
}
#[inline(always)]
@@ -92,9 +92,9 @@ impl U32x4 {
(): UpTo3<N>,
{
match N {
- 1 => U32x4([self.0[1], self.0[2], self.0[3], self.0[0]]),
- 2 => U32x4([self.0[2], self.0[3], self.0[0], self.0[1]]),
- 3 => U32x4([self.0[3], self.0[0], self.0[1], self.0[2]]),
+ 1 => Self([self.0[1], self.0[2], self.0[3], self.0[0]]),
+ 2 => Self([self.0[2], self.0[3], self.0[0], self.0[1]]),
+ 3 => Self([self.0[3], self.0[0], self.0[1], self.0[2]]),
_ => self, // Rotate by 0 is a no-op.
}
}
@@ -105,9 +105,9 @@ impl U32x4 {
(): UpTo3<N>,
{
match N {
- 1 => U32x4([self.0[3], self.0[0], self.0[1], self.0[2]]),
- 2 => U32x4([self.0[2], self.0[3], self.0[0], self.0[1]]),
- 3 => U32x4([self.0[1], self.0[2], self.0[3], self.0[0]]),
+ 1 => Self([self.0[3], self.0[0], self.0[1], self.0[2]]),
+ 2 => Self([self.0[2], self.0[3], self.0[0], self.0[1]]),
+ 3 => Self([self.0[1], self.0[2], self.0[3], self.0[0]]),
_ => self, // Rotate by 0 is a no-op.
}
}
@@ -131,7 +131,7 @@ impl BitXor for U32x4 {
(0..4).for_each(|i| {
result[i] = self.0[i] ^ rhs.0[i];
});
- U32x4(result)
+ Self(result)
}
}
@@ -165,7 +165,7 @@ impl State {
let n1 = u32::from_le_bytes([nonce.0[4], nonce.0[5], nonce.0[6], nonce.0[7]]);
let n2 = u32::from_le_bytes([nonce.0[8], nonce.0[9], nonce.0[10], nonce.0[11]]);
- State {
+ Self {
matrix: [
U32x4([WORD_1, WORD_2, WORD_3, WORD_4]),
U32x4([k0, k1, k2, k3]),
@@ -268,12 +268,12 @@ impl ChaCha20 {
pub const fn new(key: Key, nonce: Nonce, seek: u32) -> Self {
let block_count = seek / 64;
let seek_offset_bytes = (seek % 64) as usize;
- ChaCha20 { key, nonce, block_count, seek_offset_bytes }
+ Self { key, nonce, block_count, seek_offset_bytes }
}
/// Make a new instance of ChaCha20 from a block in the keystream.
pub const fn new_from_block(key: Key, nonce: Nonce, block: u32) -> Self {
- ChaCha20 { key, nonce, block_count: block, seek_offset_bytes: 0 }
+ Self { key, nonce, block_count: block, seek_offset_bytes: 0 }
}
/// Gets the keystream for a specific block.
diff --git a/chacha20_poly1305/src/lib.rs b/chacha20_poly1305/src/lib.rs
index cb887a36..88283d4b 100644
--- a/chacha20_poly1305/src/lib.rs
+++ b/chacha20_poly1305/src/lib.rs
@@ -43,7 +43,7 @@ pub enum Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
- Error::UnauthenticatedAdditionalData => write!(f, "Unauthenticated aad."),
+ Self::UnauthenticatedAdditionalData => write!(f, "Unauthenticated aad."),
}
}
}
@@ -52,7 +52,7 @@ impl fmt::Display for Error {
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
- Error::UnauthenticatedAdditionalData => None,
+ Self::UnauthenticatedAdditionalData => None,
}
}
}
@@ -65,7 +65,7 @@ pub struct ChaCha20Poly1305 {
impl ChaCha20Poly1305 {
/// Make a new instance of a ChaCha20Poly1305 AEAD.
- pub const fn new(key: Key, nonce: Nonce) -> Self { ChaCha20Poly1305 { key, nonce } }
+ pub const fn new(key: Key, nonce: Nonce) -> Self { Self { key, nonce } }
/// Encrypt content in place and return the Poly1305 16-byte authentication tag.
///
diff --git a/chacha20_poly1305/src/poly1305.rs b/chacha20_poly1305/src/poly1305.rs
index c34ba3f8..cf560bd0 100644
--- a/chacha20_poly1305/src/poly1305.rs
+++ b/chacha20_poly1305/src/poly1305.rs
@@ -41,7 +41,7 @@ impl Poly1305 {
let s2 = u32::from_le_bytes([key[24], key[25], key[26], key[27]]);
let s3 = u32::from_le_bytes([key[28], key[29], key[30], key[31]]);
- Poly1305 {
+ Self {
r: [r0, r1, r2, r3, r4],
s: [s0, s1, s2, s3],
acc: [0; 5],
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.