Skip to content

Commit 7b331b3

Browse files
committed
Address clippy::indexing_slicing lint in stackslib
1 parent fe86198 commit 7b331b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1522
-1305
lines changed

.cargo/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
stacks-node = "run --package stacks-node --"
33
fmt-stacks = "fmt -- --config group_imports=StdExternalCrate,imports_granularity=Module"
44
clippy-stacks = "clippy -p stx-genesis -p libstackerdb -p stacks-signer -p pox-locking -p clarity -p libsigner -p stacks-common --no-deps --tests --all-features -- -D warnings"
5+
clippy-stackslib = "clippy -p stackslib --no-deps -- -Aclippy::all -Wclippy::indexing_slicing"
56

67
# Uncomment to improve performance slightly, at the cost of portability
78
# * Note that native binaries may not run on CPUs that are different from the build machine

stacks-common/src/types/chainstate.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,29 +64,24 @@ impl TrieHash {
6464
return TrieHash::from_empty_data();
6565
}
6666

67-
let mut tmp = [0u8; 32];
68-
6967
let mut hasher = Sha512_256::new();
7068
hasher.update(data);
71-
tmp.copy_from_slice(hasher.finalize().as_slice());
72-
73-
TrieHash(tmp)
69+
let out = hasher.finalize().into();
70+
TrieHash(out)
7471
}
7572

7673
pub fn from_data_array<B: AsRef<[u8]>>(data: &[B]) -> TrieHash {
7774
if data.is_empty() {
7875
return TrieHash::from_empty_data();
7976
}
8077

81-
let mut tmp = [0u8; 32];
82-
8378
let mut hasher = Sha512_256::new();
8479

8580
for item in data.iter() {
8681
hasher.update(item);
8782
}
88-
tmp.copy_from_slice(hasher.finalize().as_slice());
89-
TrieHash(tmp)
83+
let out = hasher.finalize().into();
84+
TrieHash(out)
9085
}
9186

9287
/// Convert to a String that can be used in e.g. sqlite
@@ -493,9 +488,8 @@ impl BurnchainHeaderHash {
493488
}
494489

495490
pub fn to_bitcoin_hash(&self) -> Sha256dHash {
496-
let bytes = self.0.iter().rev().copied().collect::<Vec<_>>();
497-
let mut buf = [0u8; 32];
498-
buf.copy_from_slice(&bytes[0..32]);
491+
let mut buf = self.0.clone();
492+
buf.reverse();
499493
Sha256dHash(buf)
500494
}
501495

stacks-common/src/types/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,12 @@ impl<L: PartialEq + Eq> Ord for StacksEpoch<L> {
762762
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq)]
763763
pub struct EpochList<L: Clone>(Vec<StacksEpoch<L>>);
764764

