From 5252d8cf46be1bbb4301db18383b78c305ea1635 Mon Sep 17 00:00:00 2001 From: brock elmore Date: Fri, 8 Dec 2023 15:45:31 -0800 Subject: [PATCH] lint --- crates/analyzers/src/var_analyzer/mod.rs | 6 +-- .../src/var_analyzer/report_display.rs | 2 +- crates/graph/src/nodes/context/var/typing.rs | 7 ++- .../graph/src/nodes/context/var/underlying.rs | 4 +- crates/graph/src/nodes/func_ty.rs | 4 +- crates/graph/src/range/elem/expr.rs | 29 +++++++---- crates/shared/src/lib.rs | 51 ++++++++----------- crates/solc-expressions/src/array.rs | 2 +- .../src/context_builder/mod.rs | 4 +- crates/solc-expressions/src/env.rs | 1 - crates/solc-expressions/src/func_call/mod.rs | 2 +- .../solc-expressions/src/member_access/mod.rs | 2 +- 12 files changed, 60 insertions(+), 54 deletions(-) diff --git a/crates/analyzers/src/var_analyzer/mod.rs b/crates/analyzers/src/var_analyzer/mod.rs index 991f6c45..29d3609d 100644 --- a/crates/analyzers/src/var_analyzer/mod.rs +++ b/crates/analyzers/src/var_analyzer/mod.rs @@ -7,7 +7,7 @@ use graph::{ nodes::{ContextNode, ContextVarNode, KilledKind}, AnalyzerBackend, GraphBackend, Range, SolcRange, }; -use shared::{StorageLocation, Search}; +use shared::{Search, StorageLocation}; use std::collections::BTreeSet; @@ -98,7 +98,7 @@ impl VarBoundAnalysis { order: -1, name: self.var_display_name.clone(), loc: self.var_def.0.clone(), - storage: self.storage.clone(), + storage: self.storage, ctx: self.ctx, ctx_conditionals: self.conditionals(analyzer), parts, @@ -215,7 +215,7 @@ pub trait VarBoundAnalyzer: Search + AnalyzerBackend + Sized { ), bound_changes: vec![], report_config, - storage: curr.underlying(self).unwrap().storage.clone(), + storage: curr.underlying(self).unwrap().storage, ctx_killed: ctx .killed_loc(self) .unwrap() diff --git a/crates/analyzers/src/var_analyzer/report_display.rs b/crates/analyzers/src/var_analyzer/report_display.rs index e53b7e10..006a594d 100644 --- a/crates/analyzers/src/var_analyzer/report_display.rs +++ b/crates/analyzers/src/var_analyzer/report_display.rs @@ -41,7 +41,7 @@ impl ReportDisplay for VarBoundAnalysis { name: self.var_display_name.clone(), loc: bound_change.0.clone(), order: i as i32, - storage: self.storage.clone(), + storage: self.storage, ctx: self.ctx, ctx_conditionals: self.conditionals(analyzer), parts, diff --git a/crates/graph/src/nodes/context/var/typing.rs b/crates/graph/src/nodes/context/var/typing.rs index 0a3e91c5..44fc49b6 100644 --- a/crates/graph/src/nodes/context/var/typing.rs +++ b/crates/graph/src/nodes/context/var/typing.rs @@ -3,7 +3,7 @@ use crate::{ AnalyzerBackend, ContextEdge, Edge, GraphBackend, GraphError, Node, VarType, }; -use shared::{StorageLocation, Search}; +use shared::{Search, StorageLocation}; use petgraph::Direction; use solang_parser::pt::Loc; @@ -86,7 +86,10 @@ impl ContextVarNode { } pub fn is_controllable(&self, analyzer: &impl GraphBackend) -> Result { - if self.is_storage_or_calldata_input(analyzer)? || self.is_msg(analyzer)? || self.is_block(analyzer)? { + if self.is_storage_or_calldata_input(analyzer)? + || self.is_msg(analyzer)? + || self.is_block(analyzer)? + { Ok(true) } else if let Some(tmp) = self.tmp_of(analyzer)? { let rhs_controllable = if let Some(rhs) = tmp.rhs { diff --git a/crates/graph/src/nodes/context/var/underlying.rs b/crates/graph/src/nodes/context/var/underlying.rs index 4d9bd14f..d8cfda8e 100644 --- a/crates/graph/src/nodes/context/var/underlying.rs +++ b/crates/graph/src/nodes/context/var/underlying.rs @@ -8,7 +8,7 @@ use crate::{ }; use crate::range::elem::*; -use shared::{StorageLocation, NodeIdx}; +use shared::{NodeIdx, StorageLocation}; use solang_parser::pt::Loc; @@ -479,7 +479,7 @@ impl ContextVar { display_name: parent_var.name.clone() + "." + &field.name.expect("Field had no name").name, - storage: parent_var.storage.clone(), + storage: parent_var.storage, is_tmp: false, tmp_of: None, is_symbolic: true, diff --git a/crates/graph/src/nodes/func_ty.rs b/crates/graph/src/nodes/func_ty.rs index 51fc5821..eee61d52 100644 --- a/crates/graph/src/nodes/func_ty.rs +++ b/crates/graph/src/nodes/func_ty.rs @@ -785,7 +785,7 @@ impl FunctionParam { loc: param.loc, ty: analyzer.parse_expr(¶m.ty, None), order, - storage: if let Some(s) = param.storage { Some(s.into()) } else { None }, + storage: param.storage.map(|s| s.into()), name: param.name, } } @@ -875,7 +875,7 @@ impl FunctionReturn { FunctionReturn { loc: param.loc, ty: analyzer.parse_expr(¶m.ty, None), - storage: if let Some(s) = param.storage { Some(s.into()) } else { None }, + storage: param.storage.map(|s| s.into()), name: param.name, } } diff --git a/crates/graph/src/range/elem/expr.rs b/crates/graph/src/range/elem/expr.rs index d7e65947..e8c6aa1c 100644 --- a/crates/graph/src/range/elem/expr.rs +++ b/crates/graph/src/range/elem/expr.rs @@ -223,8 +223,13 @@ fn collapse(l: Elem, op: RangeOp, r: Elem) -> MaybeCollapsed (RangeOp::Sub(false), RangeOp::Min) => { // min{x - y, z} // if x <= z, then x - y will be the minimum if y >= 0 - if matches!(x.range_ord(&z), Some(std::cmp::Ordering::Equal) | Some(std::cmp::Ordering::Less)) - && matches!(y.range_ord(&zero), Some(std::cmp::Ordering::Equal) | Some(std::cmp::Ordering::Greater)) { + if matches!( + x.range_ord(&z), + Some(std::cmp::Ordering::Equal) | Some(std::cmp::Ordering::Less) + ) && matches!( + y.range_ord(&zero), + Some(std::cmp::Ordering::Equal) | Some(std::cmp::Ordering::Greater) + ) { MaybeCollapsed::Collapsed(l) } else { MaybeCollapsed::Not(l, r) @@ -234,13 +239,19 @@ fn collapse(l: Elem, op: RangeOp, r: Elem) -> MaybeCollapsed // max{x + y, z} // if x >= z, then x + y will be the maximum if y >= 0 // or if y >= z, then x + y will be the maximum if x >= 0 - if ( - matches!(x.range_ord(&z), Some(std::cmp::Ordering::Equal) | Some(std::cmp::Ordering::Greater)) - && matches!(y.range_ord(&zero), Some(std::cmp::Ordering::Equal) | Some(std::cmp::Ordering::Greater)) - ) || ( - matches!(y.range_ord(&z), Some(std::cmp::Ordering::Equal) | Some(std::cmp::Ordering::Greater)) - && matches!(x.range_ord(&zero), Some(std::cmp::Ordering::Equal) | Some(std::cmp::Ordering::Greater)) - ) { + if (matches!( + x.range_ord(&z), + Some(std::cmp::Ordering::Equal) | Some(std::cmp::Ordering::Greater) + ) && matches!( + y.range_ord(&zero), + Some(std::cmp::Ordering::Equal) | Some(std::cmp::Ordering::Greater) + )) || (matches!( + y.range_ord(&z), + Some(std::cmp::Ordering::Equal) | Some(std::cmp::Ordering::Greater) + ) && matches!( + x.range_ord(&zero), + Some(std::cmp::Ordering::Equal) | Some(std::cmp::Ordering::Greater) + )) { MaybeCollapsed::Collapsed(l) } else { MaybeCollapsed::Not(l, r) diff --git a/crates/shared/src/lib.rs b/crates/shared/src/lib.rs index 9e1538b0..bc09b01e 100644 --- a/crates/shared/src/lib.rs +++ b/crates/shared/src/lib.rs @@ -6,40 +6,33 @@ pub use analyzer_like::*; pub use graph_like::*; pub use search::*; - #[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)] pub enum StorageLocation { - Memory(solang_parser::pt::Loc), - Storage(solang_parser::pt::Loc), - Calldata(solang_parser::pt::Loc), - Block(solang_parser::pt::Loc), - Msg(solang_parser::pt::Loc), + Memory(solang_parser::pt::Loc), + Storage(solang_parser::pt::Loc), + Calldata(solang_parser::pt::Loc), + Block(solang_parser::pt::Loc), + Msg(solang_parser::pt::Loc), } impl std::fmt::Display for StorageLocation { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Memory(_) => write!(f, "memory"), - Self::Storage(_) => write!(f, "storage"), - Self::Calldata(_) => write!(f, "calldata"), - Self::Block(_) => write!(f, "block"), - Self::Msg(_) => write!(f, "msg"), - } - } + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Memory(_) => write!(f, "memory"), + Self::Storage(_) => write!(f, "storage"), + Self::Calldata(_) => write!(f, "calldata"), + Self::Block(_) => write!(f, "block"), + Self::Msg(_) => write!(f, "msg"), + } + } } impl From for StorageLocation { - fn from(sl: solang_parser::pt::StorageLocation) -> Self { - match sl { - solang_parser::pt::StorageLocation::Memory(m) =>{ - StorageLocation::Memory(m) - } - solang_parser::pt::StorageLocation::Storage(m) =>{ - StorageLocation::Storage(m) - } - solang_parser::pt::StorageLocation::Calldata(m) =>{ - StorageLocation::Calldata(m) - } - } - } -} \ No newline at end of file + fn from(sl: solang_parser::pt::StorageLocation) -> Self { + match sl { + solang_parser::pt::StorageLocation::Memory(m) => StorageLocation::Memory(m), + solang_parser::pt::StorageLocation::Storage(m) => StorageLocation::Storage(m), + solang_parser::pt::StorageLocation::Calldata(m) => StorageLocation::Calldata(m), + } + } +} diff --git a/crates/solc-expressions/src/array.rs b/crates/solc-expressions/src/array.rs index a870b1e5..3cead0f6 100644 --- a/crates/solc-expressions/src/array.rs +++ b/crates/solc-expressions/src/array.rs @@ -165,7 +165,7 @@ pub trait Array: AnalyzerBackend + Sized { parent.display_name(self).into_expr_err(loc)?, index.display_name(self).into_expr_err(loc)? ), - storage: parent.storage(self).into_expr_err(loc)?.clone(), + storage: *parent.storage(self).into_expr_err(loc)?, is_tmp: false, tmp_of: None, is_symbolic: true, diff --git a/crates/solc-expressions/src/context_builder/mod.rs b/crates/solc-expressions/src/context_builder/mod.rs index 7d96f5b6..9ce07c73 100644 --- a/crates/solc-expressions/src/context_builder/mod.rs +++ b/crates/solc-expressions/src/context_builder/mod.rs @@ -615,7 +615,7 @@ pub trait ContextBuilder: loc: Some(loc), name: name.to_string(), display_name: name.to_string(), - storage: if let Some(s) = &var_decl.storage { Some(s.clone().into()) } else { None }, + storage: var_decl.storage.as_ref().map(|s| s.clone().into()), is_tmp: false, is_symbolic: true, tmp_of: None, @@ -642,7 +642,7 @@ pub trait ContextBuilder: loc: Some(loc), name: name.to_string(), display_name: name.to_string(), - storage: if let Some(s) = &var_decl.storage { Some(s.clone().into()) } else { None }, + storage: var_decl.storage.as_ref().map(|s| s.clone().into()), is_tmp: false, is_symbolic: true, tmp_of: None, diff --git a/crates/solc-expressions/src/env.rs b/crates/solc-expressions/src/env.rs index 7dca8db2..e203455e 100644 --- a/crates/solc-expressions/src/env.rs +++ b/crates/solc-expressions/src/env.rs @@ -1,4 +1,3 @@ - use crate::{func_call::FuncCaller, ExprErr, IntoExprErr}; use graph::{ diff --git a/crates/solc-expressions/src/func_call/mod.rs b/crates/solc-expressions/src/func_call/mod.rs index a1dce7d3..33c82738 100644 --- a/crates/solc-expressions/src/func_call/mod.rs +++ b/crates/solc-expressions/src/func_call/mod.rs @@ -10,7 +10,7 @@ use graph::{ }, AnalyzerBackend, ContextEdge, Edge, GraphBackend, Node, Range, VarType, }; -use shared::{StorageLocation, NodeIdx}; +use shared::{NodeIdx, StorageLocation}; use solang_parser::helpers::CodeLocation; use solang_parser::pt::{Expression, Loc, NamedArgument}; diff --git a/crates/solc-expressions/src/member_access/mod.rs b/crates/solc-expressions/src/member_access/mod.rs index 798f3a72..80abecc6 100644 --- a/crates/solc-expressions/src/member_access/mod.rs +++ b/crates/solc-expressions/src/member_access/mod.rs @@ -851,7 +851,7 @@ pub trait MemberAccess: AnalyzerBackend + loc, parent_name, parent_display_name, - parent_stor.clone(), + *parent_stor, &parent_ty, ContextVarNode::from(*idx), )