From 3173ef9dbb2634df6f952262e13a8d7ef5ff5f3d Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 20 Jul 2022 12:57:30 +1000 Subject: [PATCH 1/5] Remove unnecessary explicit reference Clippy emits various warnings of type: warning: this expression creates a reference which is immediately dereferenced by the compiler As suggested, remove the unnecessary explicit reference. --- src/util/amount.rs | 4 ++-- src/util/key.rs | 2 +- src/util/misc.rs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/util/amount.rs b/src/util/amount.rs index d23b9c23..f68614d7 100644 --- a/src/util/amount.rs +++ b/src/util/amount.rs @@ -2033,10 +2033,10 @@ mod tests { let json = "{\"amt\": 21000000.00000001, \ \"samt\": -21000000.00000001}"; - let t: T = serde_json::from_str(&json).unwrap(); + let t: T = serde_json::from_str(json).unwrap(); assert_eq!(t, orig); - let value: serde_json::Value = serde_json::from_str(&json).unwrap(); + let value: serde_json::Value = serde_json::from_str(json).unwrap(); assert_eq!(t, serde_json::from_value(value).unwrap()); // errors diff --git a/src/util/key.rs b/src/util/key.rs index ef2fc5d9..fdaa0e63 100644 --- a/src/util/key.rs +++ b/src/util/key.rs @@ -638,7 +638,7 @@ mod tests { ]; let s = Secp256k1::new(); - let sk = PrivateKey::from_str(&KEY_WIF).unwrap(); + let sk = PrivateKey::from_str(KEY_WIF).unwrap(); let pk = PublicKey::from_private_key(&s, &sk); let pk_u = PublicKey { inner: pk.inner, diff --git a/src/util/misc.rs b/src/util/misc.rs index 447eac12..a9600172 100644 --- a/src/util/misc.rs +++ b/src/util/misc.rs @@ -317,7 +317,7 @@ mod tests { let secp = secp256k1::Secp256k1::new(); let message = "rust-bitcoin MessageSignature test"; - let msg_hash = super::signed_msg_hash(&message); + let msg_hash = super::signed_msg_hash(message); let msg = secp256k1::Message::from(msg_hash); @@ -357,7 +357,7 @@ mod tests { let secp = secp256k1::Secp256k1::new(); let message = "a different message from what was signed"; - let msg_hash = super::signed_msg_hash(&message); + let msg_hash = super::signed_msg_hash(message); // Signature of msg = "rust-bitcoin MessageSignature test" // Signed with pk "UuOGDsfLPr4HIMKQX0ipjJeRaj1geCq3yPUF2COP5ME=" From 196492554dc1939f7553e3db9d7b1dd75a76dbde Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 20 Jul 2022 12:58:51 +1000 Subject: [PATCH 2/5] Use assert instead of assert_eq Clippy emits: warning: used `assert_eq!` with a literal bool As suggested, use `assert` instead of `assert_eq`. --- src/util/misc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/misc.rs b/src/util/misc.rs index a9600172..4d106707 100644 --- a/src/util/misc.rs +++ b/src/util/misc.rs @@ -331,7 +331,7 @@ mod tests { assert_eq!(signature.to_base64(), signature.to_string()); let signature2 = super::MessageSignature::from_str(&signature.to_string()).unwrap(); let pubkey = signature2.recover_pubkey(&secp, msg_hash).unwrap(); - assert_eq!(pubkey.compressed, true); + assert!(pubkey.compressed); assert_eq!(pubkey.inner, secp256k1::PublicKey::from_secret_key(&secp, &privkey)); let p2pkh = Address::p2pkh(&pubkey, Network::Bitcoin); From d1a05401f4f104776225d455b9a0c206d9a4facc Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 20 Jul 2022 13:00:10 +1000 Subject: [PATCH 3/5] Remove redundant calls to clone Clippy emits various warnings of form: warning: redundant clone As suggested, remove the redundant calls to `clone`. --- src/util/psbt/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/util/psbt/mod.rs b/src/util/psbt/mod.rs index 663a6230..aa03f8ee 100644 --- a/src/util/psbt/mod.rs +++ b/src/util/psbt/mod.rs @@ -543,7 +543,7 @@ mod tests { let xpub: ExtendedPubKey = "xpub661MyMwAqRbcGoRVtwfvzZsq2VBJR1LAHfQstHUoxqDorV89vRoMxUZ27kLrraAj6MPi\ QfrDb27gigC1VS1dBXi5jGpxmMeBXEkKkcXUTg4".parse().unwrap(); - vec![(xpub, key_source.clone())].into_iter().collect() + vec![(xpub, key_source)].into_iter().collect() }, unsigned_tx: { let mut unsigned = tx.clone(); @@ -579,8 +579,8 @@ mod tests { }], outputs: vec![Output { bip32_derivation: keypaths, - proprietary: proprietary.clone(), - unknown: unknown.clone(), + proprietary: proprietary, + unknown: unknown, ..Default::default() }], }; From aa8109a791c74ed32d6325cd5320a6c5fcaea41e Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 20 Jul 2022 13:01:34 +1000 Subject: [PATCH 4/5] Use struct field short form Clippy emits two warnings of form: warning: redundant field names in struct initialization Remove the redundant field names and use struct short initialization form. --- src/util/psbt/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/psbt/mod.rs b/src/util/psbt/mod.rs index aa03f8ee..a2049145 100644 --- a/src/util/psbt/mod.rs +++ b/src/util/psbt/mod.rs @@ -579,8 +579,8 @@ mod tests { }], outputs: vec![Output { bip32_derivation: keypaths, - proprietary: proprietary, - unknown: unknown, + proprietary, + unknown, ..Default::default() }], }; From 74f3a5aeda5a5eeed261e2b55858509e192e7eed Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 30 Jun 2022 10:40:51 +1000 Subject: [PATCH 5/5] Run clippy from the test script Currently we run clippy in CI using a github action. The invocation has a couple of shortcomings 1. it does not lint the tests (this requires `--all-targets`) 2. it does not lint the examples I could not find a way to lint the examples without explicitly linting each example by name. Move the clippy control to `test.sh` and add an env var `DO_LINT` to control it. Remove the explicit CI job and run the linter during the `Test` job using the stable toolchain. --- .github/workflows/rust.yml | 17 +---------------- contrib/test.sh | 8 ++++++++ 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 41733534..540478c1 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -13,6 +13,7 @@ jobs: - rust: stable env: DO_COV: true + DO_LINT: true AS_DEPENDENCY: true DO_NO_STD: true - rust: beta @@ -106,19 +107,3 @@ jobs: RUSTFLAGS: "-C link-arg=-Tlink.x" CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER: "qemu-system-arm -cpu cortex-m3 -machine mps2-an385 -nographic -semihosting-config enable=on,target=native -kernel" run: cd embedded && cargo run --target thumbv7m-none-eabi - - Clippy: - name: Clippy - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - override: true - - run: rustup component add clippy - - uses: actions-rs/cargo@v1 - with: - command: clippy - args: --all-features -- -D warnings diff --git a/contrib/test.sh b/contrib/test.sh index 0bb0771b..04031f7a 100755 --- a/contrib/test.sh +++ b/contrib/test.sh @@ -31,6 +31,14 @@ if [ "$duplicate_dependencies" -ne 0 ]; then exit 1 fi +if [ "$DO_LINT" = true ] +then + cargo clippy --all-features --all-targets -- -D warnings + cargo clippy --example bip32 -- -D warnings + cargo clippy --example handshake -- -D warnings + cargo clippy --example ecdsa-psbt --features=bitcoinconsensus -- -D warnings +fi + echo "********* Testing std *************" # Test without any features other than std first cargo test --verbose --no-default-features --features="std"