Skip to content

Commit

Permalink
Implement unused variable detection
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasSte committed Jul 5, 2021
1 parent 0de6923 commit 5c2b19b
Show file tree
Hide file tree
Showing 13 changed files with 715 additions and 133 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
Cargo.lock
/target
**/*.rs.bk
.DS_Store
.idea
3 changes: 3 additions & 0 deletions src/sema/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct EventDecl {
pub fields: Vec<Parameter>,
pub signature: String,
pub anonymous: bool,
pub used: bool,
}

impl fmt::Display for EventDecl {
Expand Down Expand Up @@ -284,6 +285,8 @@ pub struct Variable {
pub visibility: pt::Visibility,
pub constant: bool,
pub initializer: Option<Expression>,
pub assigned: bool,
pub read: bool,
}

#[derive(Clone, PartialEq)]
Expand Down
4 changes: 2 additions & 2 deletions src/sema/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ pub fn resolve_call(
args: &[pt::Expression],
contract_no: Option<usize>,
ns: &mut Namespace,
symtable: &Symtable,
symtable: &mut Symtable,
is_constant: bool,
diagnostics: &mut Vec<Diagnostic>,
) -> Result<Expression, ()> {
Expand Down Expand Up @@ -590,7 +590,7 @@ pub fn resolve_method_call(
args: &[pt::Expression],
contract_no: Option<usize>,
ns: &mut Namespace,
symtable: &Symtable,
symtable: &mut Symtable,
diagnostics: &mut Vec<Diagnostic>,
) -> Result<Expression, ()> {
// The abi.* functions need special handling, others do not
Expand Down
11 changes: 9 additions & 2 deletions src/sema/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::statements;
use super::symtable::Symtable;
use super::variables;
use super::{ast, SOLANA_FIRST_OFFSET};
use crate::sema::unused_variable::emit_warning_local_variable;
use crate::{emit, Target};

impl ast::Contract {
Expand Down Expand Up @@ -249,7 +250,7 @@ fn resolve_base_args(
.position(|e| e.contract_no == base_no)
{
if let Some(args) = &base.args {
let symtable = Symtable::new();
let mut symtable = Symtable::new();

// find constructor which matches this
if let Ok((Some(constructor_no), args)) = match_constructor_to_args(
Expand All @@ -259,7 +260,7 @@ fn resolve_base_args(
base_no,
*contract_no,
ns,
&symtable,
&mut symtable,
&mut diagnostics,
) {
ns.contracts[*contract_no].bases[pos].constructor =
Expand Down Expand Up @@ -868,6 +869,12 @@ fn resolve_bodies(
.is_err()
{
broken = true;
} else {
for (_, variable) in &ns.functions[function_no].symtable.vars {
if let Some(warning) = emit_warning_local_variable(&variable) {
ns.diagnostics.push(*warning);
}
}
}
}

Expand Down
Loading

0 comments on commit 5c2b19b

Please sign in to comment.