Skip to content

Commit

Permalink
Merge pull request #149 from GnomedDev/fix-clippy-pedantic
Browse files Browse the repository at this point in the history
Fix clippy::pedantic
  • Loading branch information
lulf authored Aug 1, 2024
2 parents b9eed7f + cd8d3b8 commit 0e2b640
Show file tree
Hide file tree
Showing 35 changed files with 155 additions and 121 deletions.
28 changes: 19 additions & 9 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,28 @@ on:
# Also on PRs, just be careful not to publish anything
pull_request:


jobs:
ci:
runs-on: ubuntu-22.04
library-ci:
runs-on: ubuntu-latest
strategy:
matrix:
subcmd: [build, clippy, test]

steps:
- uses: actions/checkout@v4

- name: Build
run: cargo check
- name: Run `cargo ${{ matrix.subcmd }}` for library
run: cargo ${{ matrix.subcmd }}

example-ci:
runs-on: ubuntu-latest
strategy:
matrix:
example: [embassy, blocking, tokio, nrf52]

- name: Test
run: cargo test -- --nocapture
steps:
- uses: actions/checkout@v4

- name: Build examples
run: for i in embassy blocking tokio nrf52; do pushd examples/$i; cargo build; popd; done;
- name: Run `cargo build` for example ${{ matrix.example }}
working-directory: examples/${{ matrix.example }}
run: cargo build
3 changes: 3 additions & 0 deletions src/alert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum AlertLevel {
}

