diff options
author | Leonardo Alt <leo@ethereum.org> | 2018-09-04 00:17:59 +0800 |
---|---|---|
committer | Leonardo Alt <leo@ethereum.org> | 2018-09-04 00:21:57 +0800 |
commit | 4522c804f342707ec2bb86a19735a4084108d96d (patch) | |
tree | ccd25ee49fc27fdf0bc041f07d7a242d7b5ab25f /libsolidity/analysis/SyntaxChecker.cpp | |
parent | 6e5e05779c6085c1fc58d38390cc5cb447068ecb (diff) | |
download | dexon-solidity-4522c804f342707ec2bb86a19735a4084108d96d.tar.gz dexon-solidity-4522c804f342707ec2bb86a19735a4084108d96d.tar.zst dexon-solidity-4522c804f342707ec2bb86a19735a4084108d96d.zip |
Disallow single statement var decl in if/while/for without blocks
Diffstat (limited to 'libsolidity/analysis/SyntaxChecker.cpp')
-rw-r--r-- | libsolidity/analysis/SyntaxChecker.cpp | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/libsolidity/analysis/SyntaxChecker.cpp b/libsolidity/analysis/SyntaxChecker.cpp index ac4fa72b..1b5c00c4 100644 --- a/libsolidity/analysis/SyntaxChecker.cpp +++ b/libsolidity/analysis/SyntaxChecker.cpp @@ -138,9 +138,25 @@ void SyntaxChecker::endVisit(ModifierDefinition const& _modifier) m_placeholderFound = false; } -bool SyntaxChecker::visit(WhileStatement const&) +void SyntaxChecker::checkSingleStatementVariableDeclaration(ASTNode const* _statement) +{ + auto varDecl = dynamic_cast<VariableDeclarationStatement const*>(_statement); + if (varDecl) + m_errorReporter.syntaxError(_statement->location(), "Invalid variable declaration. Please declare it inside a block."); +} + +bool SyntaxChecker::visit(IfStatement const& _ifStatement) +{ + checkSingleStatementVariableDeclaration(&_ifStatement.trueStatement()); + if (Statement const* _statement = _ifStatement.falseStatement()) + checkSingleStatementVariableDeclaration(_statement); + return true; +} + +bool SyntaxChecker::visit(WhileStatement const& _whileStatement) { m_inLoopDepth++; + checkSingleStatementVariableDeclaration(&_whileStatement.body()); return true; } @@ -149,9 +165,10 @@ void SyntaxChecker::endVisit(WhileStatement const&) m_inLoopDepth--; } -bool SyntaxChecker::visit(ForStatement const&) +bool SyntaxChecker::visit(ForStatement const& _forStatement) { m_inLoopDepth++; + checkSingleStatementVariableDeclaration(&_forStatement.body()); return true; } |