Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Update for clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed Sep 13, 2022
2 parents 1d9f9cc + 8d534d8 commit 73aed47
Show file tree
Hide file tree
Showing 64 changed files with 2,098 additions and 2,708 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ jobs:
toolchain: stable
components: clippy
override: true
- name: clippy
run: cargo clippy --all -- -D warnings
- uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
7 changes: 2 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 1 addition & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ binascii = "0.1.4"
yaml-rust = "0.4"
linked-hash-map = "0.5.6"

[patch.crates-io]
skimmer = { git = "https://github.com/dnsl48/skimmer", rev = "ca914ef624ecf39a75ed7afef10e7838fffe9127" }

[target.x86_64-apple-darwin]
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]

[lib]
name = "clvm_tools_rs"
crate-type = ["cdylib", "rlib"]
Expand All @@ -52,7 +43,7 @@ extension-module = ["pyo3"]
default = ["extension-module"]

[target.'cfg(target_family="wasm")'.dependencies]
wasm-bindgen = "0.2.80"
wasm-bindgen = { version = "0.2.80", features = ["serde-serialize"] }
wasm-bindgen-test = "0.3.25"
js-sys = "0.3.58"
getrandom = { version = "0.2", features = ["js"] }
Expand Down
6 changes: 3 additions & 3 deletions src/classic/bins/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use clvm_tools_rs::classic::clvm_tools::stages::stage_0::DefaultProgramRunner;
fn main() {
let mut allocator = Allocator::new();
let runner = Rc::new(DefaultProgramRunner::new());
let opts = Rc::new(DefaultCompilerOpts::new(&"*program*".to_string()));
let opts = Rc::new(DefaultCompilerOpts::new("*program*"));
let stdin = io::stdin();
let mut repl = Repl::new(opts.clone(), runner.clone());
let mut repl = Repl::new(opts, runner);

print!(">>> ");
io::stdout().flush().unwrap();
Expand All @@ -29,7 +29,7 @@ fn main() {
.process_line(&mut allocator, line)
.map(|result| {
if let Some(result) = result {
print!("{}\n>>> ", result.to_sexp().to_string());
print!("{}\n>>> ", result.to_sexp());
} else {
print!("... ");
}
Expand Down
22 changes: 9 additions & 13 deletions src/classic/bins/shrink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,29 @@ use clvm_tools_rs::classic::clvm_tools::stages::stage_0::DefaultProgramRunner;
fn main() {
let mut allocator = Allocator::new();
let runner = Rc::new(DefaultProgramRunner::new());
let opts = Rc::new(DefaultCompilerOpts::new(&"*program*".to_string()));
let opts = Rc::new(DefaultCompilerOpts::new("*program*"));
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
println!("give a chialisp program to minify");
return;
}

let loc = Srcloc::start(&"*program*".to_string());
let _ = parse_sexp(loc.clone(), &args[1])
.map_err(|e| {
return CompileErr(e.0.clone(), e.1.clone());
})
.and_then(|parsed_program| {
return frontend(opts.clone(), parsed_program);
})
let loc = Srcloc::start("*program*");
let _ = parse_sexp(loc, &args[1])
.map_err(|e| CompileErr(e.0.clone(), e.1))
.and_then(|parsed_program| frontend(opts.clone(), parsed_program))
.and_then(|program| {
let e = Evaluator::new(opts.clone(), runner.clone(), program.helpers.clone());
return e.shrink_bodyform(
e.shrink_bodyform(
&mut allocator,
program.args.clone(),
&HashMap::new(),
program.exp.clone(),
program.exp,
false,
);
)
})
.map(|result| {
println!("shrunk: {}", result.to_sexp().to_string());
println!("shrunk: {}", result.to_sexp());
})
.map_err(|e| {
println!("failed: {:?}", e);
Expand Down
106 changes: 50 additions & 56 deletions src/classic/clvm/__type_compatibility__.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::clone::Clone;
use std::cmp::Ordering;
use std::cmp::{max, min};
use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::{Debug, Display};
use std::option::Option;
use std::string::String;

Expand All @@ -14,7 +14,7 @@ use sha2::Sha256;

use crate::util::Number;

pub fn to_hexstr(r: &Vec<u8>) -> String {
pub fn to_hexstr(r: &[u8]) -> String {
hex::encode(r)
}

Expand All @@ -25,7 +25,7 @@ pub fn char_to_string(ch: char) -> String {
}
}

pub fn vec_to_string(r: &Vec<u8>) -> String {
pub fn vec_to_string(r: &[u8]) -> String {
return String::from_utf8_lossy(r).as_ref().to_string();
}

Expand All @@ -34,12 +34,11 @@ pub fn vec_to_string(r: &Vec<u8>) -> String {
* @see https://github.com/python/cpython/blob/main/Objects/bytesobject.c#L1337
* @param {Uint8Array} r - byteArray to stringify
*/
pub fn PyBytes_Repr(r: &Vec<u8>, dquoted: bool) -> String {
pub fn pybytes_repr(r: &[u8], dquoted: bool) -> String {
let mut squotes = 0;
let mut dquotes = 0;
for i in 0..r.len() {
let b = r[i];
let c = b as char;
for b in r.iter() {
let c = *b as char;
match c {
'\'' => squotes += 1,
'\"' => dquotes += 1,
Expand All @@ -54,14 +53,13 @@ pub fn PyBytes_Repr(r: &Vec<u8>, dquoted: bool) -> String {
let mut s = "".to_string();

if !dquoted {
s = s + "b";
s += "b";
}

s = s + char_to_string(quote).as_str();
s += char_to_string(quote).as_str();

for i in 0..r.len() {
let b = r[i];
let c = b as char;
for b in r.iter() {
let c = *b as char;
if c == quote || c == '\\' {
s = (s + "\\") + char_to_string(c).as_str();
} else if c == '\t' {
Expand All @@ -70,9 +68,9 @@ pub fn PyBytes_Repr(r: &Vec<u8>, dquoted: bool) -> String {
s += "\\n";
} else if c == '\r' {
s += "\\r";
} else if c < ' ' || b >= 0x7f {
} else if c < ' ' || *b >= 0x7f {
s += "\\x";
s += hex::encode(vec![b]).as_str();
s += hex::encode(vec![*b]).as_str();
} else {
s += char_to_string(c).as_str();
}
Expand Down Expand Up @@ -120,6 +118,7 @@ impl Bytes {
Bytes::new(Some(BytesFromType::Raw(bvec)))
}
Some(BytesFromType::Hex(hstr)) => {
#[allow(clippy::single_char_pattern)]
let hex_stripped = hstr
.replace(" ", "")
.replace("\t", "")
Expand Down Expand Up @@ -149,16 +148,6 @@ impl Bytes {
self._b.clone()
}

pub fn repeat(&self, n: usize) -> Bytes {
let capacity = self.length() * n;
let set_size = self._b.len();
let mut ret = Vec::<u8>::with_capacity(capacity);
for i in 0..capacity - 1 {
ret[i] = self._b[i % set_size];
}
Bytes::new(Some(BytesFromType::Raw(ret)))
}

pub fn concat(&self, b: &Bytes) -> Bytes {
let mut this_bin = self._b.clone();
let mut that_bin = b.raw();
Expand Down Expand Up @@ -194,16 +183,8 @@ impl Bytes {
&self._b
}

pub fn clone(&self) -> Self {
Bytes::new(Some(BytesFromType::Raw(self._b.clone())))
}

pub fn to_string(&self) -> String {
PyBytes_Repr(&self._b, false)
}

pub fn to_formal_string(&self) -> String {
PyBytes_Repr(&self._b, true)
pybytes_repr(&self._b, true)
}

pub fn hex(&self) -> String {
Expand Down Expand Up @@ -260,10 +241,14 @@ impl Bytes {

for i in 0..slen - 1 {
let diff: i32 = other.at(i) as i32 - self._b[i] as i32;
if diff < 0 {
return Ordering::Less;
} else if diff > 0 {
return Ordering::Greater;
match (diff < 0, diff > 0) {
(true, _) => {
return Ordering::Less;
}
(_, true) => {
return Ordering::Greater;
}
_ => {}
}
}
if self._b.len() < slen {
Expand All @@ -275,6 +260,13 @@ impl Bytes {
}
}

impl Display for Bytes {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
fmt.write_str(&pybytes_repr(&self._b, false))?;
Ok(())
}
}

pub fn sha256(value: Bytes) -> Bytes {
let hashed = Sha256::digest(&value.data()[..]);
let hashed_iter = hashed.into_iter();
Expand All @@ -287,7 +279,7 @@ where
I: Iterator<Item = E>,
E: Clone,
{
vals.map(|v| v.clone()).collect()
vals.collect()
}

pub trait PythonStr {
Expand Down Expand Up @@ -329,17 +321,21 @@ impl<T1, T2> Tuple<T1, T2> {
Tuple::Tuple(_, r) => r,
}
}
}

impl<T1, T2> Display for Tuple<T1, T2>
where
T1: PythonStr,
T2: PythonStr,
{
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
fmt.write_str("(")?;
fmt.write_str(self.first().py_str().as_str())?;
fmt.write_str(", ")?;
fmt.write_str(self.rest().py_str().as_str())?;
fmt.write_str(")")?;

pub fn to_string(&self) -> String
where
T1: PythonStr,
T2: PythonStr,
{
return "(".to_owned()
+ self.first().py_str().as_str()
+ ", "
+ self.rest().py_str().as_str()
+ ")";
Ok(())
}
}

Expand Down Expand Up @@ -378,13 +374,11 @@ impl Stream {
},
Some(b) => {
let data = b.data().to_vec();
let stream = Stream {
Stream {
seek: 0,
length: data.len(),
buffer: data,
};

stream
}
}
}
}
Expand Down Expand Up @@ -448,7 +442,7 @@ impl Stream {
b.length()
}

pub fn write_string(&mut self, s: String) -> usize {
pub fn write_str(&mut self, s: &str) -> usize {
self.write(Bytes::new(Some(BytesFromType::Raw(s.as_bytes().to_vec()))))
}

Expand Down Expand Up @@ -483,19 +477,19 @@ pub fn bi_one() -> Number {
One::one()
}

pub fn get_u32(v: &Vec<u8>, n: usize) -> u32 {
pub fn get_u32(v: &[u8], n: usize) -> u32 {
let p1 = v[n] as u32;
let p2 = v[n + 1] as u32;
let p3 = v[n + 2] as u32;
let p4 = v[n + 3] as u32;
p1 | (p2 << 8) | (p3 << 16) | (p4 << 24)
}

pub fn set_u8(vec: &mut Vec<u8>, n: usize, v: u8) {
pub fn set_u8(vec: &mut [u8], n: usize, v: u8) {
vec[n] = v;
}

pub fn set_u32(vec: &mut Vec<u8>, n: usize, v: u32) {
pub fn set_u32(vec: &mut [u8], n: usize, v: u32) {
vec[n] = ((v >> 24) & 0xff) as u8;
vec[n + 1] = ((v >> 16) & 0xff) as u8;
vec[n + 2] = ((v >> 8) & 0xff) as u8;
Expand Down
Loading

0 comments on commit 73aed47

Please sign in to comment.