Skip to content

Commit f362dac

Browse files
authored
der: add workspace-level clippy config (#2231)
Applies the workspace-level lints we've configured in other repos to this repo, and enables them for the `der` crate, then fixes the lint failures, either automatically or by adding the appropriate documentation.
1 parent b5b3ee2 commit f362dac

50 files changed

Lines changed: 598 additions & 77 deletions

Some content is hidden

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

.clippy.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
allow-unwrap-in-consts = true
2+
allow-unwrap-in-tests = true

Cargo.toml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,43 @@ tls_codec_derive = { path = "./tls_codec/derive" }
6060
x509-tsp = { path = "./x509-tsp" }
6161
x509-cert = { path = "./x509-cert" }
6262
x509-ocsp = { path = "./x509-ocsp" }
63+
64+
[workspace.lints.clippy]
65+
borrow_as_ptr = "warn"
66+
cast_lossless = "warn"
67+
cast_possible_truncation = "warn"
68+
cast_possible_wrap = "warn"
69+
cast_precision_loss = "warn"
70+
cast_sign_loss = "warn"
71+
checked_conversions = "warn"
72+
doc_markdown = "warn"
73+
from_iter_instead_of_collect = "warn"
74+
implicit_saturating_sub = "warn"
75+
manual_assert = "warn"
76+
map_unwrap_or = "warn"
77+
missing_errors_doc = "warn"
78+
missing_panics_doc = "warn"
79+
mod_module_files = "warn"
80+
must_use_candidate = "warn"
81+
needless_range_loop = "allow"
82+
ptr_as_ptr = "warn"
83+
redundant_closure_for_method_calls = "warn"
84+
ref_as_ptr = "warn"
85+
return_self_not_must_use = "warn"
86+
semicolon_if_nothing_returned = "warn"
87+
trivially_copy_pass_by_ref = "warn"
88+
std_instead_of_alloc = "warn"
89+
std_instead_of_core = "warn"
90+
undocumented_unsafe_blocks = "warn"
91+
unnecessary_safety_comment = "warn"
92+
unwrap_in_result = "warn"
93+
unwrap_used = "warn"
94+
95+
[workspace.lints.rust]
96+
missing_copy_implementations = "warn"
97+
missing_debug_implementations = "warn"
98+
missing_docs = "warn"
99+
trivial_casts = "warn"
100+
trivial_numeric_casts = "warn"
101+
unused_lifetimes = "warn"
102+
unused_qualifications = "warn"

der/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ oid = ["dep:const-oid"]
4444
pem = ["dep:pem-rfc7468", "alloc", "zeroize"]
4545
real = []
4646

47+
[lints]
48+
workspace = true
49+
4750
[package.metadata.docs.rs]
4851
all-features = true
4952
rustdoc-args = ["--cfg", "docsrs"]

der/src/asn1/any.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ impl<'a> AnyRef<'a> {
3737
};
3838

3939
/// Create a new [`AnyRef`] from the provided [`Tag`] and DER bytes.
40+
///
41+
/// # Errors
42+
/// Returns [`Error`] with [`ErrorKind::Length`] if `bytes` is too long.
4043
pub const fn new(tag: Tag, bytes: &'a [u8]) -> Result<Self, Error> {
4144
match BytesRef::new(bytes) {
4245
Ok(value) => Ok(Self { tag, value }),
@@ -50,16 +53,21 @@ impl<'a> AnyRef<'a> {
5053
}
5154

5255
/// Get the raw value for this [`AnyRef`] type as a byte slice.
56+
#[must_use]
5357
pub fn value(self) -> &'a [u8] {
5458
self.value.as_slice()
5559
}
5660

5761
/// Returns [`Tag`] and [`Length`] of self.
62+
#[must_use]
5863
pub fn header(&self) -> Header {
5964
Header::new(self.tag, self.value.len())
6065
}
6166

6267
/// Attempt to decode this [`AnyRef`] type into the inner value.
68+
///
69+
/// # Errors
70+
/// Returns `T::Error` if a decoding error occurred.
6371
pub fn decode_as<T>(self) -> Result<T, <T as DecodeValue<'a>>::Error>
6472
where
6573
T: Choice<'a> + DecodeValue<'a>,
@@ -68,6 +76,9 @@ impl<'a> AnyRef<'a> {
6876
}
6977

7078
/// Attempt to decode this [`AnyRef`] type into the inner value.
79+
///
80+
/// # Errors
81+
/// Returns `T::Error` if a decoding error occurred.
7182
pub fn decode_as_encoding<T>(
7283
self,
7384
encoding: EncodingRules,
@@ -86,12 +97,16 @@ impl<'a> AnyRef<'a> {
8697
}
8798

8899
/// Is this value an ASN.1 `NULL` value?
100+
#[must_use]
89101
pub fn is_null(self) -> bool {
90102
self == Self::NULL
91103
}
92104

93105
/// Attempt to decode this value an ASN.1 `SEQUENCE`, creating a new
94106
/// nested reader and calling the provided argument with it.
107+
///
108+
/// # Errors
109+
/// Returns `E` in the event an error is returned from `F` or if a decoding error occurs.
95110
pub fn sequence<F, T, E>(self, f: F) -> Result<T, E>
96111
where
97112
F: FnOnce(&mut SliceReader<'a>) -> Result<T, E>,
@@ -192,22 +207,30 @@ mod allocating {
192207

193208
impl Any {
194209
/// Create a new [`Any`] from the provided [`Tag`] and DER bytes.
210+
///
211+
/// # Errors
212+
/// If `bytes` is too long.
195213
pub fn new(tag: Tag, bytes: impl Into<Box<[u8]>>) -> Result<Self, Error> {
196214
let value = BytesOwned::new(bytes)?;
197215
Ok(Self { tag, value })
198216
}
199217

200218
/// Allow access to value
219+
#[must_use]
201220
pub fn value(&self) -> &[u8] {
202221
self.value.as_slice()
203222
}
204223

205224
/// Returns [`Tag`] and [`Length`] of self.
225+
#[must_use]
206226
pub fn header(&self) -> Header {
207227
Header::new(self.tag, self.value.len())
208228
}
209229

210230
/// Attempt to decode this [`Any`] type into the inner value.
231+
///
232+
/// # Errors
233+
/// Returns `T::Error` if a decoding error occurred.
211234
pub fn decode_as<'a, T>(&'a self) -> Result<T, <T as DecodeValue<'a>>::Error>
212235
where
213236
T: Choice<'a> + DecodeValue<'a>,
@@ -216,6 +239,9 @@ mod allocating {
216239
}
217240

218241
/// Attempt to decode this [`Any`] type into the inner value with the given encoding rules.
242+
///
243+
/// # Errors
244+
/// Returns `T::Error` if a decoding error occurred.
219245
pub fn decode_as_encoding<'a, T>(
220246
&'a self,
221247
encoding: EncodingRules,
@@ -227,6 +253,9 @@ mod allocating {
227253
}
228254

229255
/// Encode the provided type as an [`Any`] value.
256+
///
257+
/// # Errors
258+
/// If an encoding error occurred.
230259
pub fn encode_from<T>(msg: &T) -> Result<Self, Error>
231260
where
232261
T: Tagged + EncodeValue,
@@ -239,6 +268,9 @@ mod allocating {
239268

240269
/// Attempt to decode this value an ASN.1 `SEQUENCE`, creating a new
241270
/// nested reader and calling the provided argument with it.
271+
///
272+
/// # Errors
273+
/// If a decoding error occurred.
242274
pub fn sequence<'a, F, T, E>(&'a self, f: F) -> Result<T, E>
243275
where
244276
F: FnOnce(&mut SliceReader<'a>) -> Result<T, E>,
@@ -248,6 +280,7 @@ mod allocating {
248280
}
249281

250282
/// [`Any`] representation of the ASN.1 `NULL` type.
283+
#[must_use]
251284
pub fn null() -> Self {
252285
Self {
253286
tag: Tag::Null,
@@ -256,6 +289,7 @@ mod allocating {
256289
}
257290

258291
/// Create a new [`AnyRef`] from the provided [`Any`] owned tag and bytes.
292+
#[must_use]
259293
pub fn to_ref(&self) -> AnyRef<'_> {
260294
AnyRef {
261295
tag: self.tag,
@@ -350,6 +384,7 @@ mod allocating {
350384

351385
impl Any {
352386
/// Is this value an ASN.1 `NULL` value?
387+
#[must_use]
353388
pub fn is_null(&self) -> bool {
354389
self.owned_to_ref() == AnyRef::NULL
355390
}

0 commit comments

Comments
 (0)