From 489586430202d17fb60d973503ab27670474143d Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 18 Apr 2018 14:28:53 +0200 Subject: [PATCH] Warn about functions named "constructor". --- Changelog.md | 3 ++- libsolidity/analysis/SyntaxChecker.cpp | 7 ++++++- .../syntaxTests/constructor/function_named_constructor.sol | 5 +++++ 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 test/libsolidity/syntaxTests/constructor/function_named_constructor.sol diff --git a/Changelog.md b/Changelog.md index 230c1174a..8812bacef 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,8 +1,9 @@ ### 0.4.23 (unreleased) Features: - * SMTChecker: Integration with CVC4 SMT solver * Build system: Support Ubuntu Bionic. + * SMTChecker: Integration with CVC4 SMT solver + * Syntax Checker: Warn about functions named "constructor". Bugfixes: * Type Checker: Do not complain about new-style constructor and fallback function to have the same name. diff --git a/libsolidity/analysis/SyntaxChecker.cpp b/libsolidity/analysis/SyntaxChecker.cpp index f648e5b40..396058f44 100644 --- a/libsolidity/analysis/SyntaxChecker.cpp +++ b/libsolidity/analysis/SyntaxChecker.cpp @@ -237,8 +237,13 @@ bool SyntaxChecker::visit(FunctionDefinition const& _function) if (v050) m_errorReporter.syntaxError(_function.location(), "Functions without implementation cannot have modifiers."); else - m_errorReporter.warning( _function.location(), "Modifiers of functions without implementation are ignored." ); + m_errorReporter.warning(_function.location(), "Modifiers of functions without implementation are ignored." ); } + if (_function.name() == "constructor") + m_errorReporter.warning(_function.location(), + "This function is named \"constructor\" but is not the constructor of the contract. " + "If you intend this to be a constructor, use \"constructor(...) { ... }\" without the \"function\" keyword to define it." + ); return true; } diff --git a/test/libsolidity/syntaxTests/constructor/function_named_constructor.sol b/test/libsolidity/syntaxTests/constructor/function_named_constructor.sol new file mode 100644 index 000000000..29784033e --- /dev/null +++ b/test/libsolidity/syntaxTests/constructor/function_named_constructor.sol @@ -0,0 +1,5 @@ +contract C { + function constructor() public; +} +// ---- +// Warning: (17-47): This function is named "constructor" but is not the constructor of the contract. If you intend this to be a constructor, use "constructor(...) { ... }" without the "function" keyword to define it.