Skip to content

Commit a1ac3fb

Browse files
committed
Merge #448: Add clippy to CI
65186e7 Add githooks (Tobin C. Harding) 6d76bd4 Add clippy to CI (Tobin C. Harding) 9f1ebb9 Allow nonminimal_bool in unit test (Tobin C. Harding) 685444c Use "a".repeats() instead of manual implementation (Tobin C. Harding) 42de876 Allow let_and_return for feature guarded code (Tobin C. Harding) d64132c Allow missing_safety_doc (Tobin C. Harding) 2cb687f Use to_le_bytes instead of mem::transmute (Tobin C. Harding) c15b9d2 Remove unneeded explicit reference (Tobin C. Harding) 35d59e7 Remove explicit 'static lifetime (Tobin C. Harding) 1a582db Remove redundant import (Tobin C. Harding) Pull request description: The first 8 patches clear clippy warnings. Next we add a CI job to run clippy. Finally we add a `githooks` directory that includes running clippy, also adds a section to the README on how to use the githooks. This is identical to the text in the [open PR](rust-bitcoin/rust-bitcoin#1044) on `rust-bitcoin` that adds githooks _without_ yet adding clippy. **Note**: The new clippy CI job runs and is green :) ACKs for top commit: Kixunil: ACK 65186e7 apoelstra: ACK 65186e7 Tree-SHA512: f70a157896ce2a83af8cfc10f2fbacc8f68256ac96ef7dec4d190aa72324b568d2267418eb4fe99099aeda5486957c31070943d7c209973859b7b9290676ccd7
2 parents 1c4dd0d + 65186e7 commit a1ac3fb

File tree

10 files changed

+103
-39
lines changed

10 files changed

+103
-39
lines changed

.github/workflows/rust.yml

+16
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,19 @@ jobs:
7373
env:
7474
DO_WASM: true
7575
run: ./contrib/test.sh
76+
77+
Clippy:
78+
name: Clippy
79+
runs-on: ubuntu-latest
80+
steps:
81+
- uses: actions/checkout@v2
82+
- uses: actions-rs/toolchain@v1
83+
with:
84+
profile: minimal
85+
toolchain: stable
86+
override: true
87+
- run: rustup component add clippy
88+
- uses: actions-rs/cargo@v1
89+
with:
90+
command: clippy
91+
args: --features=rand-std,recovery,lowmemory,global-context --all-targets -- -D warnings

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ Contributions to this library are welcome. A few guidelines:
2222
* No crypto should be implemented in Rust, with the possible exception of hash functions. Cryptographic contributions should be directed upstream to libsecp256k1.
2323
* This library should always compile with any combination of features on **Rust 1.41.1**.
2424

25+
### Githooks
26+
27+
To assist devs in catching errors _before_ running CI we provide some githooks. If you do not
28+
already have locally configured githooks you can use the ones in this repository by running, in the
29+
root directory of the repository:
30+
```
31+
git config --local core.hooksPath githooks/
32+
```
33+
34+
Alternatively add symlinks in your `.git/hooks` directory to any of the githooks we provide.
35+
2536
## Fuzzing
2637

2738
If you want to fuzz this library, or any library which depends on it, you will

