aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/analysis
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2018-09-04 23:24:06 +0800
committerGitHub <noreply@github.com>2018-09-04 23:24:06 +0800
commit9daac90cf7d0a28f5d631b9f5234e4268b5b61b7 (patch)
tree4a65232241445b00e29977b8e958f0b34be1ff8b /libsolidity/analysis
parentcc7daf7b476f498c4dcde7bdb342974097ffe9bf (diff)
parentac8892e0e3ab4e0152ba74c5857b79aec54e7f1b (diff)
downloaddexon-solidity-9daac90cf7d0a28f5d631b9f5234e4268b5b61b7.tar.gz
dexon-solidity-9daac90cf7d0a28f5d631b9f5234e4268b5b61b7.tar.zst
dexon-solidity-9daac90cf7d0a28f5d631b9f5234e4268b5b61b7.zip
Merge pull request #4888 from ethereum/disallow_single_statement_vardecl_if_while_for
Disallow single statement var decl in if/while/for without blocks
Diffstat (limited to 'libsolidity/analysis')
-rw-r--r--libsolidity/analysis/SyntaxChecker.cpp21
-rw-r--r--libsolidity/analysis/SyntaxChecker.h6
2 files changed, 25 insertions, 2 deletions
diff --git a/libsolidity/analysis/SyntaxChecker.cpp b/libsolidity/analysis/SyntaxChecker.cpp
index ac4fa72b..0bc20f2e 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(), "Variable declarations can only be used inside blocks.");
+}
+
+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;
}
diff --git a/libsolidity/analysis/SyntaxChecker.h b/libsolidity/analysis/SyntaxChecker.h
index 897df676..f5716bf9 100644
--- a/libsolidity/analysis/SyntaxChecker.h
+++ b/libsolidity/analysis/SyntaxChecker.h
@@ -52,6 +52,12 @@ private:
virtual bool visit(ModifierDefinition const& _modifier) override;
virtual void endVisit(ModifierDefinition const& _modifier) override;
+ /// Reports an error if _statement is a VariableDeclarationStatement.
+ /// Used by if/while/for to check for single statement variable declarations
+ /// without a block.
+ void checkSingleStatementVariableDeclaration(ASTNode const& _statement);
+
+ virtual bool visit(IfStatement const& _ifStatement) override;
virtual bool visit(WhileStatement const& _whileStatement) override;
virtual void endVisit(WhileStatement const& _whileStatement) override;
virtual bool visit(ForStatement const& _forStatement) override;