Skip to content

Commit e8b32f8

Browse files
committed
Cargo.toml: add workspace-level clippy config
Adds a set of workspace-level lints including common ones we've used in the past and others collected via running `clippy::pedantic` on several of our crates and looking for interesting ones. Workspace-level lints require explict opt-in from each crate's Cargo.toml, so there's nothing forcing any crate to use them: [lints] workspace = true That said, I have opted every crate in the workspace into these lints and gotten them all to pass except for `wycheproof2blb` which is an internal utility tool and I didn't feel like it was worth the hassle. Even then, the lints are all set to `"warn"`, so they can be easily overridden by `#[allow(...)]` attributes, including `#![allow(...)]` to shut them off at the granularity of an entire crate. I managed to fix most of the issues that `cargo clippy --fix` didn't fix automatically including some missing documentation and documentation formatting issues. The main thing I didn't fix was adding missing `SAFETY` comments, which is a problem with a few of the crates in this repo. I disabled a lint at the crate level and added a TODO where we're missing them and it wasn't easily corrected.
1 parent df664d4 commit e8b32f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+386
-202
lines changed

Cargo.toml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,43 @@ opt-level = 2
2929

3030
[patch.crates-io]
3131
cmov = { path = "cmov" }
32+
33+
[workspace.lints.clippy]
34+
borrow_as_ptr = "warn"
35+
cast_lossless = "warn"
36+
cast_possible_truncation = "warn"
37+
cast_possible_wrap = "warn"
38+
cast_precision_loss = "warn"
39+
cast_sign_loss = "warn"
40+
checked_conversions = "warn"
41+
doc_markdown = "warn"
42+
from_iter_instead_of_collect = "warn"
43+
manual_assert = "warn"
44+
map_unwrap_or = "warn"
45+
missing_errors_doc = "warn"
46+
missing_panics_doc = "warn"
47+
mod_module_files = "warn"
48+
must_use_candidate = "warn"
49+
implicit_saturating_sub = "warn"
50+
panic_in_result_fn = "warn"
51+
ptr_as_ptr = "warn"
52+
redundant_closure_for_method_calls = "warn"
53+
ref_as_ptr = "warn"
54+
return_self_not_must_use = "warn"
55+
semicolon_if_nothing_returned = "warn"
56+
trivially_copy_pass_by_ref = "warn"
57+
std_instead_of_alloc = "warn"
58+
std_instead_of_core = "warn"
59+
undocumented_unsafe_blocks = "warn"
60+
unnecessary_safety_comment = "warn"
61+
unwrap_in_result = "warn"
62+
unwrap_used = "warn"
63+
64+
[workspace.lints.rust]
65+
missing_copy_implementations = "warn"
66+
missing_debug_implementations = "warn"
67+
missing_docs = "warn"
68+
trivial_casts = "warn"
69+
trivial_numeric_casts = "warn"
70+
unused_lifetimes = "warn"
71+
unused_qualifications = "warn"

blobby/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ description = "Iterator over simple binary blob storage"
1313

1414
[features]
1515
alloc = []
16+
17+
[lints]
18+
workspace = true

blobby/src/bin/decode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Encoding utility
2-
use std::error::Error;
2+
use core::error::Error;
33

