Skip to content

Commit 41aa651

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 41aa651

Some content is hidden

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

68 files changed

+399
-233
lines changed

.github/workflows/blobby.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: blobby
22

33
on:
44
pull_request:
5-
paths:
6-
- "blobby/**"
7-
- "Cargo.*"
5+
paths:
6+
- "blobby/**"
7+
- "Cargo.*"
88
push:
99
branches: master
1010

@@ -38,11 +38,9 @@ jobs:
3838
- uses: dtolnay/rust-toolchain@master
3939
with:
4040
toolchain: ${{ matrix.rust }}
41-
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
42-
- run: rm ../Cargo.toml
4341
- run: cargo test
4442

4543
minimal-versions:
4644
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
4745
with:
48-
working-directory: ${{ github.workflow }}
46+
working-directory: ${{ github.workflow }}

.github/workflows/cpufeatures.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
minimal-versions:
2929
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
3030
with:
31-
working-directory: ${{ github.workflow }}
31+
working-directory: ${{ github.workflow }}
3232

3333
# Linux tests
3434
linux:
@@ -56,8 +56,6 @@ jobs:
5656
with:
5757
toolchain: ${{ matrix.rust }}
5858
targets: ${{ matrix.target }}
59-
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
60-
- run: rm ../Cargo.toml
6159
- run: ${{ matrix.deps }}
6260
- run: cargo test --target ${{ matrix.target }}
6361

@@ -76,8 +74,6 @@ jobs:
7674
with:
7775
toolchain: ${{ matrix.toolchain }}
7876
targets: x86_64-apple-darwin
79-
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
80-
- run: rm ../Cargo.toml
8177
- run: cargo test
8278

8379
# Windows tests
@@ -98,8 +94,6 @@ jobs:
9894
with:
9995
toolchain: ${{ matrix.toolchain }}
10096
targets: ${{ matrix.target }}
101-
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
102-
- run: rm ../Cargo.toml
10397
- run: cargo test --target ${{ matrix.target }}
10498

10599
# Cross-compiled tests
@@ -121,8 +115,6 @@ jobs:
121115
toolchain: ${{ matrix.rust }}
122116
targets: ${{ matrix.target }}
123117
- uses: RustCrypto/actions/cross-install@master
124-
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
125-
- run: rm ../Cargo.toml
126118
- run: cross test --target ${{ matrix.target }}
127119

128120
# Build-only tests

.github/workflows/hex-literal.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: hex-literal
22

33
on:
44
pull_request:
5-
paths:
6-
- "hex-literal/**"
7-
- "Cargo.*"
5+
paths:
6+
- "hex-literal/**"
7+
- "Cargo.*"
88
push:
99
branches: master
1010

@@ -42,14 +42,12 @@ jobs:
4242
with:
4343
toolchain: ${{ matrix.rust }}
4444
targets: ${{ matrix.target }}
45-
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
46-
- run: rm ../Cargo.toml
4745
- run: cargo build --target ${{ matrix.target }}
4846

4947
minimal-versions:
5048
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
5149
with:
52-
working-directory: ${{ github.workflow }}
50+
working-directory: ${{ github.workflow }}
5351

5452
test:
5553
runs-on: ubuntu-latest
@@ -64,6 +62,4 @@ jobs:
6462
- uses: dtolnay/rust-toolchain@master
6563
with:
6664
toolchain: ${{ matrix.rust }}
67-
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
68-
- run: rm ../Cargo.toml
6965
- run: cargo test

.github/workflows/opaque-debug.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: opaque-debug
22

33
on:
44
pull_request:
5-
paths:
6-
- "opaque-debug/**"
7-
- "Cargo.*"
5+
paths:
6+
- "opaque-debug/**"
7+
- "Cargo.*"
88
push:
99
branches: master
1010

@@ -42,14 +42,12 @@ jobs:
4242
with:
4343
toolchain: ${{ matrix.rust }}
4444
targets: ${{ matrix.target }}
45-
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
46-
- run: rm ../Cargo.toml
4745
- run: cargo build --target ${{ matrix.target }}
4846

4947
minimal-versions:
5048
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
5149
with:
52-
working-directory: ${{ github.workflow }}
50+
working-directory: ${{ github.workflow }}
5351

5452
test:
5553
runs-on: ubuntu-latest
@@ -64,6 +62,4 @@ jobs:
6462
- uses: dtolnay/rust-toolchain@master
6563
with:
6664
toolchain: ${{ matrix.rust }}
67-
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
68-
- run: rm ../Cargo.toml
6965
- run: cargo test

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

0 commit comments

Comments
 (0)