Skip to content

Commit

Permalink
Address reviewer issues and add a new test case
Browse files Browse the repository at this point in the history
Signed-off-by: LucasSte <[email protected]>
  • Loading branch information
LucasSte committed Jul 9, 2021
1 parent 2f1f6d9 commit 0e14dac
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/sema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/sema/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
6 changes: 2 additions & 4 deletions src/sema/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ pub fn var_decl(

let mut is_constant = false;
let mut visibility: Option<pt::Visibility> = None;
let mut initialized = false;

for attr in attrs {
match &attr {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
55 changes: 55 additions & 0 deletions tests/unused_variable_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
));
}

0 comments on commit 0e14dac

Please sign in to comment.