44
#[cfg(not(feature = "alloc"))]
55
fn main() -> Result<(), Box<dyn Error>> {

blobby/src/bin/encode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Encoding utility
2-
use std::error::Error;
2+
use core::error::Error;
33

44
#[cfg(not(feature = "alloc"))]
55
fn main() -> Result<(), Box<dyn Error>> {

blobby/src/decode.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ macro_rules! try_read_vlq {
5151
}
5252

5353
/// Blobby file header
54+
#[derive(Clone, Copy, Debug)]
5455
pub struct Header {
5556
/// Number of blobs stored in the file
5657
pub items_len: usize,
@@ -59,7 +60,10 @@ pub struct Header {
5960
}
6061

6162
impl Header {
62-
/// Parse blobby header
63+
/// Parse blobby header.
64+
///
65+
/// # Errors
66+
/// - If data could not be parsed successfully.
6367
pub const fn parse(data: &mut &[u8]) -> Result<Self, Error> {
6468
match (read_vlq(data), read_vlq(data)) {
6569
(Ok(items_len), Ok(dedup_len)) => Ok(Header {
@@ -72,6 +76,9 @@ impl Header {
7276
}
7377

7478
/// Parse blobby data into an array.
79+
///
80+
/// # Errors
81+
/// - If data could not be parsed successfully.
7582
pub const fn parse_into_array<const ITEMS_LEN: usize, const DEDUP_LEN: usize>(
7683
mut data: &[u8],
7784
) -> Result<[&[u8]; ITEMS_LEN], Error> {
@@ -127,7 +134,11 @@ pub const fn parse_into_array<const ITEMS_LEN: usize, const DEDUP_LEN: usize>(
127134
}
128135

129136
/// Parse blobby data into a vector of slices.
137+
///
138+
/// # Errors
139+
/// - if data failed to parse successfully
130140
#[cfg(feature = "alloc")]
141+
#[allow(clippy::missing_panics_doc, clippy::panic_in_result_fn)]
131142
pub fn parse_into_vec(mut data: &[u8]) -> Result<alloc::vec::Vec<&[u8]>, Error> {
132143
use alloc::{vec, vec::Vec};
133144

@@ -175,6 +186,7 @@ pub fn parse_into_vec(mut data: &[u8]) -> Result<alloc::vec::Vec<&[u8]>, Error>
175186
Ok(res)
176187
}
177188

189+
/// Parse data into a slice.
178190
#[macro_export]
179191
macro_rules! parse_into_slice {
180192
($data:expr) => {{
@@ -195,6 +207,7 @@ macro_rules! parse_into_slice {
195207
}};
196208
}
197209

210+
/// Parse data into structs.
198211
#[macro_export]
199212
macro_rules! parse_into_structs {
200213
(

blobby/src/encode.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::{NEXT_MASK, VAL_MASK};
33
/// Write a git-flavoured VLQ value into `buf`.
44
///
55
/// Returns the slice within `buf` that holds the value.
6+
#[allow(clippy::cast_possible_truncation)]
67
fn encode_vlq(mut val: usize, buf: &mut [u8; 4]) -> &[u8] {
78
macro_rules! step {
89
($n:expr) => {
@@ -40,6 +41,7 @@ fn encode_vlq(mut val: usize, buf: &mut [u8; 4]) -> &[u8] {
4041
/// - (J << 1) & 0x01: indicates this blob is index entry J
4142
/// - (L << 1) & 0x00: indicates an explicit blob of len L
4243
/// - (in the latter case) explicit blob contents (L bytes)
44+
#[allow(clippy::missing_panics_doc, clippy::unwrap_used)]
4345
pub fn encode_blobs<T>(blobs: &[T]) -> (alloc::vec::Vec<u8>, usize)
4446
where
4547
T: AsRef<[u8]>,
@@ -49,7 +51,7 @@ where
4951
let mut dedup_map = BTreeMap::new();
5052
blobs
5153
.iter()
52-
.map(|v| v.as_ref())
54+
.map(AsRef::as_ref)
5355
.filter(|blob| !blob.is_empty())
5456
.for_each(|blob| {
5557
let v = dedup_map.entry(blob.as_ref()).or_insert(0);
@@ -89,7 +91,7 @@ where
8991
out_buf.extend_from_slice(e);
9092
}
9193

92-
for blob in blobs.iter().map(|v| v.as_ref()) {
94+
for blob in blobs.iter().map(AsRef::as_ref) {
9395
if let Some(dup_pos) = rev_idx.get(blob) {
9496
let n = (dup_pos << 1) + 1usize;
9597
out_buf.extend_from_slice(encode_vlq(n, &mut buf));
@@ -104,6 +106,7 @@ where
104106
}
105107

106108
#[cfg(test)]
109+
#[allow(clippy::cast_possible_truncation, clippy::unwrap_used)]
107110
mod tests {
108111
use crate::{Error, NEXT_MASK, VAL_MASK, decode::read_vlq};
109112

blobby/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![cfg(feature = "alloc")]
2+
#![allow(missing_docs, clippy::panic_in_result_fn)]
23

34
const ITEMS_LEN: usize = 10;
45
const DEDUP_LEN: usize = 3;

block-buffer/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ zeroize = { version = "1.4", optional = true, default-features = false }
1818

1919
[dev-dependencies]
2020
hex-literal = "1"
21+
22+
[lints]
23+
workspace = true

block-buffer/src/lib.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
3939
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
4040
)]
41-
#![warn(missing_docs)]
41+
#![allow(clippy::undocumented_unsafe_blocks)] // TODO(tarcieri): document all unsafe blocks
4242

4343
pub use hybrid_array as array;
4444

@@ -94,12 +94,8 @@ pub struct BlockBuffer<BS: ArraySize, K: BufferKind> {
9494
impl<BS: ArraySize, K: BufferKind> BlockBuffer<BS, K> {
9595
/// This associated constant is used to assert block size correctness at compile time.
9696
const BLOCK_SIZE_ASSERT: bool = {
97-
if BS::USIZE == 0 {
98-
panic!("Block size can not be equal to zero!");
99-
}
100-
if BS::USIZE > 255 {
101-
panic!("Block size can not be bigger than 255!");
102-
}
97+
assert!(BS::USIZE != 0, "Block size can not be equal to zero!");
98+
assert!(BS::USIZE <= 255, "Block size can not be bigger than 255!");
10399
true
104100
};
105101
}
@@ -140,16 +136,22 @@ impl<BS: ArraySize, K: BufferKind> BlockBuffer<BS, K> {
140136
/// # Panics
141137
/// If slice length is not valid for used buffer kind.
142138
#[inline(always)]
139+
#[must_use]
140+
#[track_caller]
143141
pub fn new(buf: &[u8]) -> Self {
144-
Self::try_new(buf).unwrap()
142+
Self::try_new(buf).expect("invalid slice length for buffer kind")
145143
}
146144

147145
/// Create new buffer from slice.
148146
///
149-
/// Returns an error if slice length is not valid for used buffer kind.
147+
/// # Errors
148+
/// - if slice length is not valid for used buffer kind.
150149
#[inline(always)]
151150
pub fn try_new(buf: &[u8]) -> Result<Self, Error> {
152-
assert!(Self::BLOCK_SIZE_ASSERT);
151+
const {
152+
assert!(Self::BLOCK_SIZE_ASSERT);
153+
}
154+
153155
if !K::invariant(buf.len(), BS::USIZE) {
154156
return Err(Error);
155157
}
@@ -292,7 +294,7 @@ impl<BS: ArraySize, K: BufferKind> BlockBuffer<BS, K> {
292294
#[inline(always)]
293295
unsafe fn set_pos_unchecked(&mut self, pos: usize) {
294296
debug_assert!(K::invariant(pos, BS::USIZE));
295-
K::set_pos(&mut self.buffer, &mut self.pos, pos)
297+
K::set_pos(&mut self.buffer, &mut self.pos, pos);
296298
}
297299

298300
/// Set buffer data.
@@ -322,6 +324,7 @@ where
322324
Sum<BS, K::Overhead>: ArraySize,
323325
{
324326
/// Serialize buffer into a byte array.
327+
#[allow(clippy::missing_panics_doc)]
325328
pub fn serialize(&self) -> SerializedBuffer<BS, K> {
326329
let mut buf = SerializedBuffer::<BS, K>::default();
327330
let data = self.get_data();
@@ -332,6 +335,9 @@ where
332335
}
333336

334337
/// Deserialize buffer from a byte array.
338+
///
339+
/// # Errors
340+
/// - If algorithm-specific invariant fails to hold
335341
pub fn deserialize(buf: &SerializedBuffer<BS, K>) -> Result<Self, Error> {
336342
let (pos, block) = buf.split_at(1);
337343
let pos = usize::from(pos[0]);
@@ -366,9 +372,7 @@ impl<BS: ArraySize> BlockBuffer<BS, Eager> {
366372
suffix: &[u8],
367373
mut compress: impl FnMut(&Array<u8, BS>),
368374
) {
369-
if suffix.len() > BS::USIZE {
370-
panic!("suffix is too long");
371-
}
375+
assert!(suffix.len() <= BS::USIZE, "suffix is too long");
372376
let pos = self.get_pos();
373377
let mut buf = self.pad_with_zeros();
374378
buf[pos] = delim;

block-buffer/src/read.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ impl<BS: ArraySize> ReadBuffer<BS> {
7373
/// # Safety
7474
/// `pos` must be smaller than or equal to the buffer block size and be bigger than zero.
7575
#[inline(always)]
76+
#[allow(clippy::cast_possible_truncation)]
7677
unsafe fn set_pos_unchecked(&mut self, pos: usize) {
7778
debug_assert!(pos != 0 && pos <= BS::USIZE);
7879
self.buffer[0] = pos as u8;
@@ -141,7 +142,7 @@ impl<BS: ArraySize> ReadBuffer<BS> {
141142
}
142143

143144
self.write_block(tail.len(), gen_block, |tail_ks| {
144-
tail.copy_from_slice(tail_ks)
145+
tail.copy_from_slice(tail_ks);
145146
});
146147
}
147148

@@ -158,6 +159,10 @@ impl<BS: ArraySize> ReadBuffer<BS> {
158159
}
159160

160161
/// Deserialize buffer from a byte array.
162+
///
163+
/// # Errors
164+
/// - If the first byte is `0`.
165+
/// - If the first byte is bigger than `BS`.
161166
#[inline]
162167
pub fn deserialize(buffer: &Array<u8, BS>) -> Result<Self, Error> {
163168
let pos = usize::from(buffer[0]);

0 commit comments

Comments
 (0)