githooks/pre-commit

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/sh
2+
#
3+
# A hook script to verify what is about to be committed.
4+
# Called by "git commit" with no arguments. The hook should
5+
# exit with non-zero status after issuing an appropriate message if
6+
# it wants to stop the commit.
7+
8+
if git rev-parse --verify HEAD >/dev/null 2>&1
9+
then
10+
against=HEAD
11+
else
12+
# Initial commit: diff against an empty tree object
13+
against=$(git hash-object -t tree /dev/null)
14+
fi
15+
16+
# If you want to allow non-ASCII filenames set this variable to true.
17+
allownonascii=$(git config --bool hooks.allownonascii)
18+
19+
# Redirect output to stderr.
20+
exec 1>&2
21+
22+
# Cross platform projects tend to avoid non-ASCII filenames; prevent
23+
# them from being added to the repository. We exploit the fact that the
24+
# printable range starts at the space character and ends with tilde.
25+
if [ "$allownonascii" != "true" ] &&
26+
# Note that the use of brackets around a tr range is ok here, (it's
27+
# even required, for portability to Solaris 10's /usr/bin/tr), since
28+
# the square bracket bytes happen to fall in the designated range.
29+
test $(git diff --cached --name-only --diff-filter=A -z $against |
30+
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
31+
then
32+
cat <<\EOF
33+
Error: Attempt to add a non-ASCII file name.
34+
35+
This can cause problems if you want to work with people on other platforms.
36+
37+
To be portable it is advisable to rename the file.
38+
39+
If you know what you are doing you can disable this check using:
40+
41+
git config hooks.allownonascii true
42+
EOF
43+
exit 1
44+
fi
45+
46+
# If there are whitespace errors, print the offending file names and fail.
47+
git diff-index --check --cached $against -- || exit 1
48+
49+
# Check that code lints cleanly.
50+
cargo clippy --features=rand-std,recovery,lowmemory,global-context --all-targets -- -D warnings || exit 1

secp256k1-sys/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
// Coding conventions
2020
#![deny(non_upper_case_globals, non_camel_case_types, non_snake_case, unused_mut)]
2121

22+
#![allow(clippy::missing_safety_doc)]
23+
2224
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
2325
#![cfg_attr(docsrs, feature(doc_cfg))]
2426

src/context.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@ mod alloc_only {
114114
use crate::ffi::{self, types::{c_uint, c_void}};
115115
use crate::{Secp256k1, Signing, Verification, Context, AlignedType};
116116

117-
#[cfg(feature = "rand-std")]
118-
use rand;
119-
120117
impl private::Sealed for SignOnly {}
121118
impl private::Sealed for All {}
122119
impl private::Sealed for VerifyOnly {}
@@ -191,7 +188,7 @@ mod alloc_only {
191188
/// ctx.seeded_randomize(&seed);
192189
/// # }
193190
/// ```
194-
#[allow(unused_mut)] // Unused when `rand-std` is not enabled.
191+
#[cfg_attr(not(feature = "rand-std"), allow(clippy::let_and_return, unused_mut))]
195192
pub fn gen_new() -> Secp256k1<C> {
196193
#[cfg(target_arch = "wasm32")]
197194
ffi::types::sanity_checks_for_wasm();

src/ecdh.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl ::serde::Serialize for SharedSecret {
176176
let mut buf = [0u8; SHARED_SECRET_SIZE * 2];
177177
s.serialize_str(crate::to_hex(&self.0, &mut buf).expect("fixed-size hex serialization"))
178178
} else {
179-
s.serialize_bytes(&self.as_ref()[..])
179+
s.serialize_bytes(self.as_ref())
180180
}
181181
}
182182
}
@@ -272,9 +272,7 @@ mod tests {
272272
0xff, 0xff, 0, 0, 0xff, 0xff, 0, 0,
273273
99, 99, 99, 99, 99, 99, 99, 99
274274
];
275-
static STR: &'static str = "\
276-
01010101010101010001020304050607ffff0000ffff00006363636363636363\
277-
";
275+
static STR: &str = "01010101010101010001020304050607ffff0000ffff00006363636363636363";
278276

279277
let secret = SharedSecret::from_slice(&BYTES).unwrap();
280278

src/ecdsa/mod.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Structs and functionality related to the ECDSA signature algorithm.
22
3-
use core::{fmt, str, ops, ptr, mem};
3+
use core::{fmt, str, ops, ptr};
44

55
use crate::{Signing, Verification, Message, PublicKey, Secp256k1, SecretKey, from_hex, Error, ffi};
66
use crate::ffi::CPtr;
@@ -398,14 +398,8 @@ impl<C: Signing> Secp256k1<C> {
398398
}
399399

400400
counter += 1;
401-
// From 1.32 can use `to_le_bytes` instead
402-
let le_counter = counter.to_le();
403-
let le_counter_bytes : [u8; 4] = mem::transmute(le_counter);
404-
for (i, b) in le_counter_bytes.iter().enumerate() {
405-
extra_entropy[i] = *b;
406-
}
407-
408-
entropy_p = extra_entropy.as_ptr() as *const ffi::types::c_void;
401+
extra_entropy[..4].copy_from_slice(&counter.to_le_bytes());
402+
entropy_p = extra_entropy.as_ptr().cast::<ffi::types::c_void>();
409403

410404
// When fuzzing, these checks will usually spinloop forever, so just short-circuit them.
411405
#[cfg(fuzzing)]

src/key.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ impl PublicKey {
431431
#[cfg(feature = "global-context")]
432432
#[cfg_attr(docsrs, doc(cfg(feature = "global-context")))]
433433
pub fn from_secret_key_global(sk: &SecretKey) -> PublicKey {
434-
PublicKey::from_secret_key(&SECP256K1, sk)
434+
PublicKey::from_secret_key(SECP256K1, sk)
435435
}
436436

437437
/// Creates a public key directly from a slice.
@@ -1621,7 +1621,7 @@ pub mod serde_keypair {
16211621
let secret_key = SecretKey::deserialize(deserializer)?;
16221622

16231623
Ok(KeyPair::from_secret_key(
1624-
&crate::SECP256K1,
1624+
crate::SECP256K1,
16251625
&secret_key,
16261626
))
16271627
}
@@ -1875,7 +1875,7 @@ mod test {
18751875
assert!(PublicKey::from_str("0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd1").is_err());
18761876
assert!(PublicKey::from_str("xx0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd1").is_err());
18771877

1878-
let long_str: String = core::iter::repeat('a').take(1024 * 1024).collect();
1878+
let long_str = "a".repeat(1024 * 1024);
18791879
assert!(SecretKey::from_str(&long_str).is_err());
18801880
assert!(PublicKey::from_str(&long_str).is_err());
18811881
}
@@ -2077,6 +2077,7 @@ mod test {
20772077

20782078
#[cfg(not(fuzzing))]
20792079
#[test]
2080+
#[allow(clippy::nonminimal_bool)]
20802081
fn pubkey_equal() {
20812082
let pk1 = PublicKey::from_slice(
20822083
&hex!("0241cc121c419921942add6db6482fb36243faf83317c866d2a28d8c6d7089f7ba"),
@@ -2108,9 +2109,8 @@ mod test {
21082109
0xff, 0xff, 0, 0, 0xff, 0xff, 0, 0,
21092110
99, 99, 99, 99, 99, 99, 99, 99
21102111
];
2111-
static SK_STR: &'static str = "\
2112-
01010101010101010001020304050607ffff0000ffff00006363636363636363\
2113-
";
2112+
static SK_STR: &str = "01010101010101010001020304050607ffff0000ffff00006363636363636363";
2113+
21142114
#[cfg(fuzzing)]
21152115
static PK_BYTES: [u8; 33] = [
21162116
0x02,
@@ -2119,9 +2119,7 @@ mod test {
21192119
0x06, 0x83, 0x7f, 0x30, 0xaa, 0x0c, 0xd0, 0x54,
21202120
0x4a, 0xc8, 0x87, 0xfe, 0x91, 0xdd, 0xd1, 0x66,
21212121
];
2122-
static PK_STR: &'static str = "\
2123-
0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166\
2124-
";
2122+
static PK_STR: &str = "0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166";
21252123

21262124
#[cfg(not(fuzzing))]
21272125
let s = Secp256k1::new();
@@ -2236,9 +2234,7 @@ mod test {
22362234
0xff, 0xff, 0, 0, 0xff, 0xff, 0, 0,
22372235
99, 99, 99, 99, 99, 99, 99, 99
22382236
];
2239-
static SK_STR: &'static str = "\
2240-
01010101010101010001020304050607ffff0000ffff00006363636363636363\
2241-
";
2237+
static SK_STR: &str = "01010101010101010001020304050607ffff0000ffff00006363636363636363";
22422238

22432239
let sk = KeyPairWrapper(KeyPair::from_seckey_slice(&crate::SECP256K1, &SK_BYTES).unwrap());
22442240
assert_tokens(&sk.compact(), &[

src/lib.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@
152152
#![deny(non_upper_case_globals, non_camel_case_types, non_snake_case)]
153153
#![warn(missing_docs, missing_copy_implementations, missing_debug_implementations)]
154154

155+
#![allow(clippy::missing_safety_doc)]
156+
155157
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
156158
#![cfg_attr(all(test, feature = "unstable"), feature(test))]
157159
#![cfg_attr(docsrs, feature(doc_cfg))]
@@ -729,10 +731,10 @@ mod tests {
729731

730732
assert_eq!(
731733
ecdsa::Signature::from_der(&byte_str).expect("byte str decode"),
732-
ecdsa::Signature::from_str(&hex_str).expect("byte str decode")
734+
ecdsa::Signature::from_str(hex_str).expect("byte str decode")
733735
);
734736

735-
let sig = ecdsa::Signature::from_str(&hex_str).expect("byte str decode");
737+
let sig = ecdsa::Signature::from_str(hex_str).expect("byte str decode");
736738
assert_eq!(&sig.to_string(), hex_str);
737739
assert_eq!(&format!("{:?}", sig), hex_str);
738740

@@ -759,7 +761,7 @@ mod tests {
759761

760762
// 71 byte signature
761763
let hex_str = "30450221009d0bad576719d32ae76bedb34c774866673cbde3f4e12951555c9408e6ce774b02202876e7102f204f6bfee26c967c3926ce702cf97d4b010062e193f763190f6776";
762-
let sig = ecdsa::Signature::from_str(&hex_str).expect("byte str decode");
764+
let sig = ecdsa::Signature::from_str(hex_str).expect("byte str decode");
763765
assert_eq!(&format!("{}", sig), hex_str);
764766
}
765767

@@ -1001,7 +1003,7 @@ mod tests {
10011003
226, 108, 150, 124, 57, 38, 206, 112, 44, 249, 125, 75, 1, 0, 98, 225,
10021004
147, 247, 99, 25, 15, 103, 118
10031005
];
1004-
static SIG_STR: &'static str = "\
1006+
static SIG_STR: &str = "\
10051007
30450221009d0bad576719d32ae76bedb34c774866673cbde3f4e12951555c9408e6ce77\
10061008
4b02202876e7102f204f6bfee26c967c3926ce702cf97d4b010062e193f763190f6776\
10071009
";
@@ -1026,7 +1028,7 @@ mod tests {
10261028
let msg = Message::from_slice(&msg_data).unwrap();
10271029

10281030
// Check usage as explicit parameter
1029-
let pk = PublicKey::from_secret_key(&SECP256K1, &sk);
1031+
let pk = PublicKey::from_secret_key(SECP256K1, &sk);
10301032

10311033
// Check usage as self
10321034
let sig = SECP256K1.sign_ecdsa(&msg, &sk);

src/schnorr.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ mod tests {
505505
)
506506
.is_err());
507507

508-
let long_str: String = core::iter::repeat('a').take(1024 * 1024).collect();
508+
let long_str: String = "a".repeat(1024 * 1024);
509509
assert!(XOnlyPublicKey::from_str(&long_str).is_err());
510510
}
511511

@@ -548,17 +548,15 @@ mod tests {
548548
0x9e, 0x4d, 0x4e, 0xf5, 0x67, 0x8a, 0xd0, 0xd9, 0xd5, 0x32, 0xc0, 0xdf, 0xa9, 0x07,
549549
0xb5, 0x68, 0x72, 0x2d, 0x0b, 0x01, 0x19, 0xba,
550550
];
551-
static SIG_STR: &'static str = "\
551+
static SIG_STR: &str = "\
552552
14d0bf1a8953506fb460f58be141af767fd112535fb3922ef217308e2c26706f1eeb432b3dba9a01082f9e4d4ef5678ad0d9d532c0dfa907b568722d0b0119ba\
553553
";
554554

555555
static PK_BYTES: [u8; 32] = [
556556
24, 132, 87, 129, 246, 49, 196, 143, 28, 151, 9, 226, 48, 146, 6, 125, 6, 131, 127,
557557
48, 170, 12, 208, 84, 74, 200, 135, 254, 145, 221, 209, 102
558558
];
559-
static PK_STR: &'static str = "\
560-
18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166\
561-
";
559+
static PK_STR: &str = "18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166";
562560
let pk = XOnlyPublicKey::from_slice(&PK_BYTES).unwrap();
563561

564562
assert_tokens(&sig.compact(), &[Token::BorrowedBytes(&SIG_BYTES[..])]);

0 commit comments

Comments
 (0)