From 0e14dac7ee48105a6701cf30c1dba706cbe8f7d1 Mon Sep 17 00:00:00 2001 From: LucasSte Date: Fri, 9 Jul 2021 10:58:47 -0300 Subject: [PATCH] Address reviewer issues and add a new test case Signed-off-by: LucasSte --- src/sema/mod.rs | 10 +++--- src/sema/statements.rs | 3 +- src/sema/variables.rs | 6 ++-- tests/unused_variable_detection.rs | 55 ++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 10 deletions(-) diff --git a/src/sema/mod.rs b/src/sema/mod.rs index 97f7bdfd46..46fd79b0c9 100644 --- a/src/sema/mod.rs +++ b/src/sema/mod.rs @@ -41,16 +41,18 @@ pub const SOLANA_BUCKET_SIZE: u64 = 251; pub const SOLANA_FIRST_OFFSET: u64 = 16; pub const SOLANA_SPARSE_ARRAY_SIZE: u64 = 1024; -/// Performs semantic analysis and checks for unused variables +/// Load a file file from the cache, parse and resolve it. The file must be present in +/// the cache. pub fn sema(file: ResolvedFile, cache: &mut FileCache, ns: &mut ast::Namespace) { sema_file(file, cache, ns); + + // Checks for unused variables check_unused_namespace_variables(ns); check_unused_events(ns); } -/// Load a file file from the cache, parse and resolve it. The file must be present in -/// the cache. This function is recursive for imports. -pub fn sema_file(file: ResolvedFile, cache: &mut FileCache, ns: &mut ast::Namespace) { +/// Parse and resolve a file and its imports in a recursive manner. +fn sema_file(file: ResolvedFile, cache: &mut FileCache, ns: &mut ast::Namespace) { let file_no = ns.files.len(); let source_code = cache.get_file_contents(&file.full_path); diff --git a/src/sema/statements.rs b/src/sema/statements.rs index cc3710a0ab..7f849cbe13 100644 --- a/src/sema/statements.rs +++ b/src/sema/statements.rs @@ -1295,8 +1295,7 @@ fn destructure_values( diagnostics, Some(&Type::Bool), )?; - std::println!("Aqui!!"); - std::println!("{:?}", cond); + used_variable(ns, &cond, symtable); let left = destructure_values( &left.loc(), diff --git a/src/sema/variables.rs b/src/sema/variables.rs index 64c761fa8d..e679ae75ce 100644 --- a/src/sema/variables.rs +++ b/src/sema/variables.rs @@ -98,7 +98,6 @@ pub fn var_decl( let mut is_constant = false; let mut visibility: Option = None; - let mut initialized = false; for attr in attrs { match &attr { @@ -179,7 +178,6 @@ pub fn var_decl( let initializer = if let Some(initializer) = &s.initializer { let mut diagnostics = Vec::new(); - initialized = true; let res = match expression( &initializer, @@ -251,9 +249,9 @@ pub fn var_decl( visibility: visibility.clone(), ty: ty.clone(), constant: is_constant, - initializer, + initializer: initializer.clone(), read: false, - assigned: initialized, + assigned: initializer.is_some(), }; let pos = if let Some(contract_no) = contract_no { diff --git a/tests/unused_variable_detection.rs b/tests/unused_variable_detection.rs index 2e7c45cf05..4e27ced1d5 100644 --- a/tests/unused_variable_detection.rs +++ b/tests/unused_variable_detection.rs @@ -844,3 +844,58 @@ fn struct_initialization() { let ns = generic_target_parse(file); assert_eq!(count_warnings(&ns.diagnostics), 0); } + +#[test] +fn subarray_mapping_struct_literal() { + let file = r#" + contract T { + int p; + constructor(int b) { + p = b; + } + + function sum(int a, int b) virtual public returns (int){ + uint8 v1 = 1; + uint8 v2 = 2; + uint8 v3 = 3; + uint8 v4 = 4; + uint8[2][2] memory v = [[v1, v2], [v3, v4]]; + return a + b * p/v[0][1]; + } + } + + contract Test is T(2){ + + struct fooStruct { + int foo; + int figther; + } + mapping(string => int) public mp; + enum FreshJuiceSize{ SMALL, MEDIUM, LARGE } + FreshJuiceSize choice; + + function sum(int a, int b) override public returns (int) { + choice = FreshJuiceSize.LARGE; + return a*b; + } + + function test() public returns (int){ + int a = 1; + int b = 2; + int c = super.sum(a, b); + int d = 3; + fooStruct memory myStruct = fooStruct({foo: c, figther: d}); + string memory t = "Do some tests"; + mp[t] = myStruct.figther; + return mp[t]; + } + } + "#; + + let ns = generic_target_parse(file); + assert_eq!(count_warnings(&ns.diagnostics), 1); + assert!(assert_message_in_warnings( + &ns.diagnostics, + "storage variable 'choice' has been assigned, but never read" + )); +}