mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #4888 from ethereum/disallow_single_statement_vardecl_if_while_for
Disallow single statement var decl in if/while/for without blocks
This commit is contained in:
commit
9daac90cf7
@ -68,6 +68,7 @@ Breaking Changes:
|
||||
* Syntax Checker: Named return values in function types are an error.
|
||||
* Syntax Checker: Strictly require visibility specifier for functions. This was already the case in the experimental 0.5.0 mode.
|
||||
* Syntax Checker: Disallow unary ``+``. This was already the case in the experimental 0.5.0 mode.
|
||||
* Syntax Checker: Disallow single statement variable declaration inside if/while/for bodies that are not blocks.
|
||||
* View Pure Checker: Strictly enfore state mutability. This was already the case in the experimental 0.5.0 mode.
|
||||
|
||||
Language Features:
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -0,0 +1,12 @@
|
||||
pragma solidity >0.4.24;
|
||||
|
||||
contract C
|
||||
{
|
||||
function f(uint x) public pure {
|
||||
do
|
||||
uint y;
|
||||
while (x > 0);
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// SyntaxError: (81-87): Variable declarations can only be used inside blocks.
|
13
test/libsolidity/syntaxTests/variableDeclaration/else.sol
Normal file
13
test/libsolidity/syntaxTests/variableDeclaration/else.sol
Normal file
@ -0,0 +1,13 @@
|
||||
pragma solidity >0.4.24;
|
||||
|
||||
contract C
|
||||
{
|
||||
function f(uint x) public pure {
|
||||
if (x > 0)
|
||||
{uint y;}
|
||||
else
|
||||
uint z;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// SyntaxError: (109-115): Variable declarations can only be used inside blocks.
|
11
test/libsolidity/syntaxTests/variableDeclaration/for.sol
Normal file
11
test/libsolidity/syntaxTests/variableDeclaration/for.sol
Normal file
@ -0,0 +1,11 @@
|
||||
pragma solidity >0.4.24;
|
||||
|
||||
contract C
|
||||
{
|
||||
function f(uint x) public pure {
|
||||
for (uint i = 0; i < x; ++i)
|
||||
uint y;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// SyntaxError: (107-113): Variable declarations can only be used inside blocks.
|
11
test/libsolidity/syntaxTests/variableDeclaration/if.sol
Normal file
11
test/libsolidity/syntaxTests/variableDeclaration/if.sol
Normal file
@ -0,0 +1,11 @@
|
||||
pragma solidity >0.4.24;
|
||||
|
||||
contract C
|
||||
{
|
||||
function f(uint x) public pure {
|
||||
if (x > 0)
|
||||
uint y;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// SyntaxError: (89-95): Variable declarations can only be used inside blocks.
|
11
test/libsolidity/syntaxTests/variableDeclaration/while.sol
Normal file
11
test/libsolidity/syntaxTests/variableDeclaration/while.sol
Normal file
@ -0,0 +1,11 @@
|
||||
pragma solidity >0.4.24;
|
||||
|
||||
contract C
|
||||
{
|
||||
function f(uint x) public pure {
|
||||
while (x > 0)
|
||||
uint y;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// SyntaxError: (92-98): Variable declarations can only be used inside blocks.
|
Loading…
Reference in New Issue
Block a user