Skip to content

Commit

Permalink
Fix rust clippies and evm balance length (hyperledger-solang#1603)
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Young <[email protected]>
  • Loading branch information
seanyoung authored Nov 29, 2023
1 parent 90b3f70 commit f401069
Show file tree
Hide file tree
Showing 19 changed files with 44 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/bin/languageserver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,7 +1644,7 @@ impl<'a> Builder<'a> {
}

for (i, constant) in self.ns.constants.iter().enumerate() {
let samptb = symtable::Symtable::new();
let samptb = symtable::Symtable::default();
self.contract_variable(constant, &samptb, None, i);
}

Expand Down Expand Up @@ -1676,7 +1676,7 @@ impl<'a> Builder<'a> {
}

for (i, variable) in contract.variables.iter().enumerate() {
let symtable = symtable::Symtable::new();
let symtable = symtable::Symtable::default();
self.contract_variable(variable, &symtable, Some(ci), i);
}

Expand Down
4 changes: 2 additions & 2 deletions src/codegen/revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ pub(super) fn revert(
.collect::<Vec<_>>();

if opt.log_runtime_errors {
match (error_no, exprs.get(0)) {
match (error_no, exprs.first()) {
// In the case of Error(string), we can print the reason
(None, Some(expr)) => {
let prefix = b"runtime_error: ";
Expand Down Expand Up @@ -369,7 +369,7 @@ pub(super) fn revert(
}
}

let error = match (*error_no, exprs.get(0)) {
let error = match (*error_no, exprs.first()) {
// Having an error number requires a custom error
(Some(error_no), _) => SolidityError::Custom { error_no, exprs },
// No error number but an expression requires Error(String)
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/statements/try_catch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ fn exec_try(
call_args,
..
} => {
let address_res = match try_stmt.returns.get(0) {
let address_res = match try_stmt.returns.first() {
Some((Some(pos), _)) => *pos,
_ => vartab.temp_anonymous(&Type::Contract(*contract_no)),
};
Expand Down
2 changes: 1 addition & 1 deletion src/emit/polkadot/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ impl StorageSlot for PolkadotTarget {
}
}
Type::Struct(str_ty) => {
for (_, field) in str_ty.definition(ns).fields.iter().enumerate() {
for field in &str_ty.definition(ns).fields {
self.storage_delete_slot(bin, &field.ty, slot, slot_ptr, function, ns);

if !field.ty.is_reference_type(ns)
Expand Down
2 changes: 1 addition & 1 deletion src/sema/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ impl Function {
has_body: false,
is_override: None,
body: Vec::new(),
symtable: Symtable::new(),
symtable: Symtable::default(),
emits_events: Vec::new(),
mangled_name,
annotations: ConstructorAnnotations::default(),
Expand Down
2 changes: 1 addition & 1 deletion src/sema/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ fn resolve_base_args(contracts: &[ContractDefinition], file_no: usize, ns: &mut
.position(|e| e.contract_no == base_no)
{
if let Some(args) = &base.args {
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();

// find constructor which matches this
if let Ok((Some(constructor_no), args)) = match_constructor_to_args(
Expand Down
2 changes: 1 addition & 1 deletion src/sema/expression/literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ fn check_subarrays<'a>(
flatten: &mut Vec<&'a pt::Expression>,
diagnostics: &mut Diagnostics,
) -> Result<(), ()> {
if let Some(pt::Expression::ArrayLiteral(_, first)) = exprs.get(0) {
if let Some(pt::Expression::ArrayLiteral(_, first)) = exprs.first() {
// ensure all elements are array literals of the same length
check_subarrays(first, dims, flatten, diagnostics)?;

Expand Down
2 changes: 1 addition & 1 deletion src/sema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn sema_file(file: &ResolvedFile, resolver: &mut FileResolver, ns: &mut ast::Nam
&item.doccomments,
None,
ns,
&mut Symtable::new(),
&mut Symtable::default(),
);
}
_ => (),
Expand Down
14 changes: 7 additions & 7 deletions src/sema/mutability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,13 @@ fn check_mutability(func: &Function, ns: &Namespace) -> Diagnostics {
SolanaAccount {
loc: Loc::Codegen,
is_writer: true,
/// With a @payer annotation, the account is created on-chain and needs a signer. The client
/// provides an address that does not exist yet, so SystemProgram.CreateAccount is called
/// on-chain.
///
/// However, if a @seed is also provided, the program can sign for the account
/// with the seed using program derived address (pda) when SystemProgram.CreateAccount is called,
/// so no signer is required from the client.
// With a @payer annotation, the account is created on-chain and needs a signer. The client
// provides an address that does not exist yet, so SystemProgram.CreateAccount is called
// on-chain.
//
// However, if a @seed is also provided, the program can sign for the account
// with the seed using program derived address (pda) when SystemProgram.CreateAccount is called,
// so no signer is required from the client.
is_signer: func.has_payer_annotation() && !func.has_seed_annotation(),
generated: true,
},
Expand Down
6 changes: 3 additions & 3 deletions src/sema/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Namespace {
/// Create a namespace and populate with the parameters for the target
pub fn new(target: Target) -> Self {
let (address_length, value_length) = match target {
Target::EVM => (20, 16),
Target::EVM => (20, 32),
Target::Polkadot {
address_length,
value_length,
Expand Down Expand Up @@ -1310,7 +1310,7 @@ impl Namespace {
}
}

if let Some(contract_name) = namespace.get(0) {
if let Some(contract_name) = namespace.first() {
contract_no = match self
.variable_symbols
.get(&(import_file_no, None, contract_name.name.clone()))
Expand Down Expand Up @@ -1524,7 +1524,7 @@ impl Namespace {
expr: &pt::Expression,
diagnostics: &mut Diagnostics,
) -> Result<ArrayDimension, ()> {
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
let mut context = ExprContext {
file_no,
unchecked: true,
Expand Down
2 changes: 1 addition & 1 deletion src/sema/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn resolve_function_body(
function_no: usize,
ns: &mut Namespace,
) -> Result<(), ()> {
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
let mut res = Vec::new();
let mut context = ExprContext {
file_no,
Expand Down
9 changes: 0 additions & 9 deletions src/sema/symtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,6 @@ pub struct Symtable {
}

impl Symtable {
pub fn new() -> Self {
Symtable {
vars: IndexMap::new(),
arguments: Vec::new(),
returns: Vec::new(),
scopes: Vec::new(),
}
}

pub fn add(
&mut self,
id: &pt::Identifier,
Expand Down
4 changes: 2 additions & 2 deletions src/sema/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,15 +640,15 @@ fn get_import_path() {

let ns = parse_and_resolve(OsStr::new("example.sol"), &mut cache, Target::EVM);

let file = ns.files.get(0);
let file = ns.files.first();
assert!(file.is_some());
if let Some(file) = file {
let import_path = cache.get_import_path(file.import_no.unwrap());
assert_eq!(Some(&(None, examples.clone())), import_path);
}

let ns = parse_and_resolve(OsStr::new("incrementer.sol"), &mut cache, Target::EVM);
let file = ns.files.get(0);
let file = ns.files.first();
assert!(file.is_some());
if let Some(file) = file {
let import_path = cache.get_import_path(file.import_no.unwrap());
Expand Down
6 changes: 3 additions & 3 deletions src/sema/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn contract_variables<'a>(
file_no: usize,
ns: &mut Namespace,
) -> Vec<DelayedResolveInitializer<'a>> {
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
let mut delayed = Vec::new();

for part in &def.parts {
Expand Down Expand Up @@ -447,7 +447,7 @@ pub fn variable_decl<'a>(

// If the variable is an array or mapping, the accessor function takes mapping keys
// or array indices as arguments, and returns the dereferenced value
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
let mut context = ExprContext::default();
context.enter_scope();
let mut params = Vec::new();
Expand Down Expand Up @@ -784,7 +784,7 @@ pub fn resolve_initializers(
file_no: usize,
ns: &mut Namespace,
) {
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
let mut diagnostics = Diagnostics::default();

for DelayedResolveInitializer {
Expand Down
2 changes: 1 addition & 1 deletion src/sema/yul/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ pub(crate) fn resolve_function_definition(
context: &mut ExprContext,
ns: &mut Namespace,
) -> Result<YulFunction, ()> {
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
context.enter_scope();

let prev_yul_function = context.yul_function;
Expand Down
22 changes: 11 additions & 11 deletions src/sema/yul/tests/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::sync::Arc;
fn resolve_bool_literal() {
let mut ctx = ExprContext::default();
ctx.enter_scope();
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
let mut function_table = FunctionsTable::new(0);

let mut ns = Namespace::new(Target::Solana);
Expand Down Expand Up @@ -66,7 +66,7 @@ fn resolve_bool_literal() {
fn resolve_number_literal() {
let mut ctx = ExprContext::default();
ctx.enter_scope();
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
let mut function_table = FunctionsTable::new(0);

let loc = Loc::File(0, 3, 5);
Expand Down Expand Up @@ -124,7 +124,7 @@ fn resolve_number_literal() {
fn resolve_hex_number_literal() {
let mut ctx = ExprContext::default();
ctx.enter_scope();
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
let mut function_table = FunctionsTable::new(0);

let mut ns = Namespace::new(Target::EVM);
Expand Down Expand Up @@ -170,7 +170,7 @@ fn resolve_hex_number_literal() {
fn resolve_hex_string_literal() {
let mut ctx = ExprContext::default();
ctx.enter_scope();
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
let mut function_table = FunctionsTable::new(0);

let mut ns = Namespace::new(Target::EVM);
Expand Down Expand Up @@ -237,7 +237,7 @@ fn resolve_hex_string_literal() {
fn resolve_string_literal() {
let mut ctx = ExprContext::default();
ctx.enter_scope();
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
let mut function_table = FunctionsTable::new(0);

let mut ns = Namespace::new(Target::Solana);
Expand Down Expand Up @@ -268,7 +268,7 @@ fn resolve_string_literal() {
fn resolve_variable_local() {
let mut context = ExprContext::default();
context.enter_scope();
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
let mut function_table = FunctionsTable::new(0);
let mut ns = Namespace::new(Target::EVM);
let loc = Loc::File(1, 2, 3);
Expand Down Expand Up @@ -346,7 +346,7 @@ fn resolve_variable_contract() {
};
context.enter_scope();

let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
let mut function_table = FunctionsTable::new(0);
let mut ns = Namespace::new(Target::EVM);
let loc = Loc::File(0, 2, 3);
Expand Down Expand Up @@ -543,7 +543,7 @@ fn resolve_variable_contract() {
fn function_call() {
let mut context = ExprContext::default();
context.enter_scope();
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
let mut function_table = FunctionsTable::new(0);
function_table.enter_scope();
let mut ns = Namespace::new(Target::EVM);
Expand Down Expand Up @@ -739,7 +739,7 @@ fn function_call() {
fn check_arguments() {
let mut context = ExprContext::default();
context.enter_scope();
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
let mut function_table = FunctionsTable::new(0);
function_table.enter_scope();
let mut ns = Namespace::new(Target::EVM);
Expand Down Expand Up @@ -896,7 +896,7 @@ fn test_member_access() {
};
context.enter_scope();

let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
let mut function_table = FunctionsTable::new(0);
let mut ns = Namespace::new(Target::EVM);
let loc = Loc::File(0, 2, 3);
Expand Down Expand Up @@ -1045,7 +1045,7 @@ fn test_check_types() {
read: false,
});
ns.contracts.push(contract);
let mut symtable = Symtable::new();
let mut symtable = Symtable::default();
symtable.add(
&Identifier {
loc,
Expand Down
6 changes: 3 additions & 3 deletions tests/codegen_testcases/yul/evm_builtin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ contract Testing {
// CHECK: (sext uint256 uint160((load (builtin GetAddress ()))))
let b := address()

// CHECK: ty:uint256 %c = (sext uint256 (builtin Balance (address((trunc uint160 (arg #0))))))
// CHECK: ty:uint256 %c = uint256((builtin Balance (address((trunc uint160 (arg #0))))))
let c := balance(arg1)

// CHECK: ty:uint256 %d = (sext uint256 (builtin Balance ((load (builtin GetAddress ())))))
// CHECK: ty:uint256 %d = uint256((builtin Balance ((load (builtin GetAddress ())))))
let d := selfbalance()

// CHECK: ty:uint256 %e = (sext uint256 uint160((builtin Sender ())))
let e := caller()

// CHECK: ty:uint256 %f = (sext uint256 (builtin Value ()))
// CHECK: ty:uint256 %f = uint256((builtin Value ()))
let f := callvalue()

// CHECK: ty:uint256 %g = (zext uint256 (builtin Gasprice ()))
Expand Down
4 changes: 1 addition & 3 deletions tests/contract_testcases/evm/comment_tests.sol
Original file line number Diff line number Diff line change
Expand Up @@ -677,11 +677,9 @@ function _approve(
}//
//
}/**//**//**//**//**//**//**///

// ---- Expect: diagnostics ----
// warning: 195:50-56: conversion truncates uint256 to uint128, as value is type uint128 on target EVM
// warning: 268:17-25: function parameter 'weiValue' is unused
// warning: 269:23-35: function parameter 'errorMessage' is unused
// warning: 276:70-78: conversion truncates uint256 to uint128, as value is type uint128 on target EVM
// warning: 321:9-17: 'internal': visibility for constructors is ignored
// warning: 386:9-61: storage variable '_isExcluded' has never been used
// warning: 390:9-51: storage variable 'MAX' has been assigned, but never read
Expand Down
4 changes: 2 additions & 2 deletions tests/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ fn create_program_address(program_id: &Account, seeds: &[&[u8]]) -> Pubkey {

let hash = hasher.finalize();

let new_address: [u8; 32] = hash.try_into().unwrap();
let new_address: [u8; 32] = hash.into();

// the real runtime does checks if this address exists on the ed25519 curve

Expand Down Expand Up @@ -1268,7 +1268,7 @@ fn sol_invoke_signed_c(

let hash = hasher.finalize();

let new_address: [u8; 32] = hash.try_into().unwrap();
let new_address: [u8; 32] = hash.into();

println!(
"creating account {} with space {} owner {}",
Expand Down

0 comments on commit f401069

Please sign in to comment.