Skip to content

Commit 2ec90e9

Browse files
committed
Merge rust-bitcoin/rust-bitcoin#1061: Enabe clippy on CI
e2e4650 Add clippy to pre-commit githook (Tobin C. Harding) 820adc0 Add githooks directory (Tobin C. Harding) 668b37a Enable clippy on CI (Tobin C. Harding) a2a54b3 Remove unnecessary ? operator (Tobin C. Harding) 67ed8f6 Remove unneeded clone (Tobin C. Harding) eccd401 Remove unneeded reference (Tobin C. Harding) fd4239f Use custom digit grouping (Tobin C. Harding) acd551e Remove unnecessary 'static lifetime (Tobin C. Harding) 3102a48 Allow clippy::collapsible_else_if (Tobin C. Harding) 63f4ff6 Remove redundant field names (Tobin C. Harding) Pull request description: We are almost ready to enable clippy on CI! First clear a few remaining warnings from feature gated code. Then add a CI job to run clippy using `--all-features` (note no `--all-targets`). Next add githooks directory (this is the patch from https://github.com/rust-bitcoin/rust-bitcoin/pull/1044/commits). Finally add `cargo clippy` to the pre-commit hook. EDIT: I just realized the running of clippy could have been done in `test.sh` instead of as a github action. Does that matter? ACKs for top commit: apoelstra: ACK e2e4650 Kixunil: ACK e2e4650 Tree-SHA512: c78cb78973d3935e5be7908eec7d6ffaa7989724b3e29551b9fa2cb35df1f39574e31e5cc93fdfb32b35039ac9dac5c080ae4287a1e6979dd076bab56e6eda1b
2 parents 83c3ddb + 5e38351 commit 2ec90e9

File tree

9 files changed

+96
-17
lines changed

9 files changed

+96
-17
lines changed

.github/workflows/rust.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,18 @@ jobs:
107107
CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER: "qemu-system-arm -cpu cortex-m3 -machine mps2-an385 -nographic -semihosting-config enable=on,target=native -kernel"
108108
run: cd embedded && cargo run --target thumbv7m-none-eabi
109109

110+
Clippy:
111+
name: Clippy
112+
runs-on: ubuntu-latest
113+
steps:
114+
- uses: actions/checkout@v2
115+
- uses: actions-rs/toolchain@v1
116+
with:
117+
profile: minimal
118+
toolchain: stable
119+
override: true
120+
- run: rustup component add clippy
121+
- uses: actions-rs/cargo@v1
122+
with:
123+
command: clippy
124+
args: --all-features -- -D warnings

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ In order to speed up the review process the CI pipeline can be run locally using
125125
skipped when using `act` due to caching being unsupported at this time. We do
126126
not *actively* support `act` but will merge PRs fixing `act` issues.
127127

128+
### Githooks
129+
130+
To assist devs in catching errors _before_ running CI we provide some githooks. If you do not
131+
already have locally configured githooks you can use the ones in this repository by running, in the
132+
root directory of the repository:
133+
```
134+
git config --local core.hooksPath githooks/
135+
```
136+
137+
Alternatively add symlinks in your `.git/hooks` directory to any of the githooks we provide.
138+
128139
## Policy on Altcoins/Altchains
129140

130141
Patches which add support for non-Bitcoin cryptocurrencies by adding constants

githooks/pre-commit

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

src/blockdata/script.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ impl serde::Serialize for Script {
10851085
if serializer.is_human_readable() {
10861086
serializer.serialize_str(&format!("{:x}", self))
10871087
} else {
1088-
serializer.serialize_bytes(&self.as_bytes())
1088+
serializer.serialize_bytes(self.as_bytes())
10891089
}
10901090
}
10911091
}

src/blockdata/transaction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ impl Transaction {
631631
if let Some(output) = spent(&input.previous_output) {
632632
output.script_pubkey.verify_with_flags(idx, crate::Amount::from_sat(output.value), tx.as_slice(), flags)?;
633633
} else {
634-
return Err(script::Error::UnknownSpentOutput(input.previous_output.clone()));
634+
return Err(script::Error::UnknownSpentOutput(input.previous_output));
635635
}
636636
}
637637
Ok(())
@@ -1662,7 +1662,7 @@ mod benches {
16621662
use crate::hashes::hex::FromHex;
16631663
use test::{black_box, Bencher};
16641664

1665-
const SOME_TX: &'static str = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000";
1665+
const SOME_TX: &str = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000";
16661666

16671667
#[bench]
16681668
pub fn bench_transaction_size(bh: &mut Bencher) {

src/internal_macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ macro_rules! serde_struct_human_string_impl {
276276
)*
277277

278278
let ret = $name {
279-
$($fe: $fe),*
279+
$($fe),*
280280
};
281281

282282
Ok(ret)
@@ -312,7 +312,7 @@ macro_rules! serde_struct_human_string_impl {
312312
)*
313313

314314
let ret = $name {
315-
$($fe: $fe),*
315+
$($fe),*
316316
};
317317

318318
Ok(ret)

src/util/amount.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,7 @@ pub mod serde {
13101310
}
13111311
fn des_btc<'d, D: Deserializer<'d>>(d: D) -> Result<Self, D::Error> {
13121312
use serde::de::Error;
1313-
Ok(Amount::from_btc(f64::deserialize(d)?).map_err(D::Error::custom)?)
1313+
Amount::from_btc(f64::deserialize(d)?).map_err(D::Error::custom)
13141314
}
13151315
}
13161316

@@ -1338,7 +1338,7 @@ pub mod serde {
13381338
}
13391339
fn des_btc<'d, D: Deserializer<'d>>(d: D) -> Result<Self, D::Error> {
13401340
use serde::de::Error;
1341-
Ok(SignedAmount::from_btc(f64::deserialize(d)?).map_err(D::Error::custom)?)
1341+
SignedAmount::from_btc(f64::deserialize(d)?).map_err(D::Error::custom)
13421342
}
13431343
}
13441344

