Skip to content

Commit ba47a25

Browse files
committed
Merge #526: Run the formatter in CI
75f3886 Add cargo fmt to pre-commit githook (Tobin C. Harding) 0516dde Add formatting check to CI (Tobin C. Harding) c7807df Run the formatter (Tobin C. Harding) Pull request description: We recently introduced `rustfmt` to the codebase but I forgot to turn it on in CI. - Patch 1: Preparatory formatting fixes, introduced since we merged the [formatting PR](#499) - Patch 2: Enable formatting in CI - Patch 3: Add formatting to the pre-commit hook ACKs for top commit: apoelstra: ACK 75f3886 Tree-SHA512: 5ac4ab4015a9728ef890e0c4fe90afcb5e45ab7665da5a8ee289dc877c1ea5c6236e54b68b7122841597864b04606c8bfae7dec86c4b6be74d32437299057b5f
2 parents 72bfdd3 + 75f3886 commit ba47a25

File tree

9 files changed

+33
-24
lines changed

9 files changed

+33
-24
lines changed

.github/workflows/rust.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ jobs:
1818
toolchain: nightly
1919
override: true
2020
components: rust-src
21+
- name: Check formatting
22+
env:
23+
DO_FMT: true
24+
run: ./contrib/test.sh
2125
- name: Running address sanitizer
2226
env:
2327
DO_ASAN: true

contrib/test.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ if [ "$DO_ASAN" = true ]; then
8888
cargo run --release --features=alloc --manifest-path=./no_std_test/Cargo.toml | grep -q "Verified alloc Successfully"
8989
fi
9090

91+
# Run formatter if told to.
92+
if [ "$DO_FMT" = true ]; then
93+
if [ "$NIGHTLY" = false ]; then
94+
echo "DO_FMT requires a nightly toolchain (consider using RUSTUP_TOOLCHAIN)"
95+
exit 1
96+
fi
97+
rustup component add rustfmt
98+
cargo fmt --check || exit 1
99+
fi
91100

92101
# Bench if told to, only works with non-stable toolchain (nightly, beta).
93102
if [ "$DO_BENCH" = true ]

githooks/pre-commit

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@ git diff-index --check --cached $against -- || exit 1
4848

4949
# Check that code lints cleanly.
5050
cargo clippy --features=rand-std,recovery,lowmemory,global-context --all-targets -- -D warnings || exit 1
51+
52+
# Check that there are no formatting issues.
53+
cargo +nightly fmt --check || exit 1

src/ecdh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ mod tests {
288288
}
289289

290290
#[cfg(bench)]
291-
#[cfg(feature = "rand-std")] // Currently only a single bench that requires "rand-std".
291+
#[cfg(feature = "rand-std")] // Currently only a single bench that requires "rand-std".
292292
mod benches {
293293
use test::{black_box, Bencher};
294294

src/ecdsa/recovery.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ mod tests {
432432
}
433433

434434
#[cfg(bench)]
435-
#[cfg(feature = "rand-std")] // Currently only a single bench that requires "rand-std".
435+
#[cfg(feature = "rand-std")] // Currently only a single bench that requires "rand-std".
436436
mod benches {
437437
use test::{black_box, Bencher};
438438

src/key.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ impl<'a> From<&'a KeyPair> for PublicKey {
951951
impl str::FromStr for KeyPair {
952952
type Err = Error;
953953

954-
#[allow(unused_variables, unreachable_code)] // When built with no default features.
954+
#[allow(unused_variables, unreachable_code)] // When built with no default features.
955955
fn from_str(s: &str) -> Result<Self, Self::Err> {
956956
#[cfg(feature = "global-context")]
957957
let ctx = SECP256K1;
@@ -1515,7 +1515,7 @@ mod test {
15151515
use core::str::FromStr;
15161516

15171517
#[cfg(feature = "rand")]
1518-
use rand::{self, RngCore, rngs::mock::StepRng};
1518+
use rand::{self, rngs::mock::StepRng, RngCore};
15191519
use serde_test::{Configure, Token};
15201520
#[cfg(target_arch = "wasm32")]
15211521
use wasm_bindgen_test::wasm_bindgen_test as test;

src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -530,19 +530,16 @@ pub(crate) fn random_32_bytes<R: rand::Rng + ?Sized>(rng: &mut R) -> [u8; 32] {
530530

531531
#[cfg(test)]
532532
mod tests {
533-
#[allow(unused_imports)] // When building with no default features.
534-
use super::*;
535-
536533
use std::str::FromStr;
537534

538535
#[cfg(target_arch = "wasm32")]
539536
use wasm_bindgen_test::wasm_bindgen_test as test;
540537

538+
#[allow(unused_imports)] // When building with no default features.
539+
use super::*;
540+
use crate::{constants, ecdsa, from_hex, Error, Message};
541541
#[cfg(feature = "alloc")]
542542
use crate::{ffi, PublicKey, Secp256k1, SecretKey};
543-
use crate::{
544-
constants, ecdsa, from_hex, Error, Message,
545-
};
546543

547544
macro_rules! hex {
548545
($hex:expr) => {{
@@ -889,9 +886,10 @@ mod tests {
889886
#[test]
890887
#[cfg(feature = "rand-std")]
891888
fn test_hex() {
892-
use super::to_hex;
893889
use rand::RngCore;
894890

891+
use super::to_hex;
892+
895893
let mut rng = rand::thread_rng();
896894
const AMOUNT: usize = 1024;
897895
for i in 0..AMOUNT {

src/macros.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,17 @@
1717
#[macro_export]
1818
macro_rules! impl_array_newtype {
1919
($thing:ident, $ty:ty, $len:expr) => {
20-
2120
// We cannot derive these traits because Rust 1.41.1 requires `std::array::LengthAtMost32`.
2221

2322
impl PartialEq for $thing {
2423
#[inline]
25-
fn eq(&self, other: &$thing) -> bool {
26-
&self[..] == &other[..]
27-
}
24+
fn eq(&self, other: &$thing) -> bool { &self[..] == &other[..] }
2825
}
2926

3027
impl Eq for $thing {}
3128

3229
impl core::hash::Hash for $thing {
33-
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
34-
(&self[..]).hash(state)
35-
}
30+
fn hash<H: core::hash::Hasher>(&self, state: &mut H) { (&self[..]).hash(state) }
3631
}
3732

3833
impl PartialOrd for $thing {
@@ -44,9 +39,7 @@ macro_rules! impl_array_newtype {
4439

4540
impl Ord for $thing {
4641
#[inline]
47-
fn cmp(&self, other: &$thing) -> core::cmp::Ordering {
48-
self[..].cmp(&other[..])
49-
}
42+
fn cmp(&self, other: &$thing) -> core::cmp::Ordering { self[..].cmp(&other[..]) }
5043
}
5144

5245
impl AsRef<[$ty; $len]> for $thing {
@@ -81,7 +74,7 @@ macro_rules! impl_array_newtype {
8174
dat.as_mut_ptr()
8275
}
8376
}
84-
}
77+
};
8578
}
8679

8780
macro_rules! impl_pretty_debug {
@@ -139,5 +132,5 @@ macro_rules! impl_fast_comparisons {
139132
self.0.eq_fast_unstable(&other.0)
140133
}
141134
}
142-
}
135+
};
143136
}

src/schnorr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use crate::ffi::{self, CPtr};
1111
use crate::key::{KeyPair, XOnlyPublicKey};
1212
#[cfg(feature = "global-context")]
1313
use crate::SECP256K1;
14-
use crate::{constants, from_hex, impl_array_newtype, Error, Message, Secp256k1, Signing, Verification};
14+
use crate::{
15+
constants, from_hex, impl_array_newtype, Error, Message, Secp256k1, Signing, Verification,
16+
};
1517

1618
/// Represents a Schnorr signature.
1719
#[derive(Copy, Clone)]

0 commit comments

Comments
 (0)