Skip to content

Commit

Permalink
Implement missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasSte committed Jul 7, 2021
1 parent dc179f8 commit e3a2cee
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/sema/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ fn resolve_bodies(
{
broken = true;
} else {
for (_, variable) in &ns.functions[function_no].symtable.vars {
for variable in ns.functions[function_no].symtable.vars.values() {
if let Some(warning) = emit_warning_local_variable(&variable) {
ns.diagnostics.push(warning);
}
Expand Down
60 changes: 36 additions & 24 deletions src/sema/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,27 +1262,35 @@ fn destructure_values(
diagnostics: &mut Vec<Diagnostic>,
) -> Result<Expression, ()> {
let expr = match expr {
pt::Expression::FunctionCall(loc, ty, args) => function_call_expr(
loc,
ty,
args,
file_no,
contract_no,
ns,
symtable,
false,
diagnostics,
)?,
pt::Expression::NamedFunctionCall(loc, ty, args) => named_function_call_expr(
loc,
ty,
args,
file_no,
contract_no,
ns,
symtable,
diagnostics,
)?,
pt::Expression::FunctionCall(loc, ty, args) => {
let res = function_call_expr(
loc,
ty,
args,
file_no,
contract_no,
ns,
symtable,
false,
diagnostics,
)?;
check_function_call(ns, &res, symtable);
res
}
pt::Expression::NamedFunctionCall(loc, ty, args) => {
let res = named_function_call_expr(
loc,
ty,
args,
file_no,
contract_no,
ns,
symtable,
diagnostics,
)?;
check_function_call(ns, &res, symtable);
res
}
pt::Expression::Ternary(loc, cond, left, right) => {
let cond = expression(
cond,
Expand Down Expand Up @@ -1319,13 +1327,15 @@ fn destructure_values(
diagnostics,
)?;

return Ok(Expression::Ternary(
let ternary = Expression::Ternary(
*loc,
Type::Unreachable,
Box::new(cond),
Box::new(left),
Box::new(right),
));
);
used_variable(ns, &ternary, symtable);
return Ok(ternary);
}
_ => {
let mut list = Vec::new();
Expand Down Expand Up @@ -1364,7 +1374,9 @@ fn destructure_values(
));
return Err(());
}
_ => (),
_ => {
used_variable(ns, &e, symtable);
}
}

list.push(e);
Expand Down
14 changes: 10 additions & 4 deletions src/sema/unused_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ pub fn used_variable(ns: &mut Namespace, exp: &Expression, symtable: &mut Symtab
}
}

Expression::Ternary(_, _, cond, left, right) => {
used_variable(ns, cond, symtable);
used_variable(ns, left, symtable);
used_variable(ns, right, symtable);
}

_ => {}
}
}
Expand Down Expand Up @@ -145,12 +151,12 @@ pub fn check_function_call(ns: &mut Namespace, exp: &Expression, symtable: &mut
Expression::ExternalFunctionCallRaw {
loc: _,
ty: _,
address: function,
address,
args,
..
} => {
used_variable(ns, args, symtable);
check_function_call(ns, function, symtable);
used_variable(ns, address, symtable);
}

Expression::ExternalFunction {
Expand Down Expand Up @@ -207,12 +213,12 @@ pub fn check_var_usage_expression(
}

/// Generate warnings for unused varibles
fn generate_unused_warning(loc: Loc, text: &String, notes: Vec<Note>) -> Diagnostic {
fn generate_unused_warning(loc: Loc, text: &str, notes: Vec<Note>) -> Diagnostic {
Diagnostic {
level: Level::Warning,
ty: ErrorType::Warning,
pos: Some(loc),
message: text.clone(),
message: text.parse().unwrap(),
notes,
}
}
Expand Down
29 changes: 29 additions & 0 deletions tests/unused_variable_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,3 +878,32 @@ fn try_catch() {
true
);
}

#[test]
fn destructure() {
let file = r#"
contract Test2{
function callTest() public view returns (uint32 ret) {
uint32 ta = 1;
uint32 tb = 2;
uint32 te = 3;
string memory tc = "hey";
bytes memory td = bytes(tc);
address nameReg = address(this);
(bool tf,) = nameReg.call(td);
ta = tf? tb : te;
uint8 tg = 1;
uint8 th = 2;
(tg, th) = (th, tg);
return ta;
}
}
"#;

let ns = generic_target_parse(file);
assert_eq!(count_warnings(&ns.diagnostics), 0);
}

0 comments on commit e3a2cee

Please sign in to comment.