Skip to content

Commit e7683f4

Browse files
authored
Merge pull request #836 from chriseth/unusedunderscore
BREAKING: Require modifiers to contain "_".
2 parents e457898 + 15b85e2 commit e7683f4

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

libsolidity/analysis/SyntaxChecker.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,26 @@ void SyntaxChecker::syntaxError(SourceLocation const& _location, std::string con
4040
m_errors.push_back(err);
4141
}
4242

43+
bool SyntaxChecker::visit(ModifierDefinition const&)
44+
{
45+
m_placeholderFound = false;
46+
return true;
47+
}
48+
49+
void SyntaxChecker::endVisit(ModifierDefinition const& _modifier)
50+
{
51+
if (!m_placeholderFound)
52+
syntaxError(_modifier.body().location(), "Modifier body does not contain '_'.");
53+
m_placeholderFound = false;
54+
}
55+
4356
bool SyntaxChecker::visit(WhileStatement const&)
4457
{
4558
m_inLoopDepth++;
4659
return true;
4760
}
4861

49-
void SyntaxChecker::endVisit(WhileStatement const&)
62+
void SyntaxChecker::endVisit(WhileStatement const& )
5063
{
5164
m_inLoopDepth--;
5265
}
@@ -78,3 +91,9 @@ bool SyntaxChecker::visit(Break const& _breakStatement)
7891
return true;
7992
}
8093

94+
bool SyntaxChecker::visit(const PlaceholderStatement&)
95+
{
96+
m_placeholderFound = true;
97+
return true;
98+
}
99+

libsolidity/analysis/SyntaxChecker.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace solidity
3131
/**
3232
* The module that performs syntax analysis on the AST:
3333
* - whether continue/break is in a for/while loop.
34+
* - whether a modifier contains at least one '_'
3435
*/
3536
class SyntaxChecker: private ASTConstVisitor
3637
{
@@ -44,6 +45,9 @@ class SyntaxChecker: private ASTConstVisitor
4445
/// Adds a new error to the list of errors.
4546
void syntaxError(SourceLocation const& _location, std::string const& _description);
4647

48+
virtual bool visit(ModifierDefinition const& _modifier) override;
49+
virtual void endVisit(ModifierDefinition const& _modifier) override;
50+
4751
virtual bool visit(WhileStatement const& _whileStatement) override;
4852
virtual void endVisit(WhileStatement const& _whileStatement) override;
4953
virtual bool visit(ForStatement const& _forStatement) override;
@@ -52,8 +56,13 @@ class SyntaxChecker: private ASTConstVisitor
5256
virtual bool visit(Continue const& _continueStatement) override;
5357
virtual bool visit(Break const& _breakStatement) override;
5458

59+
virtual bool visit(PlaceholderStatement const& _placeholderStatement) override;
60+
5561
ErrorList& m_errors;
5662

63+
/// Flag that indicates whether a function modifier actually contains '_'.
64+
bool m_placeholderFound = false;
65+
5766
int m_inLoopDepth = 0;
5867
};
5968

test/libsolidity/SolidityEndToEndTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2410,7 +2410,7 @@ BOOST_AUTO_TEST_CASE(function_modifier_overriding)
24102410
modifier mod { _ }
24112411
}
24122412
contract C is A {
2413-
modifier mod { }
2413+
modifier mod { if (false) _ }
24142414
}
24152415
)";
24162416
compileAndRun(sourceCode);
@@ -2427,7 +2427,7 @@ BOOST_AUTO_TEST_CASE(function_modifier_calling_functions_in_creation_context)
24272427
function f2() { data |= 0x20; }
24282428
function f3() { }
24292429
modifier mod1 { f2(); _ }
2430-
modifier mod2 { f3(); }
2430+
modifier mod2 { f3(); if (false) _ }
24312431
function getData() returns (uint r) { return data; }
24322432
}
24332433
contract C is A {

test/libsolidity/SolidityNameAndTypeResolution.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -901,35 +901,35 @@ BOOST_AUTO_TEST_CASE(function_modifier_invocation_local_variables)
901901
BOOST_AUTO_TEST_CASE(legal_modifier_override)
902902
{
903903
char const* text = R"(
904-
contract A { modifier mod(uint a) {} }
905-
contract B is A { modifier mod(uint a) {} }
904+
contract A { modifier mod(uint a) { _ } }
905+
contract B is A { modifier mod(uint a) { _ } }
906906
)";
907907
BOOST_CHECK(success(text));
908908
}
909909

910910
BOOST_AUTO_TEST_CASE(illegal_modifier_override)
911911
{
912912
char const* text = R"(
913-
contract A { modifier mod(uint a) {} }
914-
contract B is A { modifier mod(uint8 a) {} }
913+
contract A { modifier mod(uint a) { _ } }
914+
contract B is A { modifier mod(uint8 a) { _ } }
915915
)";
916916
BOOST_CHECK(expectError(text) == Error::Type::TypeError);
917917
}
918918

919919
BOOST_AUTO_TEST_CASE(modifier_overrides_function)
920920
{
921921
char const* text = R"(
922-
contract A { modifier mod(uint a) {} }
923-
contract B is A { function mod(uint a) {} }
922+
contract A { modifier mod(uint a) { _ } }
923+
contract B is A { function mod(uint a) { } }
924924
)";
925925
BOOST_CHECK(expectError(text) == Error::Type::TypeError);
926926
}
927927

928928
BOOST_AUTO_TEST_CASE(function_overrides_modifier)
929929
{
930930
char const* text = R"(
931-
contract A { function mod(uint a) {} }
932-
contract B is A { modifier mod(uint a) {} }
931+
contract A { function mod(uint a) { } }
932+
contract B is A { modifier mod(uint a) { _ } }
933933
)";
934934
BOOST_CHECK(expectError(text) == Error::Type::TypeError);
935935
}
@@ -938,8 +938,8 @@ BOOST_AUTO_TEST_CASE(modifier_returns_value)
938938
{
939939
char const* text = R"(
940940
contract A {
941-
function f(uint a) mod(2) returns (uint r) {}
942-
modifier mod(uint a) { return 7; }
941+
function f(uint a) mod(2) returns (uint r) { }
942+
modifier mod(uint a) { _ return 7; }
943943
}
944944
)";
945945
BOOST_CHECK(expectError(text) == Error::Type::TypeError);
@@ -3823,6 +3823,16 @@ BOOST_AUTO_TEST_CASE(unused_return_value_delegatecall)
38233823
BOOST_CHECK(expectError(text, true) == Error::Type::Warning);
38243824
}
38253825

3826+
BOOST_AUTO_TEST_CASE(modifier_without_underscore)
3827+
{
3828+
char const* text = R"(
3829+
contract test {
3830+
modifier m() {}
3831+
}
3832+
)";
3833+
BOOST_CHECK(expectError(text, true) == Error::Type::SyntaxError);
3834+
}
3835+
38263836
BOOST_AUTO_TEST_SUITE_END()
38273837

38283838
}

0 commit comments

Comments
 (0)