Skip to content

Commit 6d2831d

Browse files
authored
Merge pull request #6188 from kantai/feat/clippy-stackslib
Feat: Add clippy stackslib, pass indexing_slicing
2 parents 97a96fc + e0b6ffe commit 6d2831d

Some content is hidden

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

84 files changed

+1665
-1440
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 -A clippy::uninlined-format-args"
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;
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: 15 additions & 26 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] {
@@ -588,7 +577,7 @@ where
588577
// borrowed from Andrew Poelstra's rust-bitcoin library
589578
/// Convert a hexadecimal-encoded string to its corresponding bytes
590579
pub fn hex_bytes(s: &str) -> Result<Vec<u8>, HexError> {
591-
let mut v = vec![];
580+
let mut v = Vec::with_capacity(s.len() / 2);
592581
let mut iter = s.chars().pair();
593582
// Do the parsing
594583
iter.by_ref()

stackslib/src/blockstack_cli.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ fn parse_postcondition_mode(
520520
}
521521
}
522522

523+
#[allow(clippy::indexing_slicing)]
523524
fn handle_contract_publish(
524525
args_slice: &[String],
525526
version: TransactionVersion,
@@ -580,6 +581,7 @@ fn handle_contract_publish(
580581
Ok(to_hex(&signed_tx_bytes))
581582
}
582583

584+
#[allow(clippy::indexing_slicing)]
583585
fn handle_contract_call(
584586
args_slice: &[String],
585587
version: TransactionVersion,
@@ -674,6 +676,7 @@ fn handle_contract_call(
674676
Ok(to_hex(&signed_tx_bytes))
675677
}
676678

679+
#[allow(clippy::indexing_slicing)]
677680
fn handle_token_transfer(
678681
args_slice: &[String],
679682
version: TransactionVersion,
@@ -736,6 +739,7 @@ fn handle_token_transfer(
736739
Ok(to_hex(&signed_tx_bytes))
737740
}
738741

742+
#[allow(clippy::indexing_slicing)]
739743
fn generate_secret_key(args: &[String], version: TransactionVersion) -> Result<String, CliError> {
740744
if !args.is_empty() && args[0] == "-h" {
741745
return Err(CliError::Message(format!("USAGE:\n {}", GENERATE_USAGE)));
@@ -767,6 +771,7 @@ fn generate_secret_key(args: &[String], version: TransactionVersion) -> Result<S
767771
))
768772
}
769773

774+
#[allow(clippy::indexing_slicing)]
770775
fn get_addresses(args: &[String], version: TransactionVersion) -> Result<String, CliError> {
771776
if (!args.is_empty() && args[0] == "-h") || args.len() != 1 {
772777
return Err(CliError::Message(format!("USAGE:\n {}", ADDRESSES_USAGE)));
@@ -806,6 +811,7 @@ fn get_addresses(args: &[String], version: TransactionVersion) -> Result<String,
806811
))
807812
}
808813

814+
#[allow(clippy::indexing_slicing)]
809815
fn decode_transaction(args: &[String], _version: TransactionVersion) -> Result<String, CliError> {
810816
if (!args.is_empty() && args[0] == "-h") || args.len() != 1 {
811817
return Err(CliError::Message(format!(
@@ -844,6 +850,7 @@ fn decode_transaction(args: &[String], _version: TransactionVersion) -> Result<S
844850
}
845851
}
846852

853+
#[allow(clippy::indexing_slicing)]
847854
fn decode_header(args: &[String], _version: TransactionVersion) -> Result<String, CliError> {
848855
if (!args.is_empty() && args[0] == "-h") || args.len() != 1 {
849856
return Err(CliError::Message(format!(
@@ -883,6 +890,7 @@ fn decode_header(args: &[String], _version: TransactionVersion) -> Result<String
883890
}
884891
}
885892

893+
#[allow(clippy::indexing_slicing)]
886894
fn decode_block(args: &[String], _version: TransactionVersion) -> Result<String, CliError> {
887895
if (!args.is_empty() && args[0] == "-h") || args.len() != 1 {
888896
return Err(CliError::Message(format!(
@@ -920,6 +928,7 @@ fn decode_block(args: &[String], _version: TransactionVersion) -> Result<String,
920928
}
921929
}
922930

931+
#[allow(clippy::indexing_slicing)]
923932
fn decode_microblock(args: &[String], _version: TransactionVersion) -> Result<String, CliError> {
924933
if (!args.is_empty() && args[0] == "-h") || args.len() != 1 {
925934
return Err(CliError::Message(format!(
@@ -959,6 +968,7 @@ fn decode_microblock(args: &[String], _version: TransactionVersion) -> Result<St
959968
}
960969
}
961970

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

0 commit comments

Comments
 (0)