Skip to content

Commit 8b705e9

Browse files
committed
Merge rust-bitcoin#5436: internals,p2p: upgrade to workspace lint rules
d893038 p2p: upgrade to workspace lint rules (Nick Johnson) d8de11d internals: upgrade to workspace lint rules (Nick Johnson) Pull request description: These include all the clippy recommendations for the new rules. I had to ask the LLM the "but why" for a handful of these, but I think they all check out. ACKs for top commit: apoelstra: ACK d893038; successfully ran local tests Tree-SHA512: 4749f19e32beaf4c7901f7d035f41d7317b76aaa3b4a20a9d6f09332e280081a69063f1891bb7698f8952e0a2b9680722da3ec29ec0dc1c1bd466514c365541a
2 parents 1950199 + d893038 commit 8b705e9

File tree

18 files changed

+140
-135
lines changed

18 files changed

+140
-135
lines changed

internals/Cargo.toml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,5 @@ bincode = { version = "1.3.1", optional = true }
3434
all-features = true
3535
rustdoc-args = ["--cfg", "docsrs"]
3636

37-
[lints.rust]
38-
unexpected_cfgs = { level = "deny", check-cfg = ['cfg(kani)'] }
39-
40-
[lints.clippy]
41-
redundant_clone = "warn"
42-
use_self = "warn"
37+
[lints]
38+
workspace = true

internals/build.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ use std::io;
77

