Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stacks-common to clippy CI and fix errors #5598

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: -p libstackerdb -p stacks-signer -p pox-locking -p clarity -p libsigner --no-deps --tests --all-features -- -D warnings
args: -p libstackerdb -p stacks-signer -p pox-locking -p clarity -p libsigner -p stacks-common --no-deps --tests --all-features -- -D warnings
3 changes: 1 addition & 2 deletions stacks-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ path = "./src/libcommon.rs"

[dependencies]
rand = { workspace = true }
serde = "1"
serde = { version = "1.0", features = ["derive"] }
serde_derive = "1"
serde_stacker = "0.1"
sha3 = "0.10.1"
Expand Down Expand Up @@ -77,7 +77,6 @@ testing = ["canonical"]
serde = []
bech32_std = []
bech32_strict = []
strason = []

[target.'cfg(all(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64"), not(any(target_os="windows"))))'.dependencies]
sha2 = { version = "0.10", features = ["asm"] }
Expand Down
8 changes: 6 additions & 2 deletions stacks-common/src/bitvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub struct BitVecIter<'a, const MAX_SIZE: u16> {
bitvec: &'a BitVec<MAX_SIZE>,
}

impl<'a, const MAX_SIZE: u16> Iterator for BitVecIter<'a, MAX_SIZE> {
impl<const MAX_SIZE: u16> Iterator for BitVecIter<'_, MAX_SIZE> {
type Item = bool;

fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -172,7 +172,7 @@ impl<const MAX_SIZE: u16> BitVec<MAX_SIZE> {
}

pub fn iter(&self) -> BitVecIter<MAX_SIZE> {
let byte = self.data.get(0);
let byte = self.data.first();
BitVecIter {
index: 0,
bitvec: self,
Expand All @@ -184,6 +184,10 @@ impl<const MAX_SIZE: u16> BitVec<MAX_SIZE> {
self.len
}

pub fn is_empty(&self) -> bool {
self.len == 0
}

/// Return the number of bytes needed to store `len` bits.
fn data_len(len: u16) -> u16 {
len / 8 + if len % 8 == 0 { 0 } else { 1 }
Expand Down
4 changes: 2 additions & 2 deletions stacks-common/src/deps_common/bech32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl<'a> Bech32Writer<'a> {
}
}

impl<'a> WriteBase32 for Bech32Writer<'a> {
impl WriteBase32 for Bech32Writer<'_> {
type Err = fmt::Error;

/// Writes a single 5 bit value of the data part
Expand All @@ -211,7 +211,7 @@ impl<'a> WriteBase32 for Bech32Writer<'a> {
}
}

impl<'a> Drop for Bech32Writer<'a> {
impl Drop for Bech32Writer<'_> {
fn drop(&mut self) {
self.write_checksum()
.expect("Unhandled error writing the checksum on drop.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ impl All {
Class::PushBytes(*self as u32)
// 60 opcodes
} else {
Class::Ordinary(unsafe { transmute(*self) })
Class::Ordinary(unsafe { transmute::<All, Ordinary>(*self) })
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions stacks-common/src/deps_common/bitcoin/blockdata/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ impl<'de> serde::Deserialize<'de> for Script {
where
E: serde::de::Error,
{
let v: Vec<u8> = ::hex::decode(v).map_err(E::custom)?;
let v: Vec<u8> = crate::util::hash::hex_bytes(v).map_err(E::custom)?;
Ok(Script::from(v))
}

Expand Down Expand Up @@ -834,15 +834,15 @@ mod test {
}

#[test]
#[cfg(all(feature = "serde", feature = "strason"))]
#[cfg(feature = "serde")]
fn script_json_serialize() {
use strason::Json;
use serde_json;

let original = hex_script!("827651a0698faaa9a8a7a687");
let json = Json::from_serialize(&original).unwrap();
assert_eq!(json.to_bytes(), b"\"827651a0698faaa9a8a7a687\"");
assert_eq!(json.string(), Some("827651a0698faaa9a8a7a687"));
let des = json.into_deserialize().unwrap();
let json_value = serde_json::to_value(&original).unwrap();
assert_eq!(serde_json::to_vec(&json_value).unwrap(), b"\"827651a0698faaa9a8a7a687\"");
assert_eq!(json_value.to_string(), "\"827651a0698faaa9a8a7a687\"");
let des = serde_json::from_value(json_value).unwrap();
assert_eq!(original, des);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl Transaction {
let mut script_len_bytes =
serialize(&script_len).expect("FATAL: failed to encode varint");
length_script.append(&mut script_len_bytes);
length_script.extend_from_slice(&script_bytes);
length_script.extend_from_slice(script_bytes);
length_script
}
}
Expand All @@ -361,7 +361,7 @@ impl Transaction {
let mut script_len_bytes =
serialize(&script_len).expect("FATAL: failed to encode varint");
raw_vec.append(&mut script_len_bytes);
raw_vec.extend_from_slice(&script_bytes);
raw_vec.extend_from_slice(script_bytes);
}
Sha256dHash::from_data(&raw_vec)
} else if sighash_type == SigHashType::Single && input_index < self.output.len() {
Expand Down
40 changes: 20 additions & 20 deletions stacks-common/src/deps_common/bitcoin/internal_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,16 @@ macro_rules! user_enum {
}

#[cfg(feature = "serde")]
impl<'de> $crate::serde::Deserialize<'de> for $name {
impl<'de> serde::Deserialize<'de> for $name {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: $crate::serde::Deserializer<'de>,
D: serde::Deserializer<'de>,
{
use $crate::std::fmt::{self, Formatter};
use std::fmt::{self, Formatter};

struct Visitor;
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = $name;

fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
Expand All @@ -124,7 +124,7 @@ macro_rules! user_enum {

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: $crate::serde::de::Error,
E: serde::de::Error,
{
static FIELDS: &'static [&'static str] = &[$(stringify!($txt)),*];

Expand All @@ -136,14 +136,14 @@ macro_rules! user_enum {

fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
where
E: $crate::serde::de::Error,
E: serde::de::Error,
{
self.visit_str(v)
}

fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: $crate::serde::de::Error,
E: serde::de::Error,
{
self.visit_str(&v)
}
Expand Down Expand Up @@ -222,19 +222,19 @@ macro_rules! hex_script (($s:expr) => (crate::deps_common::bitcoin::blockdata::s
macro_rules! serde_struct_impl {
($name:ident, $($fe:ident),*) => (
#[cfg(feature = "serde")]
impl<'de> $crate::serde::Deserialize<'de> for $name {
impl<'de> serde::Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> Result<$name, D::Error>
where
D: $crate::serde::de::Deserializer<'de>,
D: serde::de::Deserializer<'de>,
{
use $crate::std::fmt::{self, Formatter};
use $crate::serde::de::IgnoredAny;
use std::fmt::{self, Formatter};
use serde::de::IgnoredAny;

#[allow(non_camel_case_types)]
enum Enum { Unknown__Field, $($fe),* }

struct EnumVisitor;
impl<'de> $crate::serde::de::Visitor<'de> for EnumVisitor {
impl<'de> serde::de::Visitor<'de> for EnumVisitor {
type Value = Enum;

fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
Expand All @@ -243,7 +243,7 @@ macro_rules! serde_struct_impl {

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: $crate::serde::de::Error,
E: serde::de::Error,
{
match v {
$(
Expand All @@ -254,7 +254,7 @@ macro_rules! serde_struct_impl {
}
}

impl<'de> $crate::serde::Deserialize<'de> for Enum {
impl<'de> serde::Deserialize<'de> for Enum {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: ::serde::de::Deserializer<'de>,
Expand All @@ -265,7 +265,7 @@ macro_rules! serde_struct_impl {

struct Visitor;

impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = $name;

fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
Expand All @@ -274,9 +274,9 @@ macro_rules! serde_struct_impl {

fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: $crate::serde::de::MapAccess<'de>,
A: serde::de::MapAccess<'de>,
{
use $crate::serde::de::Error;
use serde::de::Error;

$(let mut $fe = None;)*

Expand Down Expand Up @@ -317,12 +317,12 @@ macro_rules! serde_struct_impl {
}

#[cfg(feature = "serde")]
impl<'de> $crate::serde::Serialize for $name {
impl<'de> serde::Serialize for $name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: $crate::serde::Serializer,
S: serde::Serializer,
{
use $crate::serde::ser::SerializeStruct;
use serde::ser::SerializeStruct;

// Only used to get the struct length.
static FIELDS: &'static [&'static str] = &[$(stringify!($fe)),*];
Expand Down
1 change: 0 additions & 1 deletion stacks-common/src/deps_common/bitcoin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

// Clippy flags
#![allow(clippy::needless_range_loop)] // suggests making a big mess of array newtypes
#![allow(clippy::extend_from_slice)] // `extend_from_slice` only available since 1.6

// Coding conventions
#![deny(non_upper_case_globals)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//!

use std::hash::Hash;
use std::{mem, u32};
use std::mem;

use hashbrown::HashMap;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use crate::deps_common::bitcoin::network::constants;
use crate::util;

/// Some simple messages

jferrant marked this conversation as resolved.
Show resolved Hide resolved
/// The `version` message
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct VersionMessage {
Expand Down
8 changes: 4 additions & 4 deletions stacks-common/src/deps_common/bitcoin/network/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,19 @@ impl BitcoinHash for Vec<u8> {
}

/// Encode an object into a vector
pub fn serialize<T: ?Sized>(data: &T) -> Result<Vec<u8>, Error>
pub fn serialize<T>(data: &T) -> Result<Vec<u8>, Error>
where
T: ConsensusEncodable<RawEncoder<Cursor<Vec<u8>>>>,
T: ConsensusEncodable<RawEncoder<Cursor<Vec<u8>>>> + ?Sized,
{
let mut encoder = RawEncoder::new(Cursor::new(vec![]));
data.consensus_encode(&mut encoder)?;
Ok(encoder.into_inner().into_inner())
}

/// Encode an object into a hex-encoded string
pub fn serialize_hex<T: ?Sized>(data: &T) -> Result<String, Error>
pub fn serialize_hex<T>(data: &T) -> Result<String, Error>
where
T: ConsensusEncodable<RawEncoder<Cursor<Vec<u8>>>>,
T: ConsensusEncodable<RawEncoder<Cursor<Vec<u8>>>> + ?Sized,
{
let serial = serialize(data)?;
Ok(hex_encode(&serial[..]))
Expand Down
2 changes: 1 addition & 1 deletion stacks-common/src/deps_common/bitcoin/util/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ pub fn bitcoin_merkle_root(data: Vec<Sha256dHash>) -> Sha256dHash {
bitcoin_merkle_root(next)
}

impl<'a, T: BitcoinHash> MerkleRoot for &'a [T] {
impl<T: BitcoinHash> MerkleRoot for &[T] {
fn merkle_root(&self) -> Sha256dHash {
bitcoin_merkle_root(self.iter().map(|obj| obj.bitcoin_hash()).collect())
}
Expand Down
2 changes: 1 addition & 1 deletion stacks-common/src/deps_common/ctrlc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// notice may not be copied, modified, or distributed except
// according to those terms.

#[macro_use]
#![macro_use]

mod error;
mod platform;
Expand Down
5 changes: 3 additions & 2 deletions stacks-common/src/deps_common/httparse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ impl<'a> Bytes<'a> {
}
}

impl<'a> AsRef<[u8]> for Bytes<'a> {
impl AsRef<[u8]> for Bytes<'_> {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.slice_peek()[self.pos..]
}
}

impl<'a> Iterator for Bytes<'a> {
impl Iterator for Bytes<'_> {
type Item = u8;

#[inline]
Expand Down Expand Up @@ -701,6 +701,7 @@ pub fn parse_headers<'b: 'h, 'h>(
}

#[inline]
#[allow(clippy::never_loop)]
fn parse_headers_iter<'a>(headers: &mut &mut [Header<'a>], bytes: &mut Bytes<'a>) -> Result<usize> {
let mut num_headers: usize = 0;
let mut count: usize = 0;
Expand Down
7 changes: 4 additions & 3 deletions stacks-common/src/types/chainstate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::fmt::{self, Display};
use std::io::{Read, Write};
use std::str::FromStr;

Expand Down Expand Up @@ -48,7 +48,7 @@ impl TrieHash {

/// TrieHash from bytes
pub fn from_data(data: &[u8]) -> TrieHash {
if data.len() == 0 {
if data.is_empty() {
return TrieHash::from_empty_data();
}

Expand All @@ -62,7 +62,7 @@ impl TrieHash {
}

pub fn from_data_array<B: AsRef<[u8]>>(data: &[B]) -> TrieHash {
if data.len() == 0 {
if data.is_empty() {
return TrieHash::from_empty_data();
}

Expand All @@ -78,6 +78,7 @@ impl TrieHash {
}

/// Convert to a String that can be used in e.g. sqlite
#[allow(clippy::inherent_to_string_shadow_display)]
pub fn to_string(&self) -> String {
jferrant marked this conversation as resolved.
Show resolved Hide resolved
let s = format!("{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
self.0[0], self.0[1], self.0[2], self.0[3],
Expand Down
Loading
Loading