Support unary operation, MINUS_PRE, on constants

pull/1094/head
alpharush 3 years ago
parent cf8be9df87
commit cac3f97e5a
  1. 13
      slither/visitors/expression/constants_folding.py
  2. 7
      tests/constant_folding_unary.sol
  3. 5
      tests/test_constant_folding_unary.py

@ -1,4 +1,4 @@
from slither.core.expressions import BinaryOperationType, Literal
from slither.core.expressions import BinaryOperationType, Literal, UnaryOperationType
from slither.utils.integer_conversion import convert_string_to_int
from slither.visitors.expression.expression import ExpressionVisitor
@ -65,7 +65,16 @@ class ConstantFolding(ExpressionVisitor):
raise NotConstant
def _post_unary_operation(self, expression):
raise NotConstant
# Case of uint a = -7; uint[-a] arr;
if expression.type == UnaryOperationType.MINUS_PRE:
expr = expression.expression
if not isinstance(expr, Literal):
cf = ConstantFolding(expr, self._type)
expr = cf.result()
assert isinstance(expr, Literal)
set_val(expression, int(expr.value))
else:
raise NotConstant
def _post_literal(self, expression):
if expression.value.isdigit():

@ -0,0 +1,7 @@
contract C {
int8 constant a = -7;
function f() public pure {
uint[-a] memory x;
x[0] = 2;
}
}

@ -0,0 +1,5 @@
from slither import Slither
def test_constant_folding_unary():
Slither("./tests/constant_folding_unary.sol")
Loading…
Cancel
Save