@@ -1622,9 +1622,9 @@ mod tests {
16221622
assert_eq!(p("1.1", btc), Ok(Amount::from_sat(1_100_000_00)));
16231623
assert_eq!(p("100", sat), Ok(Amount::from_sat(100)));
16241624
assert_eq!(p("55", sat), Ok(Amount::from_sat(55)));
1625-
assert_eq!(p("5500000000000000000", sat), Ok(Amount::from_sat(5_500_000_000_000_000_000)));
1625+
assert_eq!(p("5500000000000000000", sat), Ok(Amount::from_sat(55_000_000_000_000_000_00)));
16261626
// Should this even pass?
1627-
assert_eq!(p("5500000000000000000.", sat), Ok(Amount::from_sat(5_500_000_000_000_000_000)));
1627+
assert_eq!(p("5500000000000000000.", sat), Ok(Amount::from_sat(55_000_000_000_000_000_00)));
16281628
assert_eq!(
16291629
p("12345678901.12345678", btc),
16301630
Ok(Amount::from_sat(12_345_678_901__123_456_78))
@@ -2006,6 +2006,7 @@ mod tests {
20062006

20072007
#[cfg(feature = "serde")]
20082008
#[test]
2009+
#[allow(clippy::inconsistent_digit_grouping)] // Group to show 100,000,000 sats per bitcoin.
20092010
fn serde_as_btc() {
20102011
use serde_json;
20112012

@@ -2041,6 +2042,7 @@ mod tests {
20412042

20422043
#[cfg(feature = "serde")]
20432044
#[test]
2045+
#[allow(clippy::inconsistent_digit_grouping)] // Group to show 100,000,000 sats per bitcoin.
20442046
fn serde_as_btc_opt() {
20452047
use serde_json;
20462048

@@ -2054,8 +2056,8 @@ mod tests {
20542056
}
20552057

20562058
let with = T {
2057-
amt: Some(Amount::from_sat(2__500_000_00)),
2058-
samt: Some(SignedAmount::from_sat(-2__500_000_00)),
2059+
amt: Some(Amount::from_sat(2_500_000_00)),
2060+
samt: Some(SignedAmount::from_sat(-2_500_000_00)),
20592061
};
20602062
let without = T {
20612063
amt: None,
@@ -2085,6 +2087,7 @@ mod tests {
20852087

20862088
#[cfg(feature = "serde")]
20872089
#[test]
2090+
#[allow(clippy::inconsistent_digit_grouping)] // Group to show 100,000,000 sats per bitcoin.
20882091
fn serde_as_sat_opt() {
20892092
use serde_json;
20902093

@@ -2098,8 +2101,8 @@ mod tests {
20982101
}
20992102

21002103
let with = T {
2101-
amt: Some(Amount::from_sat(2__500_000_00)),
2102-
samt: Some(SignedAmount::from_sat(-2__500_000_00)),
2104+
amt: Some(Amount::from_sat(2_500_000_00)),
2105+
samt: Some(SignedAmount::from_sat(-2_500_000_00)),
21032106
};
21042107
let without = T {
21052108
amt: None,

src/util/key.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ impl<'de> ::serde::Deserialize<'de> for PrivateKey {
411411

412412
#[cfg(feature = "serde")]
413413
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
414+
#[allow(clippy::collapsible_else_if)] // Aids readability.
414415
impl ::serde::Serialize for PublicKey {
415416
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
416417
if s.is_human_readable() {
@@ -549,9 +550,9 @@ mod tests {
549550
fn test_key_serde() {
550551
use serde_test::{Configure, Token, assert_tokens};
551552

552-
static KEY_WIF: &'static str = "cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy";
553-
static PK_STR: &'static str = "039b6347398505f5ec93826dc61c19f47c66c0283ee9be980e29ce325a0f4679ef";
554-
static PK_STR_U: &'static str = "\
553+
static KEY_WIF: &str = "cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy";
554+
static PK_STR: &str = "039b6347398505f5ec93826dc61c19f47c66c0283ee9be980e29ce325a0f4679ef";
555+
static PK_STR_U: &str = "\
555556
04\
556557
9b6347398505f5ec93826dc61c19f47c66c0283ee9be980e29ce325a0f4679ef\
557558
87288ed73ce47fc4f5c79d19ebfa57da7cff3aff6e819e4ee971d86b5e61875d\

src/util/psbt/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ mod display_from_str {
269269

270270
fn from_str(s: &str) -> Result<Self, Self::Err> {
271271
let data = ::base64::decode(s).map_err(PsbtParseError::Base64Encoding)?;
272-
Ok(encode::deserialize(&data).map_err(PsbtParseError::PsbtEncoding)?)
272+
encode::deserialize(&data).map_err(PsbtParseError::PsbtEncoding)
273273
}
274274
}
275275
}

0 commit comments

Comments
 (0)