From adc9f8f5451067dff73afcb52f9093142f4a2744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ceyhun=20=C5=9Een?= Date: Fri, 6 Sep 2024 11:08:17 +0300 Subject: [PATCH 1/2] workflow: Add --all-targets to clippy. --- .github/workflows/code_check.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/code_check.yml b/.github/workflows/code_check.yml index bde1454..1ebae3b 100644 --- a/.github/workflows/code_check.yml +++ b/.github/workflows/code_check.yml @@ -22,4 +22,4 @@ jobs: steps: - uses: actions/checkout@v4 - name: Run Cargo clippy - run: cargo clippy --no-deps -- -Dwarnings + run: cargo clippy --no-deps --all-targets -- -Dwarnings From bb044d9f7a1a88235422b3983d1ee022c456b6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ceyhun=20=C5=9Een?= Date: Fri, 6 Sep 2024 11:15:19 +0300 Subject: [PATCH 2/2] clippy: Apply test suggestions. --- src/client/rpc_api.rs | 10 +++------- src/ledger/block.rs | 4 +--- src/ledger/script.rs | 26 ++++++++++++-------------- src/ledger/transactions.rs | 8 +++----- tests/raw_transactions.rs | 8 ++------ tests/transaction.rs | 6 ++---- 6 files changed, 23 insertions(+), 39 deletions(-) diff --git a/src/client/rpc_api.rs b/src/client/rpc_api.rs index 4b533cf..8677174 100644 --- a/src/client/rpc_api.rs +++ b/src/client/rpc_api.rs @@ -817,9 +817,7 @@ mod tests { .ledger .create_txout(Amount::from_sat(1), address.script_pubkey()); let tx = rpc.ledger.create_transaction(vec![], vec![txout]); - if let Ok(()) = rpc.ledger.check_transaction(&tx) { - assert!(false); - }; + assert!(rpc.ledger.check_transaction(&tx).is_err()); // Generating blocks should add funds to wallet. rpc.generate_to_address(101, &address).unwrap(); @@ -830,7 +828,7 @@ mod tests { txid: rpc .ledger ._get_transactions() - .get(0) + .first() .unwrap() .compute_txid(), vout: 0, @@ -842,9 +840,7 @@ mod tests { .ledger .create_txout(Amount::from_sat(1), address.script_pubkey()); let tx = rpc.ledger.create_transaction(vec![txin], vec![txout]); - if let Err(e) = rpc.ledger.check_transaction(&tx) { - assert!(false, "{:?}", e); - }; + rpc.ledger.check_transaction(&tx).unwrap(); } #[test] diff --git a/src/ledger/block.rs b/src/ledger/block.rs index bfb565a..46c2ae3 100644 --- a/src/ledger/block.rs +++ b/src/ledger/block.rs @@ -340,9 +340,7 @@ mod tests { assert_eq!(current_height, 1); assert_eq!(ledger.get_mempool_transactions().len(), 0); - if let Some(_) = ledger.get_mempool_transaction(tx.compute_txid()) { - assert!(false); - } + assert!(ledger.get_mempool_transaction(tx.compute_txid()).is_none()); } #[test] diff --git a/src/ledger/script.rs b/src/ledger/script.rs index 0327079..39b75db 100644 --- a/src/ledger/script.rs +++ b/src/ledger/script.rs @@ -181,7 +181,7 @@ mod tests { assert_eq!(ledger.get_block_height().unwrap(), 3); let script = Builder::new() - .push_int(0x1 as i64) + .push_int(0x1_i64) .push_opcode(OP_CSV) .push_opcode(OP_DROP) .push_x_only_key(&xonly_pk) @@ -195,7 +195,7 @@ mod tests { assert_eq!(ledger.get_block_height().unwrap(), 6); let script = Builder::new() - .push_int(0x1 as i64) + .push_int(0x1_i64) .push_opcode(OP_CSV) .push_opcode(OP_DROP) .push_x_only_key(&xonly_pk) @@ -208,22 +208,20 @@ mod tests { } assert_eq!(ledger.get_block_height().unwrap(), 9); let script = Builder::new() - .push_int(0x45 as i64) + .push_int(0x45_i64) .push_opcode(OP_CSV) .push_opcode(OP_DROP) .push_x_only_key(&xonly_pk) .push_opcode(OP_CHECKSIG) .into_script(); - if let Ok(_) = ledger.check_sequence(utxo, script, 0x46) { - assert!(false); - } + assert!(ledger.check_sequence(utxo, script, 0x46).is_err()); for _ in 0..0x100 { ledger.mine_block(&credential.address).unwrap(); } assert_eq!(ledger.get_block_height().unwrap(), 9 + 0x100); let script = Builder::new() - .push_int(0x100 as i64) + .push_int(0x100_i64) .push_opcode(OP_CSV) .push_opcode(OP_DROP) .push_x_only_key(&xonly_pk) @@ -271,13 +269,13 @@ mod tests { .push_x_only_key(&xonly_pk) .push_opcode(OP_CHECKSIG) .into_script(); - if let Ok(_) = ledger.check_sequence( - utxo, - script, - Sequence::from_512_second_intervals(0x44).to_consensus_u32(), - ) { - assert!(false); - }; + assert!(ledger + .check_sequence( + utxo, + script, + Sequence::from_512_second_intervals(0x44).to_consensus_u32(), + ) + .is_err()); ledger.mine_block(&credential.address).unwrap(); diff --git a/src/ledger/transactions.rs b/src/ledger/transactions.rs index c4cc567..af1fa66 100644 --- a/src/ledger/transactions.rs +++ b/src/ledger/transactions.rs @@ -422,7 +422,7 @@ mod tests { let txs = ledger._get_transactions(); assert_eq!(txs.len(), 1); - let tx2 = txs.get(0).unwrap().to_owned(); + let tx2 = txs.first().unwrap().to_owned(); assert_eq!(tx, tx2); let tx2 = ledger.get_transaction(txid).unwrap(); @@ -449,10 +449,8 @@ mod tests { ); // Input amount is zero. Same transaction should not be accepted, if - // checks are performed.. - if let Ok(_) = ledger.add_transaction(tx.clone()) { - assert!(false); - }; + // checks are performed. + assert!(ledger.add_transaction(tx.clone()).is_err()); // Create a valid transaction. This should pass checks. let txin = TxIn { diff --git a/tests/raw_transactions.rs b/tests/raw_transactions.rs index 6b73303..bb3c345 100644 --- a/tests/raw_transactions.rs +++ b/tests/raw_transactions.rs @@ -273,17 +273,13 @@ fn fund_sign_raw_transaction_with_wallet() { let tx = common::create_transaction(vec![], vec![txout]); // Lower input funds should be a problem. - if rpc.send_raw_transaction(&tx).is_ok() { - assert!(false); - }; + assert!(rpc.send_raw_transaction(&tx).is_err()); let res = rpc.fund_raw_transaction(&tx, None, None).unwrap(); let tx = bitcoin::consensus::encode::deserialize::(&res.hex).unwrap(); // Not signed inputs should be a problem. - if rpc.send_raw_transaction(&tx).is_ok() { - assert!(false); - }; + assert!(rpc.send_raw_transaction(&tx).is_err()); let res = rpc .sign_raw_transaction_with_wallet(&tx, None, None) diff --git a/tests/transaction.rs b/tests/transaction.rs index ab902bb..8835b41 100644 --- a/tests/transaction.rs +++ b/tests/transaction.rs @@ -66,7 +66,7 @@ fn use_utxo_from_send_to_address() { // assert_eq!(rpc.get_balance(None, None).unwrap(), deposit_value * 0x1F); let tx = rpc.get_raw_transaction(&txid, None).unwrap(); - assert_eq!(tx.output.get(0).unwrap().value, deposit_value * 0x1F); + assert_eq!(tx.output.first().unwrap().value, deposit_value * 0x1F); // Valid tx. let txin = TxIn { @@ -84,7 +84,5 @@ fn use_utxo_from_send_to_address() { deposit_address.script_pubkey(), ); let tx = common::create_transaction(vec![txin], vec![txout]); - if let Ok(_) = rpc.send_raw_transaction(&tx) { - assert!(false); - }; + assert!(rpc.send_raw_transaction(&tx).is_err()); }