impl AlertLevel {
#[must_use]
pub fn of(num: u8) -> Option<Self> {
match num {
1 => Some(AlertLevel::Warning),
Expand Down Expand Up @@ -52,6 +53,7 @@ pub enum AlertDescription {
}

impl AlertDescription {
#[must_use]
pub fn of(num: u8) -> Option<Self> {
match num {
0 => Some(AlertDescription::CloseNotify),
Expand Down Expand Up @@ -94,6 +96,7 @@ pub struct Alert {
}

impl Alert {
#[must_use]
pub fn new(level: AlertLevel, description: AlertDescription) -> Self {
Self { level, description }
}
Expand Down
2 changes: 1 addition & 1 deletion src/application_data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::buffer::*;
use crate::buffer::CryptoBuffer;
use crate::record::RecordHeader;
use core::fmt::{Debug, Formatter};

Expand Down
2 changes: 1 addition & 1 deletion src/asynch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::common::decrypted_buffer_info::DecryptedBufferInfo;
use crate::common::decrypted_read_handler::DecryptedReadHandler;
use crate::connection::*;
use crate::connection::{decrypt_record, Handshake, State};
use crate::key_schedule::KeySchedule;
use crate::key_schedule::{ReadKeySchedule, SharedState, WriteKeySchedule};
use crate::read_buffer::ReadBuffer;
Expand Down
2 changes: 1 addition & 1 deletion src/blocking.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::common::decrypted_buffer_info::DecryptedBufferInfo;
use crate::common::decrypted_read_handler::DecryptedReadHandler;
use crate::connection::*;
use crate::connection::{decrypt_record, Handshake, State};
use crate::key_schedule::KeySchedule;
use crate::key_schedule::{ReadKeySchedule, SharedState, WriteKeySchedule};
use crate::read_buffer::ReadBuffer;
Expand Down
4 changes: 2 additions & 2 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl<'b> CryptoBuffer<'b> {
}

pub fn truncate(&mut self, len: usize) {
self.truncate_internal(len)
self.truncate_internal(len);
}

pub fn len(&self) -> usize {
Expand Down Expand Up @@ -235,7 +235,7 @@ impl<'b> Buffer for CryptoBuffer<'b> {
}

fn truncate(&mut self, len: usize) {
self.truncate_internal(len)
self.truncate_internal(len);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/change_cipher_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::TlsError;
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ChangeCipherSpec {}

#[allow(clippy::unnecessary_wraps)] // TODO
impl ChangeCipherSpec {
pub fn new() -> Self {
Self {}
Expand All @@ -21,8 +22,8 @@ impl ChangeCipherSpec {
Ok(Self {})
}

#[allow(dead_code)]
pub(crate) fn encode(&self, buf: &mut CryptoBuffer<'_>) -> Result<(), TlsError> {
#[allow(dead_code, clippy::unused_self)]
pub(crate) fn encode(self, buf: &mut CryptoBuffer<'_>) -> Result<(), TlsError> {
buf.push(1).map_err(|_| TlsError::EncodeError)?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/decrypted_read_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl DecryptedReadHandler<'_> {
// by the user.
Ok(())
}
_ => {
ServerRecord::Handshake(_) => {
unimplemented!()
}
}
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ where

#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[must_use = "TlsConfig does nothing unless consumed"]
pub struct TlsConfig<'a> {
pub(crate) server_name: Option<&'a str>,
pub(crate) psk: Option<(&'a [u8], Vec<&'a [u8], 4>)>,
Expand Down
8 changes: 4 additions & 4 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::record::{ClientRecord, ServerRecord};
use crate::record_reader::RecordReader;
use crate::write_buffer::WriteBuffer;
use crate::{
alert::*,
alert::{Alert, AlertDescription, AlertLevel},
handshake::{certificate::CertificateRef, certificate_request::CertificateRequest},
};
use crate::{CertificateVerify, CryptoProvider, TlsError, TlsVerifier};
Expand Down Expand Up @@ -551,11 +551,11 @@ where
Ok((mut signing_key, signature_scheme)) => {
let ctx_str = b"TLS 1.3, client CertificateVerify\x00";
let mut msg: heapless::Vec<u8, 130> = heapless::Vec::new();
msg.resize(64, 0x20).map_err(|_| TlsError::EncodeError)?;
msg.resize(64, 0x20).map_err(|()| TlsError::EncodeError)?;
msg.extend_from_slice(ctx_str)
.map_err(|_| TlsError::EncodeError)?;
.map_err(|()| TlsError::EncodeError)?;
msg.extend_from_slice(&key_schedule.transcript_hash().clone().finalize())
.map_err(|_| TlsError::EncodeError)?;
.map_err(|()| TlsError::EncodeError)?;

let signature = signing_key.sign(&msg);

Expand Down
1 change: 1 addition & 0 deletions src/crypto_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use p256::ecdh::SharedSecret;

pub struct CryptoEngine {}

#[allow(clippy::unused_self, clippy::needless_pass_by_value)] // TODO
impl CryptoEngine {
pub fn new(_group: NamedGroup, _shared: SharedSecret) -> Self {
Self {}
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/extension_data/key_share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'a, const N: usize> KeyShareClientHello<'a, N> {

pub fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.with_u16_length(|buf| {
for client_share in self.client_shares.iter() {
for client_share in &self.client_shares {
client_share.encode(buf)?;
}
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/extension_data/pre_shared_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<const N: usize> PreSharedKeyClientHello<'_, N> {

pub fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.with_u16_length(|buf| {
for identity in self.identities.iter() {
for identity in &self.identities {
buf.with_u16_length(|buf| buf.extend_from_slice(identity))
.map_err(|_| TlsError::EncodeError)?;

Expand Down Expand Up @@ -56,7 +56,7 @@ impl PreSharedKeyServerHello {
})
}

pub fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
pub fn encode(self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.push_u16(self.selected_identity)
.map_err(|_| TlsError::EncodeError)
}
Expand Down
6 changes: 3 additions & 3 deletions src/extensions/extension_data/psk_key_exchange_modes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ impl PskKeyExchangeMode {
}
}

fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.push(*self as u8).map_err(|_| TlsError::EncodeError)
fn encode(self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.push(self as u8).map_err(|_| TlsError::EncodeError)
}
}

Expand All @@ -44,7 +44,7 @@ impl<const N: usize> PskKeyExchangeModes<N> {

pub fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.with_u8_length(|buf| {
for mode in self.modes.iter() {
for mode in &self.modes {
mode.encode(buf)?;
}
Ok(())
Expand Down
13 changes: 7 additions & 6 deletions src/extensions/extension_data/server_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl NameType {
}
}

pub fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.push(*self as u8).map_err(|_| TlsError::EncodeError)
pub fn encode(self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.push(self as u8).map_err(|_| TlsError::EncodeError)
}
}

Expand Down Expand Up @@ -97,7 +97,7 @@ impl<'a, const N: usize> ServerNameList<'a, N> {

pub fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.with_u16_length(|buf| {
for name in self.names.iter() {
for name in &self.names {
name.encode(buf)?;
}

Expand All @@ -118,13 +118,14 @@ pub struct ServerNameResponse;

impl ServerNameResponse {
pub fn parse(buf: &mut ParseBuffer) -> Result<Self, ParseError> {
if !buf.is_empty() {
Err(ParseError::InvalidData)
} else {
if buf.is_empty() {
Ok(Self)
} else {
Err(ParseError::InvalidData)
}
}

#[allow(clippy::unused_self, clippy::unnecessary_wraps)]
pub fn encode(&self, _buf: &mut CryptoBuffer) -> Result<(), TlsError> {
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion src/extensions/extension_data/signature_algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl SignatureScheme {
}
}

#[must_use]
pub fn as_u16(self) -> u16 {
match self {
Self::RsaPkcs1Sha256 => 0x0401,
Expand Down Expand Up @@ -126,7 +127,7 @@ impl<const N: usize> SignatureAlgorithms<N> {

pub fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.with_u16_length(|buf| {
for &a in self.supported_signature_algorithms.iter() {
for &a in &self.supported_signature_algorithms {
buf.push_u16(a.as_u16())
.map_err(|_| TlsError::EncodeError)?;
}
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/extension_data/signature_algorithms_cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<const N: usize> SignatureAlgorithmsCert<N> {

pub fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.with_u16_length(|buf| {
for &a in self.supported_signature_algorithms.iter() {
for &a in &self.supported_signature_algorithms {
buf.push_u16(a.as_u16())
.map_err(|_| TlsError::EncodeError)?;
}
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/extension_data/supported_groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl NamedGroup {
}
}

pub fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
pub fn encode(self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.push_u16(self.as_u16())
.map_err(|_| TlsError::EncodeError)
}
Expand All @@ -81,7 +81,7 @@ impl<const N: usize> SupportedGroups<N> {

pub fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.with_u16_length(|buf| {
for g in self.supported_groups.iter() {
for g in &self.supported_groups {
g.encode(buf)?;
}
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions src/extensions/extension_data/supported_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use heapless::Vec;
pub struct ProtocolVersion(u16);

impl ProtocolVersion {
pub fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
pub fn encode(self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.push_u16(self.0).map_err(|_| TlsError::EncodeError)
}

Expand Down Expand Up @@ -38,7 +38,7 @@ impl<const N: usize> SupportedVersionsClientHello<N> {

pub fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.with_u8_length(|buf| {
for v in self.versions.iter() {
for v in &self.versions {
v.encode(buf)?;
}
Ok(())
Expand All @@ -59,7 +59,7 @@ impl SupportedVersionsServerHello {
})
}

pub fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
pub fn encode(self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
self.selected_version.encode(buf)
}
}
1 change: 1 addition & 0 deletions src/extensions/extension_data/unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Unimplemented<'a> {
}

impl<'a> Unimplemented<'a> {
#[allow(clippy::unnecessary_wraps)]
pub fn parse(buf: &mut ParseBuffer<'a>) -> Result<Self, ParseError> {
Ok(Self {
data: buf.as_slice(),
Expand Down
5 changes: 2 additions & 3 deletions src/extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ impl ExtensionType {
}
}

pub fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.push_u16(*self as u16)
.map_err(|_| TlsError::EncodeError)
pub fn encode(self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
buf.push_u16(self as u16).map_err(|_| TlsError::EncodeError)
}
}
2 changes: 1 addition & 1 deletion src/handshake/binder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::buffer::*;
use crate::buffer::CryptoBuffer;
use crate::TlsError;
use core::fmt::{Debug, Formatter};
//use digest::generic_array::{ArrayLength, GenericArray};
Expand Down
6 changes: 3 additions & 3 deletions src/handshake/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'a> CertificateRef<'a> {
pub(crate) fn encode(&self, buf: &mut CryptoBuffer<'_>) -> Result<(), TlsError> {
buf.with_u8_length(|buf| buf.extend_from_slice(self.request_context))?;
buf.with_u24_length(|buf| {
for entry in self.entries.iter() {
for entry in &self.entries {
entry.encode(buf)?;
}
Ok(())
Expand Down Expand Up @@ -146,11 +146,11 @@ impl<'a, const N: usize> TryFrom<CertificateRef<'a>> for Certificate<N> {
let mut request_context = Vec::new();
request_context
.extend_from_slice(cert.request_context)
.map_err(|_| TlsError::OutOfMemory)?;
.map_err(|()| TlsError::OutOfMemory)?;
let mut entries_data = Vec::new();
entries_data
.extend_from_slice(cert.raw_entries)
.map_err(|_| TlsError::OutOfMemory)?;
.map_err(|()| TlsError::OutOfMemory)?;

Ok(Self {
request_context,
Expand Down
2 changes: 1 addition & 1 deletion src/handshake/certificate_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<'a> TryFrom<CertificateRequestRef<'a>> for CertificateRequest {
let mut request_context = Vec::new();
request_context
.extend_from_slice(cert.request_context)
.map_err(|_| {
.map_err(|()| {
error!("CertificateRequest: InsufficientSpace");
TlsError::InsufficientSpace
})?;
Expand Down
2 changes: 1 addition & 1 deletion src/handshake/client_hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::extensions::messages::ClientHelloExtension;
use crate::handshake::{Random, LEGACY_VERSION};
use crate::key_schedule::{HashOutputSize, WriteKeySchedule};
use crate::TlsError;
use crate::{buffer::*, CryptoProvider};
use crate::{buffer::CryptoBuffer, CryptoProvider};

pub struct ClientHello<'config, CipherSuite>
where
Expand Down
2 changes: 1 addition & 1 deletion src/handshake/finished.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::buffer::*;
use crate::buffer::CryptoBuffer;
use crate::parse_buffer::ParseBuffer;
use crate::TlsError;
use core::fmt::{Debug, Formatter};
Expand Down
Loading

0 comments on commit 0e2b640

Please sign in to comment.