Skip to content

Commit dec175a

Browse files
committed
Chore: Run Clippy
1 parent ef03822 commit dec175a

File tree

5 files changed

+37
-30
lines changed

5 files changed

+37
-30
lines changed

src/analyzer.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ pub struct StackAnalyzer {
3131
last_constant: Option<i64>,
3232
}
3333

34+
impl Default for StackAnalyzer {
35+
fn default() -> Self {
36+
Self::new()
37+
}
38+
}
39+
3440
impl StackAnalyzer {
3541
pub fn new() -> Self {
3642
StackAnalyzer {
@@ -60,7 +66,7 @@ impl StackAnalyzer {
6066
self.handle_sub_script(called_script.get_stack());
6167
}
6268
Block::Script(block_script) => {
63-
for instruct in block_script.instructions().into_iter() {
69+
for instruct in block_script.instructions() {
6470
match instruct {
6571
Err(err) => {
6672
panic!("instruction extract fail from script {}", err);
@@ -82,16 +88,13 @@ impl StackAnalyzer {
8288
}
8389

8490
pub fn handle_push_slice(&mut self, bytes: &PushBytes) {
85-
match read_scriptint(bytes.as_bytes()) {
86-
Ok(x) => {
87-
// if i64(data) < 1000, last_constant is true
88-
if x <= 1000 && x >= 0 {
89-
self.last_constant = Some(x);
90-
} else {
91-
self.last_constant = None;
92-
}
91+
if let Ok(x) = read_scriptint(bytes.as_bytes()) {
92+
// if i64(data) < 1000, last_constant is true
93+
if (0..=1000).contains(&x) {
94+
self.last_constant = Some(x);
95+
} else {
96+
self.last_constant = None;
9397
}
94-
Err(_) => {}
9598
}
9699
self.stack_change(Self::plain_stack_status(0, 1));
97100
}
@@ -161,15 +164,15 @@ impl StackAnalyzer {
161164
},
162165
OP_PICK => match self.last_constant {
163166
Some(x) => {
164-
self.stack_change(Self::plain_stack_status(-1 * (x + 1 + 1) as i32, 0));
167+
self.stack_change(Self::plain_stack_status(-((x + 1 + 1) as i32), 0));
165168
}
166169
None => {
167170
panic!("need to be handled manually for op_pick")
168171
}
169172
},
170173
OP_ROLL => match self.last_constant {
171174
Some(x) => {
172-
self.stack_change(Self::plain_stack_status(-1 * (x + 1 + 1) as i32, -1));
175+
self.stack_change(Self::plain_stack_status(-((x + 1 + 1) as i32), -1));
173176
// for [x2, x1, x0, 2, OP_PICK]
174177
}
175178
None => {
@@ -220,16 +223,16 @@ impl StackAnalyzer {
220223
let y = status.altstack_changed.borrow_mut();
221224

222225
*i = min(*i, (*j) + stack_status.deepest_stack_accessed);
223-
*j = *j + stack_status.stack_changed;
226+
*j += stack_status.stack_changed;
224227

225228
*x = min(*x, (*y) + stack_status.deepest_altstack_accessed);
226-
*y = *y + stack_status.altstack_changed;
229+
*y += stack_status.altstack_changed;
227230
}
228231

229232
/// the first return is deepest access to current stack
230233
/// the second return is the impact for the stack
231234
fn opcode_stack_table(data: &Opcode) -> StackStatus {
232-
let (i, j) = match data.clone() {
235+
let (i, j) = match *data {
233236
OP_PUSHBYTES_0 | OP_PUSHBYTES_1 | OP_PUSHBYTES_2 | OP_PUSHBYTES_3 | OP_PUSHBYTES_4
234237
| OP_PUSHBYTES_5 | OP_PUSHBYTES_6 | OP_PUSHBYTES_7 | OP_PUSHBYTES_8
235238
| OP_PUSHBYTES_9 | OP_PUSHBYTES_10 | OP_PUSHBYTES_11 | OP_PUSHBYTES_12
@@ -314,7 +317,7 @@ impl StackAnalyzer {
314317
}
315318
};
316319

317-
let (x, y) = match data.clone() {
320+
let (x, y) = match *data {
318321
OP_FROMALTSTACK => (-1, -1),
319322
OP_TOALTSTACK => (0, 1),
320323
_ => (0, 0),

src/builder.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,7 @@ impl StructuredScript {
230230
let id = calculate_hash(&data);
231231
self.blocks.push(Block::Call(id));
232232
// Register script
233-
if !self.script_map.contains_key(&id) {
234-
self.script_map.insert(id, data);
235-
}
233+
self.script_map.entry(id).or_insert(data);
236234
self
237235
}
238236

@@ -400,8 +398,8 @@ impl StructuredScript {
400398
}
401399

402400
pub fn push_expression<T: Pushable>(self, expression: T) -> StructuredScript {
403-
let builder = expression.bitcoin_script_push(self);
404-
builder
401+
402+
expression.bitcoin_script_push(self)
405403
}
406404
}
407405

src/chunker.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ pub struct UndoInfo {
2727
num_unclosed_ifs: i32,
2828
}
2929

30+
impl Default for UndoInfo {
31+
fn default() -> Self {
32+
Self::new()
33+
}
34+
}
35+
3036
impl UndoInfo {
3137
pub fn new() -> Self {
3238
Self {
@@ -105,9 +111,9 @@ impl Chunker {
105111
for chunk in chunks.iter_mut() {
106112
let status = self.stack_analyze(&mut chunk.scripts);
107113
// ((-1 * access) as u32, (depth - access) as u32)
108-
let stack_input_size = status.deepest_stack_accessed.abs() as usize;
114+
let stack_input_size = status.deepest_stack_accessed.unsigned_abs() as usize;
109115
let stack_output_size = (status.stack_changed - status.deepest_stack_accessed) as usize;
110-
let altstack_input_size = status.deepest_altstack_accessed.abs() as usize;
116+
let altstack_input_size = status.deepest_altstack_accessed.unsigned_abs() as usize;
111117
let altstack_output_size =
112118
(status.altstack_changed - status.deepest_altstack_accessed) as usize;
113119
chunk.stats = Some(ChunkStats {
@@ -253,7 +259,7 @@ impl Chunker {
253259
for block in builder.blocks.iter().rev() {
254260
match block {
255261
Block::Call(id) => {
256-
let sub_builder = builder.script_map.get(&id).unwrap();
262+
let sub_builder = builder.script_map.get(id).unwrap();
257263
self.call_stack.push(Box::new(sub_builder.clone())); //TODO: Avoid cloning here by
258264
//putting Box<Builder> into
259265
//the script_map

tests/test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bitcoin::opcodes::all::{OP_ADD, OP_ENDIF};
1+
use bitcoin::opcodes::all::OP_ADD;
22
use bitcoin_script::{script, Script};
33

44
#[test]
@@ -383,15 +383,15 @@ fn test_is_script_buf() {
383383
OP_IF
384384
OP_ENDIF
385385
};
386-
assert_eq!(script.is_script_buf(), true);
387-
assert_eq!(script.contains_flow_op(), true);
386+
assert!(script.is_script_buf());
387+
assert!(script.contains_flow_op());
388388
}
389389

390390
#[test]
391391
fn test_is_script_buf_false() {
392392
let script = script! {
393393
{ script! {OP_ADD} }
394394
};
395-
assert_eq!(script.is_script_buf(), false);
396-
assert_eq!(script.contains_flow_op(), false);
395+
assert!(!script.is_script_buf());
396+
assert!(!script.contains_flow_op());
397397
}

tests/test_chunker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bitcoin::{opcodes::all::OP_OVER, ScriptBuf};
1+
use bitcoin::ScriptBuf;
22
use bitcoin_script::{script, Chunker};
33

44
#[test]

0 commit comments

Comments
 (0)