765+
impl<L: Clone> From<Vec<StacksEpoch<L>>> for EpochList<L> {
766+
fn from(value: Vec<StacksEpoch<L>>) -> Self {
767+
Self(value)
768+
}
769+
}
770+
765771
impl<L: Clone> EpochList<L> {
766772
pub fn new(epochs: &[StacksEpoch<L>]) -> EpochList<L> {
767773
EpochList(epochs.to_vec())
@@ -804,8 +810,8 @@ impl<L: Clone> EpochList<L> {
804810
self.0.push(epoch);
805811
}
806812

807-
pub fn to_vec(&self) -> Vec<StacksEpoch<L>> {
808-
self.0.clone()
813+
pub fn to_vec(self) -> Vec<StacksEpoch<L>> {
814+
self.0
809815
}
810816
}
811817

stacks-common/src/types/net.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,8 @@ impl<'de> Deserialize<'de> for PeerAddress {
7373

7474
impl PeerAddress {
7575
pub fn from_slice(bytes: &[u8]) -> Option<PeerAddress> {
76-
if bytes.len() != 16 {
77-
return None;
78-
}
79-
80-
let mut bytes16 = [0u8; 16];
81-
bytes16.copy_from_slice(&bytes[0..16]);
82-
Some(PeerAddress(bytes16))
76+
let bytes16: &[u8; 16] = bytes.try_into().ok()?;
77+
Some(PeerAddress(*bytes16))
8378
}
8479

8580
/// Is this an IPv4 address?
@@ -98,9 +93,7 @@ impl PeerAddress {
9893
{
9994
return None;
10095
}
101-
let mut ret = [0u8; 4];
102-
ret.copy_from_slice(&self.0[12..16]);
103-
Some(ret)
96+
Some([self[12], self[13], self[14], self[15]])
10497
}
10598

10699
/// Return the bit representation of this peer address as an IPv4 address, in network byte

stacks-common/src/util/hash.rs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,8 @@ const MERKLE_PATH_NODE_TAG: u8 = 0x01;
172172
impl Hash160 {
173173
pub fn from_sha256(sha256_hash: &[u8; 32]) -> Hash160 {
174174
let mut rmd = Ripemd160::new();
175-
let mut ret = [0u8; 20];
176175
rmd.update(sha256_hash);
177-
ret.copy_from_slice(rmd.finalize().as_slice());
176+
let ret = rmd.finalize().into();
178177
Hash160(ret)
179178
}
180179

@@ -183,7 +182,7 @@ impl Hash160 {
183182
pub fn from_data(data: &[u8]) -> Hash160 {
184183
let sha2_result = Sha256::digest(data);
185184
let ripe_160_result = Ripemd160::digest(sha2_result.as_slice());
186-
Hash160::from(ripe_160_result.as_slice())
185+
Hash160(ripe_160_result.into())
187186
}
188187

189188
pub fn from_node_public_key(pubkey: &Secp256k1PublicKey) -> Hash160 {
@@ -197,16 +196,16 @@ impl Hash160 {
197196

198197
impl Sha512Sum {
199198
pub fn from_data(data: &[u8]) -> Sha512Sum {
200-
Sha512Sum::from(Sha512::digest(data).as_slice())
199+
Sha512Sum(Sha512::digest(data).into())
201200
}
202201
}
203202

204203
impl Sha512Trunc256Sum {
205204
pub fn from_data(data: &[u8]) -> Sha512Trunc256Sum {
206-
Sha512Trunc256Sum::from(Sha512_256::digest(data).as_slice())
205+
Sha512Trunc256Sum(Sha512_256::digest(data).into())
207206
}
208207
pub fn from_hasher(hasher: Sha512_256) -> Sha512Trunc256Sum {
209-
Sha512Trunc256Sum::from(hasher.finalize().as_slice())
208+
Sha512Trunc256Sum(hasher.finalize().into())
210209
}
211210
}
212211

@@ -216,12 +215,11 @@ impl MerkleHashFunc for Hash160 {
216215
}
217216

218217
fn from_tagged_data(tag: u8, data: &[u8]) -> Hash160 {
219-
let mut tmp = [0u8; 32];
220218
let mut sha2 = Sha256::new();
221219
sha2.update([tag]);
222220
sha2.update(data);
223-
tmp.copy_from_slice(sha2.finalize().as_slice());
224-
Hash160::from_sha256(&tmp)
221+
let sha2_bytes = sha2.finalize().into();
222+
Hash160::from_sha256(&sha2_bytes)
225223
}
226224

227225
fn bits(&self) -> &[u8] {
@@ -235,14 +233,12 @@ impl MerkleHashFunc for Sha256Sum {
235233
}
236234

237235
fn from_tagged_data(tag: u8, data: &[u8]) -> Sha256Sum {
238-
let mut tmp = [0u8; 32];
239-
240236
let mut sha2 = Sha256::new();
241237
sha2.update([tag]);
242238
sha2.update(data);
243-
tmp.copy_from_slice(sha2.finalize().as_slice());
239+
let out = sha2.finalize().into();
244240

245-
Sha256Sum(tmp)
241+
Sha256Sum(out)
246242
}
247243

248244
fn bits(&self) -> &[u8] {
@@ -256,19 +252,15 @@ impl MerkleHashFunc for DoubleSha256 {
256252
}
257253

258254
fn from_tagged_data(tag: u8, data: &[u8]) -> DoubleSha256 {
259-
let mut tmp = [0u8; 32];
260-
let mut tmp2 = [0u8; 32];
261-
262255
let mut sha2_1 = Sha256::new();
263256
sha2_1.update([tag]);
264257
sha2_1.update(data);
265-
tmp.copy_from_slice(sha2_1.finalize().as_slice());
266258

267259
let mut sha2_2 = Sha256::new();
268-
sha2_2.update(tmp);
269-
tmp2.copy_from_slice(sha2_2.finalize().as_slice());
260+
sha2_2.update(sha2_1.finalize().as_slice());
261+
let ret = sha2_2.finalize().into();
270262

271-
DoubleSha256(tmp2)
263+
DoubleSha256(ret)
272264
}
273265

274266
fn bits(&self) -> &[u8] {
@@ -282,15 +274,12 @@ impl MerkleHashFunc for Sha512Trunc256Sum {
282274
}
283275

284276
fn from_tagged_data(tag: u8, data: &[u8]) -> Sha512Trunc256Sum {
285-
use sha2::Digest;
286-
let mut tmp = [0u8; 32];
287-
288277
let mut sha2 = Sha512_256::new();
289278
sha2.update([tag]);
290279
sha2.update(data);
291-
tmp.copy_from_slice(sha2.finalize().as_slice());
280+
let ret = sha2.finalize().into();
292281

293-
Sha512Trunc256Sum(tmp)
282+
Sha512Trunc256Sum(ret)
294283
}
295284

296285
fn bits(&self) -> &[u8] {

stackslib/src/blockstack_cli.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ fn sign_transaction_single_sig_standard(
329329
.ok_or("TX did not finish signing -- was this a standard single signature transaction?")?)
330330
}
331331

332+
#[allow(clippy::indexing_slicing)]
332333
fn parse_anchor_mode(
333334
args: &mut Vec<String>,
334335
usage: &str,
@@ -367,6 +368,7 @@ fn parse_anchor_mode(
367368
}
368369
}
369370

371+
#[allow(clippy::indexing_slicing)]
370372
fn handle_contract_publish(
371373
args_slice: &[String],
372374
version: TransactionVersion,
@@ -425,6 +427,7 @@ fn handle_contract_publish(
425427
Ok(to_hex(&signed_tx_bytes))
426428
}
427429

430+
#[allow(clippy::indexing_slicing)]
428431
fn handle_contract_call(
429432
args_slice: &[String],
430433
version: TransactionVersion,
@@ -511,6 +514,7 @@ fn handle_contract_call(
511514
Ok(to_hex(&signed_tx_bytes))
512515
}
513516

517+
#[allow(clippy::indexing_slicing)]
514518
fn handle_token_transfer(
515519
args_slice: &[String],
516520
version: TransactionVersion,
@@ -573,6 +577,7 @@ fn handle_token_transfer(
573577
Ok(to_hex(&signed_tx_bytes))
574578
}
575579

580+
#[allow(clippy::indexing_slicing)]
576581
fn generate_secret_key(args: &[String], version: TransactionVersion) -> Result<String, CliError> {
577582
if !args.is_empty() && args[0] == "-h" {
578583
return Err(CliError::Message(format!("USAGE:\n {}", GENERATE_USAGE)));
@@ -604,6 +609,7 @@ fn generate_secret_key(args: &[String], version: TransactionVersion) -> Result<S
604609
))
605610
}
606611

612+
#[allow(clippy::indexing_slicing)]
607613
fn get_addresses(args: &[String], version: TransactionVersion) -> Result<String, CliError> {
608614
if (!args.is_empty() && args[0] == "-h") || args.len() != 1 {
609615
return Err(CliError::Message(format!("USAGE:\n {}", ADDRESSES_USAGE)));
@@ -643,6 +649,7 @@ fn get_addresses(args: &[String], version: TransactionVersion) -> Result<String,
643649
))
644650
}
645651

652+
#[allow(clippy::indexing_slicing)]
646653
fn decode_transaction(args: &[String], _version: TransactionVersion) -> Result<String, CliError> {
647654
if (!args.is_empty() && args[0] == "-h") || args.len() != 1 {
648655
return Err(CliError::Message(format!(
@@ -681,6 +688,7 @@ fn decode_transaction(args: &[String], _version: TransactionVersion) -> Result<S
681688
}
682689
}
683690

691+
#[allow(clippy::indexing_slicing)]
684692
fn decode_header(args: &[String], _version: TransactionVersion) -> Result<String, CliError> {
685693
if (!args.is_empty() && args[0] == "-h") || args.len() != 1 {
686694
return Err(CliError::Message(format!(
@@ -720,6 +728,7 @@ fn decode_header(args: &[String], _version: TransactionVersion) -> Result<String
720728
}
721729
}
722730

731+
#[allow(clippy::indexing_slicing)]
723732
fn decode_block(args: &[String], _version: TransactionVersion) -> Result<String, CliError> {
724733
if (!args.is_empty() && args[0] == "-h") || args.len() != 1 {
725734
return Err(CliError::Message(format!(
@@ -757,6 +766,7 @@ fn decode_block(args: &[String], _version: TransactionVersion) -> Result<String,
757766
}
758767
}
759768

769+
#[allow(clippy::indexing_slicing)]
760770
fn decode_microblock(args: &[String], _version: TransactionVersion) -> Result<String, CliError> {
761771
if (!args.is_empty() && args[0] == "-h") || args.len() != 1 {
762772
return Err(CliError::Message(format!(
@@ -796,6 +806,7 @@ fn decode_microblock(args: &[String], _version: TransactionVersion) -> Result<St
796806
}
797807
}
798808

809+
#[allow(clippy::indexing_slicing)]
799810
fn decode_microblocks(args: &[String], _version: TransactionVersion) -> Result<String, CliError> {
800811
if (!args.is_empty() && args[0] == "-h") || args.len() != 1 {
801812
return Err(CliError::Message(format!(

0 commit comments

Comments
 (0)