refactor: enable redundant_clone lint and fix warnings
What changed, and why it matters
This commit is a routine code cleanup: it turns on a Rust linter warning for unnecessary .clone() calls and removes those unnecessary clones. There is no security-relevant behavior change.
No security action needed; this is a normal refactoring/lint cleanup commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit enables the Clippy redundant_clone lint across workspace Cargo.toml files and fixes the resulting warnings. All source changes are mechanical removals of redundant .clone() calls or equivalent refactorings in test code (e.g., moving a value instead of cloning it, constructing a fresh value instead of cloning). No logic, API, or cryptographic behavior is altered.
Changed components
addresses/Cargo.tomlbase58/Cargo.tomlbitcoin/Cargo.tomlbitcoin/src/consensus/encode.rsbitcoin/src/psbt/mod.rsbitcoin/tests/psbt-sign-taproot.rschacha20_poly1305/Cargo.tomlconsensus_encoding/Cargo.tomlfuzz/Cargo.tomlhashes/Cargo.tomlinternals/Cargo.tomlio/Cargo.tomlp2p/Cargo.tomlprimitives/Cargo.tomlprimitives/src/script/mod.rsprimitives/src/script/owned.rsprimitives/src/transaction.rsprimitives/src/witness.rsunits/Cargo.tomlInspect captured patch +28 / −20
diff --git a/addresses/Cargo.toml b/addresses/Cargo.toml
index f2d878d9..bc9066de 100644
--- a/addresses/Cargo.toml
+++ b/addresses/Cargo.toml
@@ -26,4 +26,5 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[lints.clippy]
+redundant_clone = "warn"
use_self = "warn"
diff --git a/base58/Cargo.toml b/base58/Cargo.toml
index 731bab4f..a87ec890 100644
--- a/base58/Cargo.toml
+++ b/base58/Cargo.toml
@@ -32,4 +32,5 @@ rustdoc-args = ["--cfg", "docsrs"]
unexpected_cfgs = { level = "deny", check-cfg = ['cfg(bench)', 'cfg(fuzzing)', 'cfg(kani)' ] }
[lints.clippy]
+redundant_clone = "warn"
use_self = "warn"
diff --git a/bitcoin/Cargo.toml b/bitcoin/Cargo.toml
index 12e4e289..a09076e2 100644
--- a/bitcoin/Cargo.toml
+++ b/bitcoin/Cargo.toml
@@ -103,4 +103,5 @@ required-features = ["std", "serde"]
unexpected_cfgs = { level = "deny", check-cfg = ['cfg(fuzzing)', 'cfg(kani)'] }
[lints.clippy]
+redundant_clone = "warn"
use_self = "warn"
diff --git a/bitcoin/src/consensus/encode.rs b/bitcoin/src/consensus/encode.rs
index bad559bb..90e44d0c 100644
--- a/bitcoin/src/consensus/encode.rs
+++ b/bitcoin/src/consensus/encode.rs
@@ -1094,7 +1094,7 @@ mod tests {
// 4. Test with a large script sig. With scriptsigs there is no limit on how large
// an object we can parse, which is inconsistent with witnesses. Also not an
// API guarantee.
- let mut tx_copy = tx.clone();
+ let mut tx_copy = tx;
tx_copy.inputs[0].script_sig = ScriptSigBuf::from(vec![0; 8_000_001]);
let roundtrip = deserialize(&serialize(&tx_copy)).unwrap();
assert_eq!(tx_copy, roundtrip);
diff --git a/bitcoin/src/psbt/mod.rs b/bitcoin/src/psbt/mod.rs
index cda173cf..c62c25d9 100644
--- a/bitcoin/src/psbt/mod.rs
+++ b/bitcoin/src/psbt/mod.rs
@@ -2543,7 +2543,7 @@ mod tests {
e => panic!("unexpected error: {:?}", e),
}
// negative fee
- let mut t3 = t.clone();
+ let mut t3 = t;
t3.unsigned_tx.outputs[0].amount = prev_output_val;
match t3.fee().unwrap_err() {
Error::NegativeFee => {}
diff --git a/bitcoin/tests/psbt-sign-taproot.rs b/bitcoin/tests/psbt-sign-taproot.rs
index 4d4356ac..f882eb41 100644
--- a/bitcoin/tests/psbt-sign-taproot.rs
+++ b/bitcoin/tests/psbt-sign-taproot.rs
@@ -65,7 +65,7 @@ fn psbt_sign_taproot() {
let internal_key = kp.x_only_public_key().0; // Ignore the parity.
let tree =
- create_taproot_tree(secp, script1.clone(), script2.clone(), script3.clone(), internal_key);
+ create_taproot_tree(secp, script1, script2.clone(), script3, internal_key);
let address = create_p2tr_address(tree.clone());
assert_eq!(
@@ -129,7 +129,7 @@ fn psbt_sign_taproot() {
let mut psbt_script_path_spend = create_psbt_for_taproot_script_path_spend(
address,
to_address,
- tree.clone(),
+ tree,
x_only_pubkey,
signing_key_path,
script2.clone(),
@@ -145,7 +145,7 @@ fn psbt_sign_taproot() {
sig,
psbt_script_path_spend.inputs[0]
.tap_script_sigs
- .get(&(x_only_pubkey.into(), script2.clone().tapscript_leaf_hash()))
+ .get(&(x_only_pubkey.into(), script2.tapscript_leaf_hash()))
.unwrap()
.signature
.to_string()
@@ -314,7 +314,7 @@ fn create_psbt_for_taproot_script_path_spend<K: Into<XOnlyPublicKey>>(
let mut tap_scripts = BTreeMap::new();
tap_scripts.insert(
tree.control_block(&(use_script.clone(), LeafVersion::TapScript)).unwrap(),
- (use_script.clone(), LeafVersion::TapScript),
+ (use_script, LeafVersion::TapScript),
);
let mut input = Input {
diff --git a/chacha20_poly1305/Cargo.toml b/chacha20_poly1305/Cargo.toml
index 5b1f15cf..8abd82a8 100644
--- a/chacha20_poly1305/Cargo.toml
+++ b/chacha20_poly1305/Cargo.toml
@@ -28,4 +28,5 @@ rustdoc-args = ["--cfg", "docsrs"]
unexpected_cfgs = { level = "deny", check-cfg = ['cfg(bench)'] }
[lints.clippy]
+redundant_clone = "warn"
use_self = "warn"
diff --git a/consensus_encoding/Cargo.toml b/consensus_encoding/Cargo.toml
index 0db56de4..72ecdb75 100644
--- a/consensus_encoding/Cargo.toml
+++ b/consensus_encoding/Cargo.toml
@@ -119,6 +119,7 @@ ptr_cast_constness = "warn"
pub_underscore_fields = "warn"
range_minus_one = "warn"
range_plus_one = "warn"
+redundant_clone = "warn"
redundant_closure_for_method_calls = "warn"
redundant_else = "warn"
ref_as_ptr = "warn"
diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml
index 4ae3c1ef..72288071 100644
--- a/fuzz/Cargo.toml
+++ b/fuzz/Cargo.toml
@@ -23,6 +23,7 @@ serde_json = "1.0.68"
unexpected_cfgs = { level = "deny", check-cfg = ['cfg(fuzzing)'] }
[lints.clippy]
+redundant_clone = "warn"
use_self = "warn"
[[bin]]
diff --git a/hashes/Cargo.toml b/hashes/Cargo.toml
index 32dd8f64..8f404e97 100644
--- a/hashes/Cargo.toml
+++ b/hashes/Cargo.toml
@@ -39,4 +39,5 @@ rustdoc-args = ["--cfg", "docsrs"]
unexpected_cfgs = { level = "deny", check-cfg = ['cfg(hashes_fuzz)', 'cfg(rust_v_1_64)' ] }
[lints.clippy]
+redundant_clone = "warn"
use_self = "warn"
diff --git a/internals/Cargo.toml b/internals/Cargo.toml
index 2c41b3c3..5f519a5e 100644
--- a/internals/Cargo.toml
+++ b/internals/Cargo.toml
@@ -38,4 +38,5 @@ rustdoc-args = ["--cfg", "docsrs"]
unexpected_cfgs = { level = "deny", check-cfg = ['cfg(kani)'] }
[lints.clippy]
+redundant_clone = "warn"
use_self = "warn"
diff --git a/io/Cargo.toml b/io/Cargo.toml
index 6faf58d4..4129ae92 100644
--- a/io/Cargo.toml
+++ b/io/Cargo.toml
@@ -35,4 +35,5 @@ rustdoc-args = ["--cfg", "docsrs"]
unexpected_cfgs = { level = "deny" }
[lints.clippy]
+redundant_clone = "warn"
use_self = "warn"
diff --git a/p2p/Cargo.toml b/p2p/Cargo.toml
index 42d28596..f377fbf1 100644
--- a/p2p/Cargo.toml
+++ b/p2p/Cargo.toml
@@ -39,4 +39,5 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[lints.clippy]
+redundant_clone = "warn"
use_self = "warn"
diff --git a/primitives/Cargo.toml b/primitives/Cargo.toml
index 90cb37a5..a4539018 100644
--- a/primitives/Cargo.toml
+++ b/primitives/Cargo.toml
@@ -129,6 +129,7 @@ ptr_cast_constness = "warn"
pub_underscore_fields = "warn"
range_minus_one = "warn"
range_plus_one = "warn"
+redundant_clone = "warn"
redundant_closure_for_method_calls = "warn"
redundant_else = "warn"
ref_as_ptr = "warn"
diff --git a/primitives/src/script/mod.rs b/primitives/src/script/mod.rs
index 317c1b54..173a7c2c 100644
--- a/primitives/src/script/mod.rs
+++ b/primitives/src/script/mod.rs
@@ -762,8 +762,7 @@ mod tests {
#[test]
fn cow_scriptbuf_to_script() {
- let script_buf = ScriptBuf::from(vec![0x51, 0x52, 0x53]);
- let cow_owned: Cow<Script> = Cow::Owned(script_buf.clone());
+ let cow_owned: Cow<Script> = Cow::Owned(ScriptBuf::from(vec![0x51, 0x52, 0x53]));
let script: &Script = cow_owned.borrow();
assert_eq!(script.as_bytes(), &[0x51, 0x52, 0x53]);
}
diff --git a/primitives/src/script/owned.rs b/primitives/src/script/owned.rs
index eef5a5aa..463b4711 100644
--- a/primitives/src/script/owned.rs
+++ b/primitives/src/script/owned.rs
@@ -240,11 +240,10 @@ mod tests {
#[test]
fn script_buf_as_mut_script() {
- let bytes = vec![1, 2, 3];
- let mut script = ScriptBuf::from_bytes(bytes.clone());
+ let mut script = ScriptBuf::from_bytes(vec![1, 2, 3]);
let script_mut_ref = script.as_mut_script();
script_mut_ref.as_mut_bytes()[0] = 4;
- assert_eq!(script.as_mut_bytes(), vec![4, 2, 3]);
+ assert_eq!(script.as_mut_bytes(), &[4, 2, 3]);
}
#[test]
diff --git a/primitives/src/transaction.rs b/primitives/src/transaction.rs
index 01309ba6..33aac638 100644
--- a/primitives/src/transaction.rs
+++ b/primitives/src/transaction.rs
@@ -1550,8 +1550,8 @@ mod tests {
let tx_orig = Transaction {
version: Version::ONE,
lock_time: absolute::LockTime::from_consensus(1_738_968_231), // The time this was written
- inputs: vec![txin.clone()],
- outputs: vec![txout.clone()],
+ inputs: vec![txin],
+ outputs: vec![txout],
};
// Test changing the transaction
diff --git a/primitives/src/witness.rs b/primitives/src/witness.rs
index 54dae968..7ace35d6 100644
--- a/primitives/src/witness.rs
+++ b/primitives/src/witness.rs
@@ -948,7 +948,7 @@ mod test {
let content = append_u32_vec(&elements, &[0, 2]);
let indices_start = elements.len();
let witness =
- Witness::from_parts__unstable(content.clone(), witness_elements, indices_start);
+ Witness::from_parts__unstable(content, witness_elements, indices_start);
assert_eq!(witness.get(0).unwrap(), [11_u8]);
assert_eq!(witness.get(1).unwrap(), [21_u8, 22]);
assert_eq!(witness.size(), 6);
@@ -1022,8 +1022,6 @@ mod test {
}};
}
- let witness = Witness::from_slice(&[DATA_1, DATA_2]);
-
// &[T]
let container: &[&[u8]] = &[EMPTY_BYTES];
let different: &[&[u8]] = &[DATA_1];
@@ -1046,22 +1044,22 @@ mod test {
// Vec<T>
let container: Vec<&[u8]> = vec![DATA_1, DATA_2];
let different: Vec<&[u8]> = vec![DATA_2, DATA_1];
- ck!(witness.clone(), container, different);
+ ck!(Witness::from(container.as_slice()), container, different);
// Box<[T]>
let container: Box<[&[u8]]> = vec![DATA_1, DATA_2].into_boxed_slice();
let different: Box<[&[u8]]> = vec![DATA_2, DATA_1].into_boxed_slice();
- ck!(witness.clone(), container, different);
+ ck!(Witness::from(&*container), container, different);
// Rc<[T]>
let container: alloc::rc::Rc<[&[u8]]> = vec![DATA_1, DATA_2].into();
let different: alloc::rc::Rc<[&[u8]]> = vec![DATA_2, DATA_1].into();
- ck!(witness.clone(), container, different);
+ ck!(Witness::from(&*container), container, different);
// Arc<[T]>
let container: alloc::sync::Arc<[&[u8]]> = vec![DATA_1, DATA_2].into();
let different: alloc::sync::Arc<[&[u8]]> = vec![DATA_2, DATA_1].into();
- ck!(witness, container, different);
+ ck!(Witness::from(&*container), container, different);
}
#[test]
diff --git a/units/Cargo.toml b/units/Cargo.toml
index e1e6293d..f4c0d301 100644
--- a/units/Cargo.toml
+++ b/units/Cargo.toml
@@ -127,6 +127,7 @@ ptr_cast_constness = "warn"
pub_underscore_fields = "warn"
range_minus_one = "warn"
range_plus_one = "warn"
+redundant_clone = "warn"
redundant_closure_for_method_calls = "warn"
redundant_else = "warn"
ref_as_ptr = "warn"
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.