Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate use of unary '+' #2199

Merged
merged 2 commits into from May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Expand Up @@ -10,6 +10,7 @@ Features:
* Inline Assembly: Storage variable access using ``_slot`` and ``_offset`` suffixes.
* Inline Assembly: Disallow blocks with unbalanced stack.
* Static analyzer: Warn about statements without effects.
* Syntax checker: issue deprecation warning for unary '+'

Bugfixes:
* Assembly output: Implement missing AssemblyItem types.
Expand Down
17 changes: 17 additions & 0 deletions libsolidity/analysis/SyntaxChecker.cpp
Expand Up @@ -32,6 +32,16 @@ bool SyntaxChecker::checkSyntax(ASTNode const& _astRoot)
return Error::containsOnlyWarnings(m_errors);
}

void SyntaxChecker::warning(SourceLocation const& _location, string const& _description)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should figure out a way to have a helper for this. I think we have this duplicated in every single file almost by now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'm a fan of keeping refactoring commits separate from feature commits, so if it's okay, I'll work on a helper class in a separate pull request.

{
auto err = make_shared<Error>(Error::Type::Warning);
*err <<
errinfo_sourceLocation(_location) <<
errinfo_comment(_description);

m_errors.push_back(err);
}

void SyntaxChecker::syntaxError(SourceLocation const& _location, std::string const& _description)
{
auto err = make_shared<Error>(Error::Type::SyntaxError);
Expand Down Expand Up @@ -148,6 +158,13 @@ bool SyntaxChecker::visit(Break const& _breakStatement)
return true;
}

bool SyntaxChecker::visit(UnaryOperation const& _operation)
{
if (_operation.getOperator() == Token::Add)
warning(_operation.location(), "Use of unary + is deprecated.");
return true;
}

bool SyntaxChecker::visit(PlaceholderStatement const&)
{
m_placeholderFound = true;
Expand Down
4 changes: 4 additions & 0 deletions libsolidity/analysis/SyntaxChecker.h
Expand Up @@ -32,6 +32,7 @@ namespace solidity
* The module that performs syntax analysis on the AST:
* - whether continue/break is in a for/while loop.
* - whether a modifier contains at least one '_'
* - issues deprecation warnings for unary '+'
*/
class SyntaxChecker: private ASTConstVisitor
{
Expand All @@ -43,6 +44,7 @@ class SyntaxChecker: private ASTConstVisitor

private:
/// Adds a new error to the list of errors.
void warning(SourceLocation const& _location, std::string const& _description);
void syntaxError(SourceLocation const& _location, std::string const& _description);

virtual bool visit(SourceUnit const& _sourceUnit) override;
Expand All @@ -60,6 +62,8 @@ class SyntaxChecker: private ASTConstVisitor
virtual bool visit(Continue const& _continueStatement) override;
virtual bool visit(Break const& _breakStatement) override;

virtual bool visit(UnaryOperation const& _operation) override;

virtual bool visit(PlaceholderStatement const& _placeholderStatement) override;

ErrorList& m_errors;
Expand Down
19 changes: 18 additions & 1 deletion test/libsolidity/SolidityNameAndTypeResolution.cpp
Expand Up @@ -3936,14 +3936,31 @@ BOOST_AUTO_TEST_CASE(invalid_int_implicit_conversion_from_fixed)
BOOST_AUTO_TEST_CASE(rational_unary_operation)
{
char const* text = R"(
contract test {
function f() {
ufixed8x16 a = 3.25;
fixed8x16 b = -3.25;
}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
text = R"(
contract test {
function f() {
ufixed8x16 a = +3.25;
fixed8x16 b = -3.25;
}
}
)";
CHECK_SUCCESS(text);
CHECK_WARNING(text,"Use of unary + is deprecated");
text = R"(
contract test {
function f(uint x) {
uint y = +x;
}
}
)";
CHECK_WARNING(text,"Use of unary + is deprecated");
}

BOOST_AUTO_TEST_CASE(leading_zero_rationals_convert)
Expand Down