88
fn main() {
99
let rustc = std::env::var_os("RUSTC");
10-
let rustc = rustc.as_ref().map(std::path::Path::new).unwrap_or_else(|| "rustc".as_ref());
10+
let rustc = rustc.as_ref().map_or_else(|| "rustc".as_ref(), std::path::Path::new);
1111
let output = std::process::Command::new(rustc)
1212
.arg("--version")
1313
.output()
1414
.unwrap_or_else(|error| panic!("failed to run `{:?} --version`: {:?}", rustc, error));
1515
assert!(output.status.success(), "{:?} -- version returned non-zero exit code", rustc);
1616
let stdout = String::from_utf8(output.stdout).expect("rustc produced non-UTF-8 output");
1717
let version_prefix = "rustc ";
18-
if !stdout.starts_with(version_prefix) {
19-
panic!("unexpected rustc output: {}", stdout);
20-
}
18+
assert!(stdout.starts_with(version_prefix), "unexpected rustc output: {}", stdout);
2119

2220
let version = &stdout[version_prefix.len()..];
2321
let end = version.find(&[' ', '-'] as &[_]).unwrap_or(version.len());
@@ -32,7 +30,7 @@ fn main() {
3230
.expect("invalid Rust minor version");
3331

3432
let msrv = std::env::var("CARGO_PKG_RUST_VERSION").unwrap();
35-
let mut msrv = msrv.split(".");
33+
let mut msrv = msrv.split('.');
3634
let msrv_major = msrv.next().unwrap();
3735
assert_eq!(msrv_major, "1", "unexpected Rust major version");
3836
let msrv_minor = msrv.next().unwrap().parse::<u64>().unwrap();
@@ -75,7 +73,7 @@ fn write_macro(mut macro_file: impl io::Write, msrv_minor: u64, minor: u64) -> i
7573
writeln!(macro_file, " $($if_yes)*")?;
7674
writeln!(macro_file, " }};")?;
7775
}
78-
for version in (minor + 1)..(MAX_USED_VERSION + 1) {
76+
for version in (minor + 1)..=MAX_USED_VERSION {
7977
writeln!(
8078
macro_file,
8179
" (if >= 1.{} {{ $($if_yes:tt)* }} $(else {{ $($if_no:tt)* }})?) => {{",

internals/src/array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<const N: usize, T> ArrayExt for [T; N] {
7777

7878
fn sub_array<const OFFSET: usize, const LEN: usize>(&self) -> &[Self::Item; LEN] {
7979
#[allow(clippy::let_unit_value)]
80-
let _ = Hack::<N, OFFSET, LEN>::IS_VALID_RANGE;
80+
let () = Hack::<N, OFFSET, LEN>::IS_VALID_RANGE;
8181

8282
self[OFFSET..(OFFSET + LEN)].try_into().expect("this is also compiler-checked above")
8383
}
@@ -86,7 +86,7 @@ impl<const N: usize, T> ArrayExt for [T; N] {
8686
&self,
8787
) -> (&[Self::Item; LEFT], &[Self::Item; RIGHT]) {
8888
#[allow(clippy::let_unit_value)]
89-
let _ = Hack2::<N, LEFT, RIGHT>::IS_FULL_RANGE;
89+
let () = Hack2::<N, LEFT, RIGHT>::IS_FULL_RANGE;
9090

9191
(self.sub_array::<0, LEFT>(), self.sub_array::<LEFT, RIGHT>())
9292
}

internals/src/array_vec.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ mod safety_boundary {
7373
///
7474
/// # Returns
7575
///
76-
/// None if the ArrayVec is empty.
76+
/// None if the `ArrayVec` is empty.
7777
pub fn pop(&mut self) -> Option<T> {
7878
if self.len > 0 {
7979
self.len -= 1;
@@ -114,6 +114,7 @@ impl<T: Copy, const CAP: usize> Default for ArrayVec<T, CAP> {
114114
/// Because we avoid copying the uninitialized part of the array this copies the value faster than
115115
/// memcpy.
116116
#[allow(clippy::non_canonical_clone_impl)]
117+
#[allow(clippy::expl_impl_clone_on_copy)]
117118
impl<T: Copy, const CAP: usize> Clone for ArrayVec<T, CAP> {
118119
fn clone(&self) -> Self { Self::from_slice(self) }
119120
}
@@ -190,14 +191,14 @@ mod tests {
190191
}
191192

192193
#[test]
193-
#[should_panic]
194+
#[should_panic(expected = "assertion failed")]
194195
fn overflow_push() {
195196
let mut av = ArrayVec::<_, 0>::new();
196197
av.push(42);
197198
}
198199

199200
#[test]
200-
#[should_panic]
201+
#[should_panic(expected = "buffer overflow")]
201202
fn overflow_extend() {
202203
let mut av = ArrayVec::<_, 0>::new();
203204
av.extend_from_slice(&[42]);

internals/src/compact_size.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub const fn encoded_size_const(value: u64) -> usize {
4444
match value {
4545
0..=0xFC => 1,
4646
0xFD..=0xFFFF => 3,
47-
0x10000..=0xFFFFFFFF => 5,
47+
0x10000..=0xFFFF_FFFF => 5,
4848
_ => 9,
4949
}
5050
}
@@ -63,7 +63,7 @@ pub fn encode(value: impl ToU64) -> ArrayVec<u8, MAX_ENCODING_SIZE> {
6363
res.push(0xFD);
6464
res.extend_from_slice(&v.to_le_bytes());
6565
}
66-
0x10000..=0xFFFFFFFF => {
66+
0x10000..=0xFFFF_FFFF => {
6767
let v = value as u32; // Cast ok because of match.
6868
res.push(0xFE);
6969
res.extend_from_slice(&v.to_le_bytes());
@@ -88,16 +88,12 @@ pub fn encode(value: impl ToU64) -> ArrayVec<u8, MAX_ENCODING_SIZE> {
8888
/// * Panics in release mode if the `slice` does not contain a valid minimal compact size encoding.
8989
/// * Panics in debug mode if the encoding is not minimal (referred to as "non-canonical" in Core).
9090
pub fn decode_unchecked(slice: &mut &[u8]) -> u64 {
91-
if slice.is_empty() {
92-
panic!("tried to decode an empty slice");
93-
}
91+
assert!(!slice.is_empty(), "tried to decode an empty slice");
9492

9593
match slice[0] {
9694
0xFF => {
9795
const SIZE: usize = 9;
98-
if slice.len() < SIZE {
99-
panic!("slice too short, expected at least 9 bytes");
100-
};
96+
assert!(slice.len() >= SIZE, "slice too short, expected at least 9 bytes");
10197

10298
let mut bytes = [0_u8; SIZE - 1];
10399
bytes.copy_from_slice(&slice[1..SIZE]);
@@ -109,9 +105,7 @@ pub fn decode_unchecked(slice: &mut &[u8]) -> u64 {
109105
}
110106
0xFE => {
111107
const SIZE: usize = 5;
112-
if slice.len() < SIZE {
113-
panic!("slice too short, expected at least 5 bytes");
114-
};
108+
assert!(slice.len() >= SIZE, "slice too short, expected at least 5 bytes");
115109

116110
let mut bytes = [0_u8; SIZE - 1];
117111
bytes.copy_from_slice(&slice[1..SIZE]);
@@ -123,9 +117,7 @@ pub fn decode_unchecked(slice: &mut &[u8]) -> u64 {
123117
}
124118
0xFD => {
125119
const SIZE: usize = 3;
126-
if slice.len() < SIZE {
127-
panic!("slice too short, expected at least 3 bytes");
128-
};
120+
assert!(slice.len() >= SIZE, "slice too short, expected at least 3 bytes");
129121

130122
let mut bytes = [0_u8; SIZE - 1];
131123
bytes.copy_from_slice(&slice[1..SIZE]);
@@ -239,14 +231,14 @@ mod tests {
239231
}
240232

241233
#[test]
242-
#[should_panic]
234+
#[should_panic(expected = "tried to decode an empty slice")]
243235
fn decode_from_empty_slice_panics() {
244236
let mut slice = [].as_slice();
245237
let _ = decode_unchecked(&mut slice);
246238
}
247239

248240
#[test]
249-
#[should_panic]
241+
#[should_panic(expected = "slice too short")]
250242
// Non-minimal is referred to as non-canonical in Core (`bitcoin/src/serialize.h`).
251243
fn decode_non_minimal_panics() {
252244
let mut slice = [0xFE, 0xCD, 0xAB].as_slice();

internals/src/error/input_string.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ impl InputString {
6868
/// }
6969
/// }
7070
/// ```
71+
///
72+
/// # Errors
73+
///
74+
/// Returns an error if the write to the formatter fails.
7175
pub fn unknown_variant<T>(&self, what: &T, f: &mut fmt::Formatter) -> fmt::Result
7276
where
7377
T: fmt::Display + ?Sized,

internals/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
#![warn(missing_docs)]
1111
#![warn(deprecated_in_future)]
1212
#![doc(test(attr(warn(unused))))]
13-
// Exclude lints we don't think are valuable.
14-
#![allow(clippy::needless_question_mark)] // https://github.com/rust-bitcoin/rust-bitcoin/pull/2134
15-
#![allow(clippy::manual_range_contains)] // More readable than clippy's format.
16-
#![allow(clippy::uninlined_format_args)] // Allow `format!("{}", x)` instead of enforcing `format!("{x}")`
1713

1814
#[cfg(feature = "alloc")]
1915
extern crate alloc;

internals/src/script.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
///
77
/// A script push data instruction includes the length of the data being pushed, this function reads
88
/// that length from an iterator (encoded in either 1, 2, or 4 bytes).
9+
///
10+
/// # Errors
11+
///
12+
/// Returns an error if the iterator does not contain enough bytes to read the length.
913
// We internally use implementation based on iterator so that it automatically advances as needed.
1014
pub fn read_push_data_len(
1115
data: &mut core::slice::Iter<'_, u8>,
@@ -52,22 +56,22 @@ mod tests {
5256
let bytes = [0x01, 0x23, 0x45, 0x67];
5357
let want = u32::from_le_bytes([0x01, 0x23, 0x45, 0x67]);
5458
let got = read_push_data_len(&mut bytes.iter(), PushDataLenLen::Four).unwrap();
55-
assert_eq!(got, want as usize)
59+
assert_eq!(got, want as usize);
5660
}
5761

5862
#[test]
5963
fn reads_2_bytes() {
6064
let bytes = [0x01, 0x23];
6165
let want = u16::from_le_bytes([0x01, 0x23]);
6266
let got = read_push_data_len(&mut bytes.iter(), PushDataLenLen::Two).unwrap();
63-
assert_eq!(got, want as usize)
67+
assert_eq!(got, want as usize);
6468
}
6569

6670
#[test]
6771
fn reads_1_byte() {
6872
let bytes = [0x01];
6973
let want = 0x01;
7074
let got = read_push_data_len(&mut bytes.iter(), PushDataLenLen::One).unwrap();
71-
assert_eq!(got, want as usize)
75+
assert_eq!(got, want as usize);
7276
}
7377
}

internals/src/serde.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ pub trait IntoDeError: Sized {
1818
///
1919
/// If the error type doesn't contain enough information to explain the error precisely this
2020
/// should return `Err(self)` allowing the caller to use its information instead.
21+
///
22+
/// # Errors
23+
///
24+
/// Returns `Err(self)` if the error cannot be converted to a deserializer error.
2125
fn try_into_de_error<E>(self, expected: Option<&dyn de::Expected>) -> Result<E, Self>
2226
where
2327
E: de::Error,
@@ -27,7 +31,7 @@ pub trait IntoDeError: Sized {
2731
}
2832

2933
mod impls {
30-
use super::*;
34+
use super::{IntoDeError, de};
3135

3236
impl IntoDeError for core::convert::Infallible {
3337
fn into_de_error<E: de::Error>(self, _expected: Option<&dyn de::Expected>) -> E {
@@ -124,7 +128,7 @@ macro_rules! serde_string_impl {
124128
}
125129

126130
/// A combination macro where the human-readable serialization is done like
127-
/// serde_string_impl and the non-human-readable impl is done as a struct.
131+
/// `serde_string_impl` and the non-human-readable impl is done as a struct.
128132
#[macro_export]
129133
macro_rules! serde_struct_human_string_impl {
130134
($name:ident, $expecting:literal, $($fe:ident),*) => (

internals/src/slice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<T> SliceExt for [T] {
5858

5959
fn bitcoin_as_chunks<const N: usize>(&self) -> (&[[Self::Item; N]], &[Self::Item]) {
6060
#[allow(clippy::let_unit_value)]
61-
let _ = Hack::<N>::IS_NONZERO;
61+
let () = Hack::<N>::IS_NONZERO;
6262

6363
let chunks_count = self.len() / N;
6464
let total_left_len = chunks_count * N;
@@ -77,7 +77,7 @@ impl<T> SliceExt for [T] {
7777
&mut self,
7878
) -> (&mut [[Self::Item; N]], &mut [Self::Item]) {
7979
#[allow(clippy::let_unit_value)]
80-
let _ = Hack::<N>::IS_NONZERO;
80+
let () = Hack::<N>::IS_NONZERO;
8181

8282
let chunks_count = self.len() / N;
8383
let total_left_len = chunks_count * N;

0 commit comments

Comments
 (0)