diff --git a/slither/detectors/all_detectors.py b/slither/detectors/all_detectors.py index fb2e3c731..1cab317ad 100644 --- a/slither/detectors/all_detectors.py +++ b/slither/detectors/all_detectors.py @@ -19,7 +19,8 @@ from .reentrancy.reentrancy_eth import ReentrancyEth from .reentrancy.reentrancy_no_gas import ReentrancyNoGas from .reentrancy.reentrancy_events import ReentrancyEvent from .variables.unused_state_variables import UnusedStateVars -from .variables.possible_const_state_variables import ConstCandidateStateVars +from .variables.could_be_constant import CouldBeConstant +from .variables.could_be_immutable import CouldBeImmutable from .statements.tx_origin import TxOrigin from .statements.assembly import Assembly from .operations.low_level_calls import LowLevelCalls diff --git a/slither/detectors/variables/could_be_constant.py b/slither/detectors/variables/could_be_constant.py new file mode 100644 index 000000000..0a294dd8d --- /dev/null +++ b/slither/detectors/variables/could_be_constant.py @@ -0,0 +1,45 @@ +from typing import List, Dict +from slither.utils.output import Output +from slither.core.compilation_unit import SlitherCompilationUnit +from slither.formatters.variables.unchanged_state_variables import custom_format +from slither.detectors.abstract_detector import AbstractDetector, DetectorClassification +from .unchanged_state_variables import UnchangedStateVariables + + +class CouldBeConstant(AbstractDetector): + """ + State variables that could be declared as constant. + Not all types for constants are implemented in Solidity as of 0.4.25. + The only supported types are value types and strings (ElementaryType). + Reference: https://solidity.readthedocs.io/en/latest/contracts.html#constant-state-variables + """ + + ARGUMENT = "constable-states" + HELP = "State variables that could be declared constant" + IMPACT = DetectorClassification.OPTIMIZATION + CONFIDENCE = DetectorClassification.HIGH + + WIKI = "https://github.com/crytic/slither/wiki/Detector-Documentation#state-variables-that-could-be-declared-constant" + + WIKI_TITLE = "State variables that could be declared constant" + WIKI_DESCRIPTION = "State variables that are not updated following deployment should be declared constant to save gas." + WIKI_RECOMMENDATION = "Add the `constant` attribute to state variables that never change." + + def _detect(self) -> List[Output]: + """Detect state variables that could be constant""" + results = {} + + unchanged_state_variables = UnchangedStateVariables(self.compilation_unit) + unchanged_state_variables.detect() + + for variable in unchanged_state_variables.constant_candidates: + results[variable.canonical_name] = self.generate_result( + [variable, " should be constant \n"] + ) + + # Order by canonical name for deterministic results + return [results[k] for k in sorted(results)] + + @staticmethod + def _format(compilation_unit: SlitherCompilationUnit, result: Dict) -> None: + custom_format(compilation_unit, result, "constant") diff --git a/slither/detectors/variables/could_be_immutable.py b/slither/detectors/variables/could_be_immutable.py new file mode 100644 index 000000000..a9ecb60cd --- /dev/null +++ b/slither/detectors/variables/could_be_immutable.py @@ -0,0 +1,44 @@ +from typing import List, Dict +from slither.utils.output import Output +from slither.core.compilation_unit import SlitherCompilationUnit +from slither.formatters.variables.unchanged_state_variables import custom_format +from slither.detectors.abstract_detector import AbstractDetector, DetectorClassification +from .unchanged_state_variables import UnchangedStateVariables + + +class CouldBeImmutable(AbstractDetector): + """ + State variables that could be declared immutable. + # Immutable attribute available in Solidity 0.6.5 and above + # https://blog.soliditylang.org/2020/04/06/solidity-0.6.5-release-announcement/ + """ + + # VULNERABLE_SOLC_VERSIONS = + ARGUMENT = "immutable-states" + HELP = "State variables that could be declared immutable" + IMPACT = DetectorClassification.OPTIMIZATION + CONFIDENCE = DetectorClassification.HIGH + + WIKI = "https://github.com/crytic/slither/wiki/Detector-Documentation#state-variables-that-could-be-declared-immutable" + + WIKI_TITLE = "State variables that could be declared immutable" + WIKI_DESCRIPTION = "State variables that are not updated following deployment should be declared immutable to save gas." + WIKI_RECOMMENDATION = "Add the `immutable` attribute to state variables that never change or are set only in the constructor." + + def _detect(self) -> List[Output]: + """Detect state variables that could be immutable""" + results = {} + unchanged_state_variables = UnchangedStateVariables(self.compilation_unit) + unchanged_state_variables.detect() + + for variable in unchanged_state_variables.immutable_candidates: + results[variable.canonical_name] = self.generate_result( + [variable, " should be immutable \n"] + ) + + # Order by canonical name for deterministic results + return [results[k] for k in sorted(results)] + + @staticmethod + def _format(compilation_unit: SlitherCompilationUnit, result: Dict) -> None: + custom_format(compilation_unit, result, "immutable") diff --git a/slither/detectors/variables/possible_const_state_variables.py b/slither/detectors/variables/possible_const_state_variables.py deleted file mode 100644 index e5b35bb79..000000000 --- a/slither/detectors/variables/possible_const_state_variables.py +++ /dev/null @@ -1,125 +0,0 @@ -""" -Module detecting state variables that could be declared as constant -""" -from typing import Set, List, Dict - -from slither.core.compilation_unit import SlitherCompilationUnit -from slither.core.solidity_types.elementary_type import ElementaryType -from slither.core.solidity_types.user_defined_type import UserDefinedType -from slither.core.variables.variable import Variable -from slither.detectors.abstract_detector import AbstractDetector, DetectorClassification -from slither.utils.output import Output -from slither.visitors.expression.export_values import ExportValues -from slither.core.declarations import Contract, Function -from slither.core.declarations.solidity_variables import SolidityFunction -from slither.core.variables.state_variable import StateVariable -from slither.formatters.variables.possible_const_state_variables import custom_format - - -def _is_valid_type(v: StateVariable) -> bool: - t = v.type - if isinstance(t, ElementaryType): - return True - if isinstance(t, UserDefinedType) and isinstance(t.type, Contract): - return True - return False - - -def _valid_candidate(v: StateVariable) -> bool: - return _is_valid_type(v) and not (v.is_constant or v.is_immutable) - - -def _is_constant_var(v: Variable) -> bool: - if isinstance(v, StateVariable): - return v.is_constant - return False - - -class ConstCandidateStateVars(AbstractDetector): - """ - State variables that could be declared as constant detector. - Not all types for constants are implemented in Solidity as of 0.4.25. - The only supported types are value types and strings (ElementaryType). - Reference: https://solidity.readthedocs.io/en/latest/contracts.html#constant-state-variables - """ - - ARGUMENT = "constable-states" - HELP = "State variables that could be declared constant" - IMPACT = DetectorClassification.OPTIMIZATION - CONFIDENCE = DetectorClassification.HIGH - - WIKI = "https://github.com/crytic/slither/wiki/Detector-Documentation#state-variables-that-could-be-declared-constant" - - WIKI_TITLE = "State variables that could be declared constant" - WIKI_DESCRIPTION = "Constant state variables should be declared constant to save gas." - WIKI_RECOMMENDATION = "Add the `constant` attributes to state variables that never change." - - # https://solidity.readthedocs.io/en/v0.5.2/contracts.html#constant-state-variables - valid_solidity_function = [ - SolidityFunction("keccak256()"), - SolidityFunction("keccak256(bytes)"), - SolidityFunction("sha256()"), - SolidityFunction("sha256(bytes)"), - SolidityFunction("ripemd160()"), - SolidityFunction("ripemd160(bytes)"), - SolidityFunction("ecrecover(bytes32,uint8,bytes32,bytes32)"), - SolidityFunction("addmod(uint256,uint256,uint256)"), - SolidityFunction("mulmod(uint256,uint256,uint256)"), - ] - - def _constant_initial_expression(self, v: Variable) -> bool: - if not v.expression: - return True - - export = ExportValues(v.expression) - values = export.result() - if not values: - return True - if all((val in self.valid_solidity_function or _is_constant_var(val) for val in values)): - return True - return False - - def _detect(self) -> List[Output]: - """Detect state variables that could be const""" - results = [] - - all_variables_l = [c.state_variables for c in self.compilation_unit.contracts] - all_variables: Set[StateVariable] = { - item for sublist in all_variables_l for item in sublist - } - all_non_constant_elementary_variables = {v for v in all_variables if _valid_candidate(v)} - - all_functions_nested = [c.all_functions_called for c in self.compilation_unit.contracts] - all_functions = list( - { - item1 - for sublist in all_functions_nested - for item1 in sublist - if isinstance(item1, Function) - } - ) - - all_variables_written = [ - f.state_variables_written for f in all_functions if not f.is_constructor_variables - ] - all_variables_written = {item for sublist in all_variables_written for item in sublist} - - constable_variables: List[Variable] = [ - v - for v in all_non_constant_elementary_variables - if (v not in all_variables_written) and self._constant_initial_expression(v) - ] - # Order for deterministic results - constable_variables = sorted(constable_variables, key=lambda x: x.canonical_name) - - # Create a result for each finding - for v in constable_variables: - info = [v, " should be constant\n"] - json = self.generate_result(info) - results.append(json) - - return results - - @staticmethod - def _format(compilation_unit: SlitherCompilationUnit, result: Dict) -> None: - custom_format(compilation_unit, result) diff --git a/slither/detectors/variables/unchanged_state_variables.py b/slither/detectors/variables/unchanged_state_variables.py new file mode 100644 index 000000000..0dccb6d1c --- /dev/null +++ b/slither/detectors/variables/unchanged_state_variables.py @@ -0,0 +1,125 @@ +""" +Module detecting state variables that could be declared as constant +""" +from typing import Set, List +from packaging import version +from slither.core.compilation_unit import SlitherCompilationUnit +from slither.core.solidity_types.elementary_type import ElementaryType +from slither.core.solidity_types.user_defined_type import UserDefinedType +from slither.core.variables.variable import Variable + +from slither.visitors.expression.export_values import ExportValues +from slither.core.declarations import Contract, Function +from slither.core.declarations.solidity_variables import SolidityFunction +from slither.core.variables.state_variable import StateVariable +from slither.core.expressions import CallExpression, NewContract + + +def _is_valid_type(v: StateVariable) -> bool: + t = v.type + if isinstance(t, ElementaryType): + return True + if isinstance(t, UserDefinedType) and isinstance(t.type, Contract): + return True + return False + + +def _valid_candidate(v: StateVariable) -> bool: + return _is_valid_type(v) and not (v.is_constant or v.is_immutable) + + +def _is_constant_var(v: Variable) -> bool: + if isinstance(v, StateVariable): + return v.is_constant + return False + + +# https://solidity.readthedocs.io/en/v0.5.2/contracts.html#constant-state-variables +valid_solidity_function = [ + SolidityFunction("keccak256()"), + SolidityFunction("keccak256(bytes)"), + SolidityFunction("sha256()"), + SolidityFunction("sha256(bytes)"), + SolidityFunction("ripemd160()"), + SolidityFunction("ripemd160(bytes)"), + SolidityFunction("ecrecover(bytes32,uint8,bytes32,bytes32)"), + SolidityFunction("addmod(uint256,uint256,uint256)"), + SolidityFunction("mulmod(uint256,uint256,uint256)"), +] + + +def _constant_initial_expression(v: Variable) -> bool: + if not v.expression: + return True + + # B b = new B(); b cannot be constant, so filter out and recommend it be immutable + if isinstance(v.expression, CallExpression) and isinstance(v.expression.called, NewContract): + return False + + export = ExportValues(v.expression) + values = export.result() + if not values: + return True + + return all((val in valid_solidity_function or _is_constant_var(val) for val in values)) + + +class UnchangedStateVariables: + """ + Find state variables that could be declared as constant or immutable (not written after deployment). + """ + + def __init__(self, compilation_unit: SlitherCompilationUnit): + self.compilation_unit = compilation_unit + self._constant_candidates: List[StateVariable] = [] + self._immutable_candidates: List[StateVariable] = [] + + @property + def immutable_candidates(self) -> List[StateVariable]: + """Return the immutable candidates""" + return self._immutable_candidates + + @property + def constant_candidates(self) -> List[StateVariable]: + """Return the constant candidates""" + return self._constant_candidates + + def detect(self): + """Detect state variables that could be constant or immutable""" + for c in self.compilation_unit.contracts_derived: + variables = [] + functions = [] + + variables.append(c.state_variables) + functions.append(c.all_functions_called) + + valid_candidates: Set[StateVariable] = { + item for sublist in variables for item in sublist if _valid_candidate(item) + } + + all_functions: List[Function] = list( + {item1 for sublist in functions for item1 in sublist if isinstance(item1, Function)} + ) + + variables_written = [] + constructor_variables_written = [] + variables_initialized = [] + for f in all_functions: + if f.is_constructor_variables: + variables_initialized.extend(f.state_variables_written) + elif f.is_constructor: + constructor_variables_written.extend(f.state_variables_written) + else: + variables_written.extend(f.state_variables_written) + + for v in valid_candidates: + if v not in variables_written: + if _constant_initial_expression(v) and v not in constructor_variables_written: + self.constant_candidates.append(v) + + elif ( + v in constructor_variables_written or v in variables_initialized + ) and version.parse(self.compilation_unit.solc_version) >= version.parse( + "0.6.5" + ): + self.immutable_candidates.append(v) diff --git a/slither/formatters/variables/possible_const_state_variables.py b/slither/formatters/variables/unchanged_state_variables.py similarity index 94% rename from slither/formatters/variables/possible_const_state_variables.py rename to slither/formatters/variables/unchanged_state_variables.py index 88f92b841..c7c8bf003 100644 --- a/slither/formatters/variables/possible_const_state_variables.py +++ b/slither/formatters/variables/unchanged_state_variables.py @@ -5,7 +5,7 @@ from slither.formatters.exceptions import FormatError, FormatImpossible from slither.formatters.utils.patches import create_patch -def custom_format(compilation_unit: SlitherCompilationUnit, result): +def custom_format(compilation_unit: SlitherCompilationUnit, result, attribute: str) -> None: elements = result["elements"] for element in elements: @@ -15,14 +15,14 @@ def custom_format(compilation_unit: SlitherCompilationUnit, result): contract = scope.get_contract_from_name(contract_name) var = contract.get_state_variable_from_name(element["name"]) if not var.expression: - raise FormatImpossible(f"{var.name} is uninitialized and cannot become constant.") + raise FormatImpossible(f"{var.name} is uninitialized and cannot become {attribute}.") _patch( compilation_unit, result, element["source_mapping"]["filename_absolute"], element["name"], - "constant " + element["name"], + f"{attribute} " + element["name"], element["source_mapping"]["start"], element["source_mapping"]["start"] + element["source_mapping"]["length"], ) diff --git a/slither/tools/slither_format/slither_format.py b/slither/tools/slither_format/slither_format.py index c165b3fbb..3c37313fd 100644 --- a/slither/tools/slither_format/slither_format.py +++ b/slither/tools/slither_format/slither_format.py @@ -9,7 +9,8 @@ from slither.detectors.attributes.incorrect_solc import IncorrectSolc from slither.detectors.attributes.constant_pragma import ConstantPragma from slither.detectors.naming_convention.naming_convention import NamingConvention from slither.detectors.functions.external_function import ExternalFunction -from slither.detectors.variables.possible_const_state_variables import ConstCandidateStateVars +from slither.detectors.variables.could_be_constant import CouldBeConstant +from slither.detectors.variables.could_be_immutable import CouldBeImmutable from slither.detectors.attributes.const_functions_asm import ConstantFunctionsAsm from slither.detectors.attributes.const_functions_state import ConstantFunctionsState from slither.utils.colors import yellow @@ -23,7 +24,8 @@ all_detectors: Dict[str, Type[AbstractDetector]] = { "pragma": ConstantPragma, "naming-convention": NamingConvention, "external-function": ExternalFunction, - "constable-states": ConstCandidateStateVars, + "constable-states": CouldBeConstant, + "immutable-states": CouldBeImmutable, "constant-function-asm": ConstantFunctionsAsm, "constant-functions-state": ConstantFunctionsState, } diff --git a/tests/detectors/constable-states/0.4.25/const_state_variables.sol b/tests/detectors/constable-states/0.4.25/const_state_variables.sol index aed05d97f..45f8cc87a 100644 --- a/tests/detectors/constable-states/0.4.25/const_state_variables.sol +++ b/tests/detectors/constable-states/0.4.25/const_state_variables.sol @@ -44,7 +44,12 @@ contract MyConc{ address not_constant = msg.sender; uint not_constant_2 = getNumber(); uint not_constant_3 = 10 + block.number; + uint not_constant_5; + constructor(uint b) public { + not_constant_5 = b; + } + function getNumber() public returns(uint){ return block.number; } diff --git a/tests/detectors/constable-states/0.4.25/const_state_variables.sol.0.4.25.ConstCandidateStateVars.json b/tests/detectors/constable-states/0.4.25/const_state_variables.sol.0.4.25.CouldBeConstant.json similarity index 91% rename from tests/detectors/constable-states/0.4.25/const_state_variables.sol.0.4.25.ConstCandidateStateVars.json rename to tests/detectors/constable-states/0.4.25/const_state_variables.sol.0.4.25.CouldBeConstant.json index 4ab2cfa83..51a485f5b 100644 --- a/tests/detectors/constable-states/0.4.25/const_state_variables.sol.0.4.25.ConstCandidateStateVars.json +++ b/tests/detectors/constable-states/0.4.25/const_state_variables.sol.0.4.25.CouldBeConstant.json @@ -4,19 +4,19 @@ "elements": [ { "type": "variable", - "name": "myFriendsAddress", + "name": "text2", "source_mapping": { - "start": 132, - "length": 76, + "start": 333, + "length": 20, "filename_relative": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", "is_dependency": false, "lines": [ - 7 + 14 ], "starting_column": 5, - "ending_column": 81 + "ending_column": 25 }, "type_specific_fields": { "parent": { @@ -56,10 +56,10 @@ } } ], - "description": "A.myFriendsAddress (tests/detectors/constable-states/0.4.25/const_state_variables.sol#7) should be constant\n", - "markdown": "[A.myFriendsAddress](tests/detectors/constable-states/0.4.25/const_state_variables.sol#L7) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.4.25/const_state_variables.sol#L7", - "id": "1454db80653b732bf6acbe54ff0ae4707002207a2a8216708c12d61c88a43e5f", + "description": "A.text2 (tests/detectors/constable-states/0.4.25/const_state_variables.sol#14) should be constant \n", + "markdown": "[A.text2](tests/detectors/constable-states/0.4.25/const_state_variables.sol#L14) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.4.25/const_state_variables.sol#L14", + "id": "2f06e04545cea7e7a8998c65d5419f335bf2579a6ce6a832eac9c87392fd5c1a", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -68,19 +68,79 @@ "elements": [ { "type": "variable", - "name": "test", + "name": "mySistersAddress", "source_mapping": { - "start": 237, - "length": 20, + "start": 496, + "length": 76, "filename_relative": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", "is_dependency": false, "lines": [ - 10 + 26 ], "starting_column": 5, - "ending_column": 25 + "ending_column": 81 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "B", + "source_mapping": { + "start": 473, + "length": 271, + "filename_relative": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 24, + 25, + 26, + 27, + 28, + 29, + 30, + 31, + 32, + 33, + 34, + 35, + 36, + 37 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "B.mySistersAddress (tests/detectors/constable-states/0.4.25/const_state_variables.sol#26) should be constant \n", + "markdown": "[B.mySistersAddress](tests/detectors/constable-states/0.4.25/const_state_variables.sol#L26) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.4.25/const_state_variables.sol#L26", + "id": "3b5bff93954a48a79387e7981e8c45d78edc575a0988a10f1c7f439b9f930539", + "check": "constable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "myFriendsAddress", + "source_mapping": { + "start": 132, + "length": 76, + "filename_relative": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 7 + ], + "starting_column": 5, + "ending_column": 81 }, "type_specific_fields": { "parent": { @@ -120,10 +180,10 @@ } } ], - "description": "A.test (tests/detectors/constable-states/0.4.25/const_state_variables.sol#10) should be constant\n", - "markdown": "[A.test](tests/detectors/constable-states/0.4.25/const_state_variables.sol#L10) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.4.25/const_state_variables.sol#L10", - "id": "5d9e3fb413322b71a93e90f7e89bd8c83cd4884d577d039598c681fe9db38b1d", + "description": "A.myFriendsAddress (tests/detectors/constable-states/0.4.25/const_state_variables.sol#7) should be constant \n", + "markdown": "[A.myFriendsAddress](tests/detectors/constable-states/0.4.25/const_state_variables.sol#L7) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.4.25/const_state_variables.sol#L7", + "id": "52fd72f6870c4b504d1bcf9fb44249658e2077474d66208a33a47d2668b8db49", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -132,19 +192,19 @@ "elements": [ { "type": "variable", - "name": "should_be_constant_2", + "name": "should_be_constant", "source_mapping": { - "start": 841, - "length": 33, + "start": 793, + "length": 42, "filename_relative": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", "is_dependency": false, "lines": [ - 43 + 42 ], "starting_column": 5, - "ending_column": 38 + "ending_column": 47 }, "type_specific_fields": { "parent": { @@ -152,7 +212,7 @@ "name": "MyConc", "source_mapping": { "start": 746, - "length": 342, + "length": 423, "filename_relative": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", @@ -171,7 +231,12 @@ 49, 50, 51, - 52 + 52, + 53, + 54, + 55, + 56, + 57 ], "starting_column": 1, "ending_column": 2 @@ -180,70 +245,10 @@ } } ], - "description": "MyConc.should_be_constant_2 (tests/detectors/constable-states/0.4.25/const_state_variables.sol#43) should be constant\n", - "markdown": "[MyConc.should_be_constant_2](tests/detectors/constable-states/0.4.25/const_state_variables.sol#L43) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.4.25/const_state_variables.sol#L43", - "id": "9a48a4122de1a6a4774a9f1e0d4917bd0fa08f17b4af41b86ba07689e51bf711", - "check": "constable-states", - "impact": "Optimization", - "confidence": "High" - }, - { - "elements": [ - { - "type": "variable", - "name": "mySistersAddress", - "source_mapping": { - "start": 496, - "length": 76, - "filename_relative": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", - "filename_absolute": "/GENERIC_PATH", - "filename_short": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", - "is_dependency": false, - "lines": [ - 26 - ], - "starting_column": 5, - "ending_column": 81 - }, - "type_specific_fields": { - "parent": { - "type": "contract", - "name": "B", - "source_mapping": { - "start": 473, - "length": 271, - "filename_relative": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", - "filename_absolute": "/GENERIC_PATH", - "filename_short": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", - "is_dependency": false, - "lines": [ - 24, - 25, - 26, - 27, - 28, - 29, - 30, - 31, - 32, - 33, - 34, - 35, - 36, - 37 - ], - "starting_column": 1, - "ending_column": 2 - } - } - } - } - ], - "description": "B.mySistersAddress (tests/detectors/constable-states/0.4.25/const_state_variables.sol#26) should be constant\n", - "markdown": "[B.mySistersAddress](tests/detectors/constable-states/0.4.25/const_state_variables.sol#L26) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.4.25/const_state_variables.sol#L26", - "id": "bee93a722c8eae4a48aade67d8ef537d84c106f48fc9eb738c795fce10d3bc63", + "description": "MyConc.should_be_constant (tests/detectors/constable-states/0.4.25/const_state_variables.sol#42) should be constant \n", + "markdown": "[MyConc.should_be_constant](tests/detectors/constable-states/0.4.25/const_state_variables.sol#L42) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.4.25/const_state_variables.sol#L42", + "id": "8d08797efc8230b480ec669c7e2bf53c3b3d16bc59bf7770934b34fd892934f8", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -252,19 +257,19 @@ "elements": [ { "type": "variable", - "name": "should_be_constant", + "name": "should_be_constant_2", "source_mapping": { - "start": 793, - "length": 42, + "start": 841, + "length": 33, "filename_relative": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", "is_dependency": false, "lines": [ - 42 + 43 ], "starting_column": 5, - "ending_column": 47 + "ending_column": 38 }, "type_specific_fields": { "parent": { @@ -272,7 +277,7 @@ "name": "MyConc", "source_mapping": { "start": 746, - "length": 342, + "length": 423, "filename_relative": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", @@ -291,7 +296,12 @@ 49, 50, 51, - 52 + 52, + 53, + 54, + 55, + 56, + 57 ], "starting_column": 1, "ending_column": 2 @@ -300,10 +310,10 @@ } } ], - "description": "MyConc.should_be_constant (tests/detectors/constable-states/0.4.25/const_state_variables.sol#42) should be constant\n", - "markdown": "[MyConc.should_be_constant](tests/detectors/constable-states/0.4.25/const_state_variables.sol#L42) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.4.25/const_state_variables.sol#L42", - "id": "cbcafa2a3efba4d21ac1b51b4b823e5082d556bc3d6cf3fd2ab3188f9f218fc1", + "description": "MyConc.should_be_constant_2 (tests/detectors/constable-states/0.4.25/const_state_variables.sol#43) should be constant \n", + "markdown": "[MyConc.should_be_constant_2](tests/detectors/constable-states/0.4.25/const_state_variables.sol#L43) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.4.25/const_state_variables.sol#L43", + "id": "d08c6d1e331083b42c45c222691dd1e6d880814c66d114971875337ca61ba9c9", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -312,16 +322,16 @@ "elements": [ { "type": "variable", - "name": "text2", + "name": "test", "source_mapping": { - "start": 333, + "start": 237, "length": 20, "filename_relative": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.4.25/const_state_variables.sol", "is_dependency": false, "lines": [ - 14 + 10 ], "starting_column": 5, "ending_column": 25 @@ -364,10 +374,10 @@ } } ], - "description": "A.text2 (tests/detectors/constable-states/0.4.25/const_state_variables.sol#14) should be constant\n", - "markdown": "[A.text2](tests/detectors/constable-states/0.4.25/const_state_variables.sol#L14) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.4.25/const_state_variables.sol#L14", - "id": "df11e6201c4558a8c5cd90b55b134b9ca8f07203b2264d3aa93bd7745e8cb4ba", + "description": "A.test (tests/detectors/constable-states/0.4.25/const_state_variables.sol#10) should be constant \n", + "markdown": "[A.test](tests/detectors/constable-states/0.4.25/const_state_variables.sol#L10) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.4.25/const_state_variables.sol#L10", + "id": "e407a1b57b4d25949ef7c4e6d97197605857099a94774a9c7a848d7dd3463668", "check": "constable-states", "impact": "Optimization", "confidence": "High" diff --git a/tests/detectors/constable-states/0.5.16/const_state_variables.sol b/tests/detectors/constable-states/0.5.16/const_state_variables.sol index aed05d97f..8fd1ca808 100644 --- a/tests/detectors/constable-states/0.5.16/const_state_variables.sol +++ b/tests/detectors/constable-states/0.5.16/const_state_variables.sol @@ -41,10 +41,16 @@ contract MyConc{ uint constant A = 1; bytes32 should_be_constant = sha256('abc'); uint should_be_constant_2 = A + 1; + B should_be_constant_3 = B(address(0)); address not_constant = msg.sender; uint not_constant_2 = getNumber(); uint not_constant_3 = 10 + block.number; + uint not_constant_5; + constructor(uint b) public { + not_constant_5 = b; + } + function getNumber() public returns(uint){ return block.number; } diff --git a/tests/detectors/constable-states/0.5.16/const_state_variables.sol.0.5.16.ConstCandidateStateVars.json b/tests/detectors/constable-states/0.5.16/const_state_variables.sol.0.5.16.CouldBeConstant.json similarity index 78% rename from tests/detectors/constable-states/0.5.16/const_state_variables.sol.0.5.16.ConstCandidateStateVars.json rename to tests/detectors/constable-states/0.5.16/const_state_variables.sol.0.5.16.CouldBeConstant.json index 9b435a4a8..f9ed17eb1 100644 --- a/tests/detectors/constable-states/0.5.16/const_state_variables.sol.0.5.16.ConstCandidateStateVars.json +++ b/tests/detectors/constable-states/0.5.16/const_state_variables.sol.0.5.16.CouldBeConstant.json @@ -4,50 +4,52 @@ "elements": [ { "type": "variable", - "name": "myFriendsAddress", + "name": "should_be_constant_3", "source_mapping": { - "start": 132, - "length": 76, + "start": 880, + "length": 38, "filename_relative": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "is_dependency": false, "lines": [ - 7 + 44 ], "starting_column": 5, - "ending_column": 81 + "ending_column": 43 }, "type_specific_fields": { "parent": { "type": "contract", - "name": "A", + "name": "MyConc", "source_mapping": { - "start": 29, - "length": 441, + "start": 746, + "length": 467, "filename_relative": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "is_dependency": false, "lines": [ - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 17, - 18, - 19, - 20, - 21 + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56, + 57, + 58 ], "starting_column": 1, "ending_column": 2 @@ -56,10 +58,10 @@ } } ], - "description": "A.myFriendsAddress (tests/detectors/constable-states/0.5.16/const_state_variables.sol#7) should be constant\n", - "markdown": "[A.myFriendsAddress](tests/detectors/constable-states/0.5.16/const_state_variables.sol#L7) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.5.16/const_state_variables.sol#L7", - "id": "1454db80653b732bf6acbe54ff0ae4707002207a2a8216708c12d61c88a43e5f", + "description": "MyConc.should_be_constant_3 (tests/detectors/constable-states/0.5.16/const_state_variables.sol#44) should be constant \n", + "markdown": "[MyConc.should_be_constant_3](tests/detectors/constable-states/0.5.16/const_state_variables.sol#L44) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.5.16/const_state_variables.sol#L44", + "id": "29247b0a9939e854ad51bf3b2f58705156aa8b7e446e646b1832467d362b5b3e", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -68,16 +70,16 @@ "elements": [ { "type": "variable", - "name": "test", + "name": "text2", "source_mapping": { - "start": 237, + "start": 333, "length": 20, "filename_relative": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "is_dependency": false, "lines": [ - 10 + 14 ], "starting_column": 5, "ending_column": 25 @@ -120,10 +122,10 @@ } } ], - "description": "A.test (tests/detectors/constable-states/0.5.16/const_state_variables.sol#10) should be constant\n", - "markdown": "[A.test](tests/detectors/constable-states/0.5.16/const_state_variables.sol#L10) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.5.16/const_state_variables.sol#L10", - "id": "5d9e3fb413322b71a93e90f7e89bd8c83cd4884d577d039598c681fe9db38b1d", + "description": "A.text2 (tests/detectors/constable-states/0.5.16/const_state_variables.sol#14) should be constant \n", + "markdown": "[A.text2](tests/detectors/constable-states/0.5.16/const_state_variables.sol#L14) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.5.16/const_state_variables.sol#L14", + "id": "2f06e04545cea7e7a8998c65d5419f335bf2579a6ce6a832eac9c87392fd5c1a", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -132,46 +134,46 @@ "elements": [ { "type": "variable", - "name": "should_be_constant_2", + "name": "mySistersAddress", "source_mapping": { - "start": 841, - "length": 33, + "start": 496, + "length": 76, "filename_relative": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "is_dependency": false, "lines": [ - 43 + 26 ], "starting_column": 5, - "ending_column": 38 + "ending_column": 81 }, "type_specific_fields": { "parent": { "type": "contract", - "name": "MyConc", + "name": "B", "source_mapping": { - "start": 746, - "length": 342, + "start": 473, + "length": 271, "filename_relative": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "is_dependency": false, "lines": [ - 39, - 40, - 41, - 42, - 43, - 44, - 45, - 46, - 47, - 48, - 49, - 50, - 51, - 52 + 24, + 25, + 26, + 27, + 28, + 29, + 30, + 31, + 32, + 33, + 34, + 35, + 36, + 37 ], "starting_column": 1, "ending_column": 2 @@ -180,10 +182,10 @@ } } ], - "description": "MyConc.should_be_constant_2 (tests/detectors/constable-states/0.5.16/const_state_variables.sol#43) should be constant\n", - "markdown": "[MyConc.should_be_constant_2](tests/detectors/constable-states/0.5.16/const_state_variables.sol#L43) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.5.16/const_state_variables.sol#L43", - "id": "9a48a4122de1a6a4774a9f1e0d4917bd0fa08f17b4af41b86ba07689e51bf711", + "description": "B.mySistersAddress (tests/detectors/constable-states/0.5.16/const_state_variables.sol#26) should be constant \n", + "markdown": "[B.mySistersAddress](tests/detectors/constable-states/0.5.16/const_state_variables.sol#L26) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.5.16/const_state_variables.sol#L26", + "id": "3b5bff93954a48a79387e7981e8c45d78edc575a0988a10f1c7f439b9f930539", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -192,16 +194,16 @@ "elements": [ { "type": "variable", - "name": "mySistersAddress", + "name": "myFriendsAddress", "source_mapping": { - "start": 496, + "start": 132, "length": 76, "filename_relative": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "is_dependency": false, "lines": [ - 26 + 7 ], "starting_column": 5, "ending_column": 81 @@ -209,29 +211,33 @@ "type_specific_fields": { "parent": { "type": "contract", - "name": "B", + "name": "A", "source_mapping": { - "start": 473, - "length": 271, + "start": 29, + "length": 441, "filename_relative": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "is_dependency": false, "lines": [ - 24, - 25, - 26, - 27, - 28, - 29, - 30, - 31, - 32, - 33, - 34, - 35, - 36, - 37 + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21 ], "starting_column": 1, "ending_column": 2 @@ -240,10 +246,10 @@ } } ], - "description": "B.mySistersAddress (tests/detectors/constable-states/0.5.16/const_state_variables.sol#26) should be constant\n", - "markdown": "[B.mySistersAddress](tests/detectors/constable-states/0.5.16/const_state_variables.sol#L26) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.5.16/const_state_variables.sol#L26", - "id": "bee93a722c8eae4a48aade67d8ef537d84c106f48fc9eb738c795fce10d3bc63", + "description": "A.myFriendsAddress (tests/detectors/constable-states/0.5.16/const_state_variables.sol#7) should be constant \n", + "markdown": "[A.myFriendsAddress](tests/detectors/constable-states/0.5.16/const_state_variables.sol#L7) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.5.16/const_state_variables.sol#L7", + "id": "52fd72f6870c4b504d1bcf9fb44249658e2077474d66208a33a47d2668b8db49", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -272,7 +278,7 @@ "name": "MyConc", "source_mapping": { "start": 746, - "length": 342, + "length": 467, "filename_relative": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", @@ -291,7 +297,13 @@ 49, 50, 51, - 52 + 52, + 53, + 54, + 55, + 56, + 57, + 58 ], "starting_column": 1, "ending_column": 2 @@ -300,10 +312,10 @@ } } ], - "description": "MyConc.should_be_constant (tests/detectors/constable-states/0.5.16/const_state_variables.sol#42) should be constant\n", - "markdown": "[MyConc.should_be_constant](tests/detectors/constable-states/0.5.16/const_state_variables.sol#L42) should be constant\n", + "description": "MyConc.should_be_constant (tests/detectors/constable-states/0.5.16/const_state_variables.sol#42) should be constant \n", + "markdown": "[MyConc.should_be_constant](tests/detectors/constable-states/0.5.16/const_state_variables.sol#L42) should be constant \n", "first_markdown_element": "tests/detectors/constable-states/0.5.16/const_state_variables.sol#L42", - "id": "cbcafa2a3efba4d21ac1b51b4b823e5082d556bc3d6cf3fd2ab3188f9f218fc1", + "id": "8d08797efc8230b480ec669c7e2bf53c3b3d16bc59bf7770934b34fd892934f8", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -312,16 +324,82 @@ "elements": [ { "type": "variable", - "name": "text2", + "name": "should_be_constant_2", "source_mapping": { - "start": 333, + "start": 841, + "length": 33, + "filename_relative": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 43 + ], + "starting_column": 5, + "ending_column": 38 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "MyConc", + "source_mapping": { + "start": 746, + "length": 467, + "filename_relative": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56, + 57, + 58 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "MyConc.should_be_constant_2 (tests/detectors/constable-states/0.5.16/const_state_variables.sol#43) should be constant \n", + "markdown": "[MyConc.should_be_constant_2](tests/detectors/constable-states/0.5.16/const_state_variables.sol#L43) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.5.16/const_state_variables.sol#L43", + "id": "d08c6d1e331083b42c45c222691dd1e6d880814c66d114971875337ca61ba9c9", + "check": "constable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "test", + "source_mapping": { + "start": 237, "length": 20, "filename_relative": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.5.16/const_state_variables.sol", "is_dependency": false, "lines": [ - 14 + 10 ], "starting_column": 5, "ending_column": 25 @@ -364,10 +442,10 @@ } } ], - "description": "A.text2 (tests/detectors/constable-states/0.5.16/const_state_variables.sol#14) should be constant\n", - "markdown": "[A.text2](tests/detectors/constable-states/0.5.16/const_state_variables.sol#L14) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.5.16/const_state_variables.sol#L14", - "id": "df11e6201c4558a8c5cd90b55b134b9ca8f07203b2264d3aa93bd7745e8cb4ba", + "description": "A.test (tests/detectors/constable-states/0.5.16/const_state_variables.sol#10) should be constant \n", + "markdown": "[A.test](tests/detectors/constable-states/0.5.16/const_state_variables.sol#L10) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.5.16/const_state_variables.sol#L10", + "id": "e407a1b57b4d25949ef7c4e6d97197605857099a94774a9c7a848d7dd3463668", "check": "constable-states", "impact": "Optimization", "confidence": "High" diff --git a/tests/detectors/constable-states/0.6.11/const_state_variables.sol b/tests/detectors/constable-states/0.6.11/const_state_variables.sol index 3b0c1a436..17548f46f 100644 --- a/tests/detectors/constable-states/0.6.11/const_state_variables.sol +++ b/tests/detectors/constable-states/0.6.11/const_state_variables.sol @@ -1,5 +1,3 @@ -//pragma solidity ^0.4.24; - contract A { @@ -36,17 +34,46 @@ contract B is A { } } -contract MyConc{ +contract Bad { uint constant A = 1; bytes32 should_be_constant = sha256('abc'); uint should_be_constant_2 = A + 1; - address not_constant = msg.sender; - uint not_constant_2 = getNumber(); - uint not_constant_3 = 10 + block.number; + B should_be_constant_3 = B(address(0)); + address should_be_immutable = msg.sender; + uint should_be_immutable_2 = getNumber(); + uint should_be_immutable_3 = 10 + block.number; + B should_be_immutable_4 = new B(); + uint should_be_immutable_5; + constructor(uint b) public { + should_be_immutable_5 = b; + } + function getNumber() public returns(uint){ return block.number; } } + +contract Good { + + uint constant A = 1; + bytes32 constant should_be_constant = sha256('abc'); + uint constant should_be_constant_2 = A + 1; + B constant should_be_constant_3 = B(address(0)); + address immutable should_be_immutable = msg.sender; + uint immutable should_be_immutable_2 = getNumber(); + uint immutable should_be_immutable_3 = 10 + block.number; + B immutable should_be_immutable_4 = new B(); + uint immutable should_be_immutable_5; + + constructor(uint b) public { + should_be_immutable_5 = b; + } + + function getNumber() public returns(uint){ + return block.number; + } + +} \ No newline at end of file diff --git a/tests/detectors/constable-states/0.6.11/const_state_variables.sol.0.6.11.ConstCandidateStateVars.json b/tests/detectors/constable-states/0.6.11/const_state_variables.sol.0.6.11.CouldBeConstant.json similarity index 67% rename from tests/detectors/constable-states/0.6.11/const_state_variables.sol.0.6.11.ConstCandidateStateVars.json rename to tests/detectors/constable-states/0.6.11/const_state_variables.sol.0.6.11.CouldBeConstant.json index 3c7812877..ac3608f81 100644 --- a/tests/detectors/constable-states/0.6.11/const_state_variables.sol.0.6.11.ConstCandidateStateVars.json +++ b/tests/detectors/constable-states/0.6.11/const_state_variables.sol.0.6.11.CouldBeConstant.json @@ -4,32 +4,34 @@ "elements": [ { "type": "variable", - "name": "myFriendsAddress", + "name": "text2", "source_mapping": { - "start": 132, - "length": 76, + "start": 305, + "length": 20, "filename_relative": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "is_dependency": false, "lines": [ - 7 + 12 ], "starting_column": 5, - "ending_column": 81 + "ending_column": 25 }, "type_specific_fields": { "parent": { "type": "contract", "name": "A", "source_mapping": { - "start": 29, + "start": 1, "length": 441, "filename_relative": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "is_dependency": false, "lines": [ + 2, + 3, 4, 5, 6, @@ -45,9 +47,7 @@ 16, 17, 18, - 19, - 20, - 21 + 19 ], "starting_column": 1, "ending_column": 2 @@ -56,10 +56,10 @@ } } ], - "description": "A.myFriendsAddress (tests/detectors/constable-states/0.6.11/const_state_variables.sol#7) should be constant\n", - "markdown": "[A.myFriendsAddress](tests/detectors/constable-states/0.6.11/const_state_variables.sol#L7) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.6.11/const_state_variables.sol#L7", - "id": "1454db80653b732bf6acbe54ff0ae4707002207a2a8216708c12d61c88a43e5f", + "description": "A.text2 (tests/detectors/constable-states/0.6.11/const_state_variables.sol#12) should be constant \n", + "markdown": "[A.text2](tests/detectors/constable-states/0.6.11/const_state_variables.sol#L12) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.6.11/const_state_variables.sol#L12", + "id": "2f06e04545cea7e7a8998c65d5419f335bf2579a6ce6a832eac9c87392fd5c1a", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -68,50 +68,53 @@ "elements": [ { "type": "variable", - "name": "test", + "name": "should_be_constant_2", "source_mapping": { - "start": 237, - "length": 20, + "start": 811, + "length": 33, "filename_relative": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "is_dependency": false, "lines": [ - 10 + 41 ], "starting_column": 5, - "ending_column": 25 + "ending_column": 38 }, "type_specific_fields": { "parent": { "type": "contract", - "name": "A", + "name": "Bad", "source_mapping": { - "start": 29, - "length": 441, + "start": 718, + "length": 539, "filename_relative": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "is_dependency": false, "lines": [ - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 17, - 18, - 19, - 20, - 21 + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56, + 57 ], "starting_column": 1, "ending_column": 2 @@ -120,10 +123,10 @@ } } ], - "description": "A.test (tests/detectors/constable-states/0.6.11/const_state_variables.sol#10) should be constant\n", - "markdown": "[A.test](tests/detectors/constable-states/0.6.11/const_state_variables.sol#L10) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.6.11/const_state_variables.sol#L10", - "id": "5d9e3fb413322b71a93e90f7e89bd8c83cd4884d577d039598c681fe9db38b1d", + "description": "Bad.should_be_constant_2 (tests/detectors/constable-states/0.6.11/const_state_variables.sol#41) should be constant \n", + "markdown": "[Bad.should_be_constant_2](tests/detectors/constable-states/0.6.11/const_state_variables.sol#L41) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.6.11/const_state_variables.sol#L41", + "id": "3a8b682f7960750cd8228d6cd3d0bb5d7d6f9faaf1a044de2fa7069d8e475af2", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -132,46 +135,46 @@ "elements": [ { "type": "variable", - "name": "should_be_constant_2", + "name": "mySistersAddress", "source_mapping": { - "start": 841, - "length": 33, + "start": 468, + "length": 76, "filename_relative": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "is_dependency": false, "lines": [ - 43 + 24 ], "starting_column": 5, - "ending_column": 38 + "ending_column": 81 }, "type_specific_fields": { "parent": { "type": "contract", - "name": "MyConc", + "name": "B", "source_mapping": { - "start": 746, - "length": 342, + "start": 445, + "length": 271, "filename_relative": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "is_dependency": false, "lines": [ - 39, - 40, - 41, - 42, - 43, - 44, - 45, - 46, - 47, - 48, - 49, - 50, - 51, - 52 + 22, + 23, + 24, + 25, + 26, + 27, + 28, + 29, + 30, + 31, + 32, + 33, + 34, + 35 ], "starting_column": 1, "ending_column": 2 @@ -180,10 +183,10 @@ } } ], - "description": "MyConc.should_be_constant_2 (tests/detectors/constable-states/0.6.11/const_state_variables.sol#43) should be constant\n", - "markdown": "[MyConc.should_be_constant_2](tests/detectors/constable-states/0.6.11/const_state_variables.sol#L43) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.6.11/const_state_variables.sol#L43", - "id": "9a48a4122de1a6a4774a9f1e0d4917bd0fa08f17b4af41b86ba07689e51bf711", + "description": "B.mySistersAddress (tests/detectors/constable-states/0.6.11/const_state_variables.sol#24) should be constant \n", + "markdown": "[B.mySistersAddress](tests/detectors/constable-states/0.6.11/const_state_variables.sol#L24) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.6.11/const_state_variables.sol#L24", + "id": "3b5bff93954a48a79387e7981e8c45d78edc575a0988a10f1c7f439b9f930539", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -192,16 +195,16 @@ "elements": [ { "type": "variable", - "name": "mySistersAddress", + "name": "myFriendsAddress", "source_mapping": { - "start": 496, + "start": 104, "length": 76, "filename_relative": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "is_dependency": false, "lines": [ - 26 + 5 ], "starting_column": 5, "ending_column": 81 @@ -209,29 +212,33 @@ "type_specific_fields": { "parent": { "type": "contract", - "name": "B", + "name": "A", "source_mapping": { - "start": 473, - "length": 271, + "start": 1, + "length": 441, "filename_relative": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "is_dependency": false, "lines": [ - 24, - 25, - 26, - 27, - 28, - 29, - 30, - 31, - 32, - 33, - 34, - 35, - 36, - 37 + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19 ], "starting_column": 1, "ending_column": 2 @@ -240,10 +247,10 @@ } } ], - "description": "B.mySistersAddress (tests/detectors/constable-states/0.6.11/const_state_variables.sol#26) should be constant\n", - "markdown": "[B.mySistersAddress](tests/detectors/constable-states/0.6.11/const_state_variables.sol#L26) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.6.11/const_state_variables.sol#L26", - "id": "bee93a722c8eae4a48aade67d8ef537d84c106f48fc9eb738c795fce10d3bc63", + "description": "A.myFriendsAddress (tests/detectors/constable-states/0.6.11/const_state_variables.sol#5) should be constant \n", + "markdown": "[A.myFriendsAddress](tests/detectors/constable-states/0.6.11/const_state_variables.sol#L5) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.6.11/const_state_variables.sol#L5", + "id": "52fd72f6870c4b504d1bcf9fb44249658e2077474d66208a33a47d2668b8db49", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -254,14 +261,14 @@ "type": "variable", "name": "should_be_constant", "source_mapping": { - "start": 793, + "start": 763, "length": 42, "filename_relative": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "is_dependency": false, "lines": [ - 42 + 40 ], "starting_column": 5, "ending_column": 47 @@ -269,15 +276,84 @@ "type_specific_fields": { "parent": { "type": "contract", - "name": "MyConc", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 539, + "filename_relative": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56, + 57 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_constant (tests/detectors/constable-states/0.6.11/const_state_variables.sol#40) should be constant \n", + "markdown": "[Bad.should_be_constant](tests/detectors/constable-states/0.6.11/const_state_variables.sol#L40) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.6.11/const_state_variables.sol#L40", + "id": "87097c03d57b72ad7c15336eb44e5a30054c50f8daff32e08bc4fbd97852961c", + "check": "constable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_constant_3", + "source_mapping": { + "start": 850, + "length": 38, + "filename_relative": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 42 + ], + "starting_column": 5, + "ending_column": 43 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", "source_mapping": { - "start": 746, - "length": 342, + "start": 718, + "length": 539, "filename_relative": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "is_dependency": false, "lines": [ + 37, + 38, 39, 40, 41, @@ -291,7 +367,12 @@ 49, 50, 51, - 52 + 52, + 53, + 54, + 55, + 56, + 57 ], "starting_column": 1, "ending_column": 2 @@ -300,10 +381,10 @@ } } ], - "description": "MyConc.should_be_constant (tests/detectors/constable-states/0.6.11/const_state_variables.sol#42) should be constant\n", - "markdown": "[MyConc.should_be_constant](tests/detectors/constable-states/0.6.11/const_state_variables.sol#L42) should be constant\n", + "description": "Bad.should_be_constant_3 (tests/detectors/constable-states/0.6.11/const_state_variables.sol#42) should be constant \n", + "markdown": "[Bad.should_be_constant_3](tests/detectors/constable-states/0.6.11/const_state_variables.sol#L42) should be constant \n", "first_markdown_element": "tests/detectors/constable-states/0.6.11/const_state_variables.sol#L42", - "id": "cbcafa2a3efba4d21ac1b51b4b823e5082d556bc3d6cf3fd2ab3188f9f218fc1", + "id": "8e991c1370b1adb10f01f2d7e48f341dee92a98b91b56ccb291d9149d2da97d0", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -312,16 +393,16 @@ "elements": [ { "type": "variable", - "name": "text2", + "name": "test", "source_mapping": { - "start": 333, + "start": 209, "length": 20, "filename_relative": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "is_dependency": false, "lines": [ - 14 + 8 ], "starting_column": 5, "ending_column": 25 @@ -331,13 +412,15 @@ "type": "contract", "name": "A", "source_mapping": { - "start": 29, + "start": 1, "length": 441, "filename_relative": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.6.11/const_state_variables.sol", "is_dependency": false, "lines": [ + 2, + 3, 4, 5, 6, @@ -353,9 +436,7 @@ 16, 17, 18, - 19, - 20, - 21 + 19 ], "starting_column": 1, "ending_column": 2 @@ -364,10 +445,10 @@ } } ], - "description": "A.text2 (tests/detectors/constable-states/0.6.11/const_state_variables.sol#14) should be constant\n", - "markdown": "[A.text2](tests/detectors/constable-states/0.6.11/const_state_variables.sol#L14) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.6.11/const_state_variables.sol#L14", - "id": "df11e6201c4558a8c5cd90b55b134b9ca8f07203b2264d3aa93bd7745e8cb4ba", + "description": "A.test (tests/detectors/constable-states/0.6.11/const_state_variables.sol#8) should be constant \n", + "markdown": "[A.test](tests/detectors/constable-states/0.6.11/const_state_variables.sol#L8) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.6.11/const_state_variables.sol#L8", + "id": "e407a1b57b4d25949ef7c4e6d97197605857099a94774a9c7a848d7dd3463668", "check": "constable-states", "impact": "Optimization", "confidence": "High" diff --git a/tests/detectors/constable-states/0.7.6/const_state_variables.sol b/tests/detectors/constable-states/0.7.6/const_state_variables.sol index 3b0c1a436..297dd6294 100644 --- a/tests/detectors/constable-states/0.7.6/const_state_variables.sol +++ b/tests/detectors/constable-states/0.7.6/const_state_variables.sol @@ -1,5 +1,3 @@ -//pragma solidity ^0.4.24; - contract A { @@ -36,15 +34,43 @@ contract B is A { } } -contract MyConc{ +contract Bad { uint constant A = 1; bytes32 should_be_constant = sha256('abc'); uint should_be_constant_2 = A + 1; - address not_constant = msg.sender; - uint not_constant_2 = getNumber(); - uint not_constant_3 = 10 + block.number; + B should_be_constant_3 = B(address(0)); + address should_be_immutable = msg.sender; + uint should_be_immutable_2 = getNumber(); + uint should_be_immutable_3 = 10 + block.number; + B should_be_immutable_4 = new B(); + uint should_be_immutable_5; + constructor(uint b) { + should_be_immutable_5 = b; + } + + function getNumber() public returns(uint){ + return block.number; + } +} + +contract Good { + + uint constant A = 1; + bytes32 constant should_be_constant = sha256('abc'); + uint constant should_be_constant_2 = A + 1; + B constant should_be_constant_3 = B(address(0)); + address immutable should_be_immutable = msg.sender; + uint immutable should_be_immutable_2 = getNumber(); + uint immutable should_be_immutable_3 = 10 + block.number; + B immutable should_be_immutable_4 = new B(); + uint immutable should_be_immutable_5; + + constructor(uint b) { + should_be_immutable_5 = b; + } + function getNumber() public returns(uint){ return block.number; } diff --git a/tests/detectors/constable-states/0.7.6/const_state_variables.sol.0.7.6.ConstCandidateStateVars.json b/tests/detectors/constable-states/0.7.6/const_state_variables.sol.0.7.6.CouldBeConstant.json similarity index 67% rename from tests/detectors/constable-states/0.7.6/const_state_variables.sol.0.7.6.ConstCandidateStateVars.json rename to tests/detectors/constable-states/0.7.6/const_state_variables.sol.0.7.6.CouldBeConstant.json index 3cb6d44c4..d54beb405 100644 --- a/tests/detectors/constable-states/0.7.6/const_state_variables.sol.0.7.6.ConstCandidateStateVars.json +++ b/tests/detectors/constable-states/0.7.6/const_state_variables.sol.0.7.6.CouldBeConstant.json @@ -4,32 +4,34 @@ "elements": [ { "type": "variable", - "name": "myFriendsAddress", + "name": "text2", "source_mapping": { - "start": 132, - "length": 76, + "start": 305, + "length": 20, "filename_relative": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "is_dependency": false, "lines": [ - 7 + 12 ], "starting_column": 5, - "ending_column": 81 + "ending_column": 25 }, "type_specific_fields": { "parent": { "type": "contract", "name": "A", "source_mapping": { - "start": 29, + "start": 1, "length": 441, "filename_relative": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "is_dependency": false, "lines": [ + 2, + 3, 4, 5, 6, @@ -45,9 +47,7 @@ 16, 17, 18, - 19, - 20, - 21 + 19 ], "starting_column": 1, "ending_column": 2 @@ -56,10 +56,10 @@ } } ], - "description": "A.myFriendsAddress (tests/detectors/constable-states/0.7.6/const_state_variables.sol#7) should be constant\n", - "markdown": "[A.myFriendsAddress](tests/detectors/constable-states/0.7.6/const_state_variables.sol#L7) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.7.6/const_state_variables.sol#L7", - "id": "1454db80653b732bf6acbe54ff0ae4707002207a2a8216708c12d61c88a43e5f", + "description": "A.text2 (tests/detectors/constable-states/0.7.6/const_state_variables.sol#12) should be constant \n", + "markdown": "[A.text2](tests/detectors/constable-states/0.7.6/const_state_variables.sol#L12) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.7.6/const_state_variables.sol#L12", + "id": "2f06e04545cea7e7a8998c65d5419f335bf2579a6ce6a832eac9c87392fd5c1a", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -68,50 +68,52 @@ "elements": [ { "type": "variable", - "name": "test", + "name": "should_be_constant_2", "source_mapping": { - "start": 237, - "length": 20, + "start": 811, + "length": 33, "filename_relative": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "is_dependency": false, "lines": [ - 10 + 41 ], "starting_column": 5, - "ending_column": 25 + "ending_column": 38 }, "type_specific_fields": { "parent": { "type": "contract", - "name": "A", + "name": "Bad", "source_mapping": { - "start": 29, - "length": 441, + "start": 718, + "length": 531, "filename_relative": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "is_dependency": false, "lines": [ - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 17, - 18, - 19, - 20, - 21 + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56 ], "starting_column": 1, "ending_column": 2 @@ -120,10 +122,10 @@ } } ], - "description": "A.test (tests/detectors/constable-states/0.7.6/const_state_variables.sol#10) should be constant\n", - "markdown": "[A.test](tests/detectors/constable-states/0.7.6/const_state_variables.sol#L10) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.7.6/const_state_variables.sol#L10", - "id": "5d9e3fb413322b71a93e90f7e89bd8c83cd4884d577d039598c681fe9db38b1d", + "description": "Bad.should_be_constant_2 (tests/detectors/constable-states/0.7.6/const_state_variables.sol#41) should be constant \n", + "markdown": "[Bad.should_be_constant_2](tests/detectors/constable-states/0.7.6/const_state_variables.sol#L41) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.7.6/const_state_variables.sol#L41", + "id": "3a8b682f7960750cd8228d6cd3d0bb5d7d6f9faaf1a044de2fa7069d8e475af2", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -132,46 +134,46 @@ "elements": [ { "type": "variable", - "name": "should_be_constant_2", + "name": "mySistersAddress", "source_mapping": { - "start": 841, - "length": 33, + "start": 468, + "length": 76, "filename_relative": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "is_dependency": false, "lines": [ - 43 + 24 ], "starting_column": 5, - "ending_column": 38 + "ending_column": 81 }, "type_specific_fields": { "parent": { "type": "contract", - "name": "MyConc", + "name": "B", "source_mapping": { - "start": 746, - "length": 342, + "start": 445, + "length": 271, "filename_relative": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "is_dependency": false, "lines": [ - 39, - 40, - 41, - 42, - 43, - 44, - 45, - 46, - 47, - 48, - 49, - 50, - 51, - 52 + 22, + 23, + 24, + 25, + 26, + 27, + 28, + 29, + 30, + 31, + 32, + 33, + 34, + 35 ], "starting_column": 1, "ending_column": 2 @@ -180,10 +182,10 @@ } } ], - "description": "MyConc.should_be_constant_2 (tests/detectors/constable-states/0.7.6/const_state_variables.sol#43) should be constant\n", - "markdown": "[MyConc.should_be_constant_2](tests/detectors/constable-states/0.7.6/const_state_variables.sol#L43) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.7.6/const_state_variables.sol#L43", - "id": "9a48a4122de1a6a4774a9f1e0d4917bd0fa08f17b4af41b86ba07689e51bf711", + "description": "B.mySistersAddress (tests/detectors/constable-states/0.7.6/const_state_variables.sol#24) should be constant \n", + "markdown": "[B.mySistersAddress](tests/detectors/constable-states/0.7.6/const_state_variables.sol#L24) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.7.6/const_state_variables.sol#L24", + "id": "3b5bff93954a48a79387e7981e8c45d78edc575a0988a10f1c7f439b9f930539", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -192,16 +194,16 @@ "elements": [ { "type": "variable", - "name": "mySistersAddress", + "name": "myFriendsAddress", "source_mapping": { - "start": 496, + "start": 104, "length": 76, "filename_relative": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "is_dependency": false, "lines": [ - 26 + 5 ], "starting_column": 5, "ending_column": 81 @@ -209,29 +211,33 @@ "type_specific_fields": { "parent": { "type": "contract", - "name": "B", + "name": "A", "source_mapping": { - "start": 473, - "length": 271, + "start": 1, + "length": 441, "filename_relative": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "is_dependency": false, "lines": [ - 24, - 25, - 26, - 27, - 28, - 29, - 30, - 31, - 32, - 33, - 34, - 35, - 36, - 37 + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19 ], "starting_column": 1, "ending_column": 2 @@ -240,10 +246,10 @@ } } ], - "description": "B.mySistersAddress (tests/detectors/constable-states/0.7.6/const_state_variables.sol#26) should be constant\n", - "markdown": "[B.mySistersAddress](tests/detectors/constable-states/0.7.6/const_state_variables.sol#L26) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.7.6/const_state_variables.sol#L26", - "id": "bee93a722c8eae4a48aade67d8ef537d84c106f48fc9eb738c795fce10d3bc63", + "description": "A.myFriendsAddress (tests/detectors/constable-states/0.7.6/const_state_variables.sol#5) should be constant \n", + "markdown": "[A.myFriendsAddress](tests/detectors/constable-states/0.7.6/const_state_variables.sol#L5) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.7.6/const_state_variables.sol#L5", + "id": "52fd72f6870c4b504d1bcf9fb44249658e2077474d66208a33a47d2668b8db49", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -254,14 +260,14 @@ "type": "variable", "name": "should_be_constant", "source_mapping": { - "start": 793, + "start": 763, "length": 42, "filename_relative": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "is_dependency": false, "lines": [ - 42 + 40 ], "starting_column": 5, "ending_column": 47 @@ -269,15 +275,83 @@ "type_specific_fields": { "parent": { "type": "contract", - "name": "MyConc", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 531, + "filename_relative": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_constant (tests/detectors/constable-states/0.7.6/const_state_variables.sol#40) should be constant \n", + "markdown": "[Bad.should_be_constant](tests/detectors/constable-states/0.7.6/const_state_variables.sol#L40) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.7.6/const_state_variables.sol#L40", + "id": "87097c03d57b72ad7c15336eb44e5a30054c50f8daff32e08bc4fbd97852961c", + "check": "constable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_constant_3", + "source_mapping": { + "start": 850, + "length": 38, + "filename_relative": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 42 + ], + "starting_column": 5, + "ending_column": 43 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", "source_mapping": { - "start": 746, - "length": 342, + "start": 718, + "length": 531, "filename_relative": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "is_dependency": false, "lines": [ + 37, + 38, 39, 40, 41, @@ -291,7 +365,11 @@ 49, 50, 51, - 52 + 52, + 53, + 54, + 55, + 56 ], "starting_column": 1, "ending_column": 2 @@ -300,10 +378,10 @@ } } ], - "description": "MyConc.should_be_constant (tests/detectors/constable-states/0.7.6/const_state_variables.sol#42) should be constant\n", - "markdown": "[MyConc.should_be_constant](tests/detectors/constable-states/0.7.6/const_state_variables.sol#L42) should be constant\n", + "description": "Bad.should_be_constant_3 (tests/detectors/constable-states/0.7.6/const_state_variables.sol#42) should be constant \n", + "markdown": "[Bad.should_be_constant_3](tests/detectors/constable-states/0.7.6/const_state_variables.sol#L42) should be constant \n", "first_markdown_element": "tests/detectors/constable-states/0.7.6/const_state_variables.sol#L42", - "id": "cbcafa2a3efba4d21ac1b51b4b823e5082d556bc3d6cf3fd2ab3188f9f218fc1", + "id": "8e991c1370b1adb10f01f2d7e48f341dee92a98b91b56ccb291d9149d2da97d0", "check": "constable-states", "impact": "Optimization", "confidence": "High" @@ -312,16 +390,16 @@ "elements": [ { "type": "variable", - "name": "text2", + "name": "test", "source_mapping": { - "start": 333, + "start": 209, "length": 20, "filename_relative": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "is_dependency": false, "lines": [ - 14 + 8 ], "starting_column": 5, "ending_column": 25 @@ -331,13 +409,15 @@ "type": "contract", "name": "A", "source_mapping": { - "start": 29, + "start": 1, "length": 441, "filename_relative": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "filename_absolute": "/GENERIC_PATH", "filename_short": "tests/detectors/constable-states/0.7.6/const_state_variables.sol", "is_dependency": false, "lines": [ + 2, + 3, 4, 5, 6, @@ -353,9 +433,7 @@ 16, 17, 18, - 19, - 20, - 21 + 19 ], "starting_column": 1, "ending_column": 2 @@ -364,10 +442,10 @@ } } ], - "description": "A.text2 (tests/detectors/constable-states/0.7.6/const_state_variables.sol#14) should be constant\n", - "markdown": "[A.text2](tests/detectors/constable-states/0.7.6/const_state_variables.sol#L14) should be constant\n", - "first_markdown_element": "tests/detectors/constable-states/0.7.6/const_state_variables.sol#L14", - "id": "df11e6201c4558a8c5cd90b55b134b9ca8f07203b2264d3aa93bd7745e8cb4ba", + "description": "A.test (tests/detectors/constable-states/0.7.6/const_state_variables.sol#8) should be constant \n", + "markdown": "[A.test](tests/detectors/constable-states/0.7.6/const_state_variables.sol#L8) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.7.6/const_state_variables.sol#L8", + "id": "e407a1b57b4d25949ef7c4e6d97197605857099a94774a9c7a848d7dd3463668", "check": "constable-states", "impact": "Optimization", "confidence": "High" diff --git a/tests/detectors/constable-states/0.8.0/const_state_variables.sol b/tests/detectors/constable-states/0.8.0/const_state_variables.sol new file mode 100644 index 000000000..f405a1587 --- /dev/null +++ b/tests/detectors/constable-states/0.8.0/const_state_variables.sol @@ -0,0 +1,78 @@ + +contract A { + + address constant public MY_ADDRESS = 0xE0f5206BBD039e7b0592d8918820024e2a7437b9; + address public myFriendsAddress = 0xc0ffee254729296a45a3885639AC7E10F9d54979; + + uint public used; + uint public test = 5; + + uint constant X = 32**22 + 8; + string constant TEXT1 = "abc"; + string text2 = "xyz"; + + function setUsed() public { + if (msg.sender == MY_ADDRESS) { + used = test; + } + } +} + + +contract B is A { + + address public mySistersAddress = 0x999999cf1046e68e36E1aA2E0E07105eDDD1f08E; + + fallback () external { + used = 0; + } + + function setUsed(uint a) public { + if (msg.sender == MY_ADDRESS) { + used = a; + } + } +} + +contract Bad { + + uint constant A = 1; + bytes32 should_be_constant = sha256('abc'); + uint should_be_constant_2 = A + 1; + B should_be_constant_3 = B(address(0)); + address should_be_immutable = msg.sender; + uint should_be_immutable_2 = getNumber(); + uint should_be_immutable_3 = 10 + block.number; + uint should_be_immutable_5; + + constructor(uint b) { + should_be_immutable_5 = b; + } + + function getNumber() public returns(uint){ + return block.number; + } + +} + +contract Good { + + uint constant A = 1; + bytes32 constant should_be_constant = sha256('abc'); + uint constant should_be_constant_2 = A + 1; + B constant should_be_constant_3 = B(address(0)); + address immutable should_be_immutable = msg.sender; + uint immutable should_be_immutable_2 = getNumber(); + uint immutable should_be_immutable_3 = 10 + block.number; + B immutable should_be_immutable_4 = new B(); + uint immutable should_be_immutable_5; + + constructor(uint b) { + should_be_immutable_5 = b; + } + + function getNumber() public returns(uint){ + return block.number; + } + +} \ No newline at end of file diff --git a/tests/detectors/constable-states/0.8.0/const_state_variables.sol.0.8.0.CouldBeConstant.json b/tests/detectors/constable-states/0.8.0/const_state_variables.sol.0.8.0.CouldBeConstant.json new file mode 100644 index 000000000..7febfd637 --- /dev/null +++ b/tests/detectors/constable-states/0.8.0/const_state_variables.sol.0.8.0.CouldBeConstant.json @@ -0,0 +1,454 @@ +[ + [ + { + "elements": [ + { + "type": "variable", + "name": "text2", + "source_mapping": { + "start": 305, + "length": 20, + "filename_relative": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 12 + ], + "starting_column": 5, + "ending_column": 25 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "A", + "source_mapping": { + "start": 1, + "length": 441, + "filename_relative": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "A.text2 (tests/detectors/constable-states/0.8.0/const_state_variables.sol#12) should be constant \n", + "markdown": "[A.text2](tests/detectors/constable-states/0.8.0/const_state_variables.sol#L12) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.8.0/const_state_variables.sol#L12", + "id": "2f06e04545cea7e7a8998c65d5419f335bf2579a6ce6a832eac9c87392fd5c1a", + "check": "constable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_constant_2", + "source_mapping": { + "start": 811, + "length": 33, + "filename_relative": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 41 + ], + "starting_column": 5, + "ending_column": 38 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 493, + "filename_relative": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_constant_2 (tests/detectors/constable-states/0.8.0/const_state_variables.sol#41) should be constant \n", + "markdown": "[Bad.should_be_constant_2](tests/detectors/constable-states/0.8.0/const_state_variables.sol#L41) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.8.0/const_state_variables.sol#L41", + "id": "3a8b682f7960750cd8228d6cd3d0bb5d7d6f9faaf1a044de2fa7069d8e475af2", + "check": "constable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "mySistersAddress", + "source_mapping": { + "start": 468, + "length": 76, + "filename_relative": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 24 + ], + "starting_column": 5, + "ending_column": 81 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "B", + "source_mapping": { + "start": 445, + "length": 271, + "filename_relative": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 22, + 23, + 24, + 25, + 26, + 27, + 28, + 29, + 30, + 31, + 32, + 33, + 34, + 35 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "B.mySistersAddress (tests/detectors/constable-states/0.8.0/const_state_variables.sol#24) should be constant \n", + "markdown": "[B.mySistersAddress](tests/detectors/constable-states/0.8.0/const_state_variables.sol#L24) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.8.0/const_state_variables.sol#L24", + "id": "3b5bff93954a48a79387e7981e8c45d78edc575a0988a10f1c7f439b9f930539", + "check": "constable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "myFriendsAddress", + "source_mapping": { + "start": 104, + "length": 76, + "filename_relative": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 5 + ], + "starting_column": 5, + "ending_column": 81 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "A", + "source_mapping": { + "start": 1, + "length": 441, + "filename_relative": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "A.myFriendsAddress (tests/detectors/constable-states/0.8.0/const_state_variables.sol#5) should be constant \n", + "markdown": "[A.myFriendsAddress](tests/detectors/constable-states/0.8.0/const_state_variables.sol#L5) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.8.0/const_state_variables.sol#L5", + "id": "52fd72f6870c4b504d1bcf9fb44249658e2077474d66208a33a47d2668b8db49", + "check": "constable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_constant", + "source_mapping": { + "start": 763, + "length": 42, + "filename_relative": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 40 + ], + "starting_column": 5, + "ending_column": 47 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 493, + "filename_relative": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_constant (tests/detectors/constable-states/0.8.0/const_state_variables.sol#40) should be constant \n", + "markdown": "[Bad.should_be_constant](tests/detectors/constable-states/0.8.0/const_state_variables.sol#L40) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.8.0/const_state_variables.sol#L40", + "id": "87097c03d57b72ad7c15336eb44e5a30054c50f8daff32e08bc4fbd97852961c", + "check": "constable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_constant_3", + "source_mapping": { + "start": 850, + "length": 38, + "filename_relative": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 42 + ], + "starting_column": 5, + "ending_column": 43 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 493, + "filename_relative": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_constant_3 (tests/detectors/constable-states/0.8.0/const_state_variables.sol#42) should be constant \n", + "markdown": "[Bad.should_be_constant_3](tests/detectors/constable-states/0.8.0/const_state_variables.sol#L42) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.8.0/const_state_variables.sol#L42", + "id": "8e991c1370b1adb10f01f2d7e48f341dee92a98b91b56ccb291d9149d2da97d0", + "check": "constable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "test", + "source_mapping": { + "start": 209, + "length": 20, + "filename_relative": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 8 + ], + "starting_column": 5, + "ending_column": 25 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "A", + "source_mapping": { + "start": 1, + "length": 441, + "filename_relative": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/constable-states/0.8.0/const_state_variables.sol", + "is_dependency": false, + "lines": [ + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "A.test (tests/detectors/constable-states/0.8.0/const_state_variables.sol#8) should be constant \n", + "markdown": "[A.test](tests/detectors/constable-states/0.8.0/const_state_variables.sol#L8) should be constant \n", + "first_markdown_element": "tests/detectors/constable-states/0.8.0/const_state_variables.sol#L8", + "id": "e407a1b57b4d25949ef7c4e6d97197605857099a94774a9c7a848d7dd3463668", + "check": "constable-states", + "impact": "Optimization", + "confidence": "High" + } + ] +] \ No newline at end of file diff --git a/tests/detectors/constable-states/0.8.0/immutable.sol b/tests/detectors/constable-states/0.8.0/immutable.sol deleted file mode 100644 index aeb9fbfa7..000000000 --- a/tests/detectors/constable-states/0.8.0/immutable.sol +++ /dev/null @@ -1,7 +0,0 @@ -contract C{ - uint immutable v; - - constructor() public{ - v = 0; - } -} diff --git a/tests/detectors/immutable-states/0.4.25/immut_state_variables.sol b/tests/detectors/immutable-states/0.4.25/immut_state_variables.sol new file mode 100644 index 000000000..45f8cc87a --- /dev/null +++ b/tests/detectors/immutable-states/0.4.25/immut_state_variables.sol @@ -0,0 +1,57 @@ +//pragma solidity ^0.4.24; + + +contract A { + + address constant public MY_ADDRESS = 0xE0f5206BBD039e7b0592d8918820024e2a7437b9; + address public myFriendsAddress = 0xc0ffee254729296a45a3885639AC7E10F9d54979; + + uint public used; + uint public test = 5; + + uint constant X = 32**22 + 8; + string constant TEXT1 = "abc"; + string text2 = "xyz"; + + function setUsed() public { + if (msg.sender == MY_ADDRESS) { + used = test; + } + } +} + + +contract B is A { + + address public mySistersAddress = 0x999999cf1046e68e36E1aA2E0E07105eDDD1f08E; + + function () external { + used = 0; + } + + function setUsed(uint a) public { + if (msg.sender == MY_ADDRESS) { + used = a; + } + } +} + +contract MyConc{ + + uint constant A = 1; + bytes32 should_be_constant = sha256('abc'); + uint should_be_constant_2 = A + 1; + address not_constant = msg.sender; + uint not_constant_2 = getNumber(); + uint not_constant_3 = 10 + block.number; + uint not_constant_5; + + constructor(uint b) public { + not_constant_5 = b; + } + + function getNumber() public returns(uint){ + return block.number; + } + +} diff --git a/tests/detectors/constable-states/0.8.0/immutable.sol.0.8.0.ConstCandidateStateVars.json b/tests/detectors/immutable-states/0.4.25/immut_state_variables.sol.0.4.25.CouldBeImmutable.json similarity index 100% rename from tests/detectors/constable-states/0.8.0/immutable.sol.0.8.0.ConstCandidateStateVars.json rename to tests/detectors/immutable-states/0.4.25/immut_state_variables.sol.0.4.25.CouldBeImmutable.json diff --git a/tests/detectors/immutable-states/0.5.16/immut_state_variables.sol b/tests/detectors/immutable-states/0.5.16/immut_state_variables.sol new file mode 100644 index 000000000..8fd1ca808 --- /dev/null +++ b/tests/detectors/immutable-states/0.5.16/immut_state_variables.sol @@ -0,0 +1,58 @@ +//pragma solidity ^0.4.24; + + +contract A { + + address constant public MY_ADDRESS = 0xE0f5206BBD039e7b0592d8918820024e2a7437b9; + address public myFriendsAddress = 0xc0ffee254729296a45a3885639AC7E10F9d54979; + + uint public used; + uint public test = 5; + + uint constant X = 32**22 + 8; + string constant TEXT1 = "abc"; + string text2 = "xyz"; + + function setUsed() public { + if (msg.sender == MY_ADDRESS) { + used = test; + } + } +} + + +contract B is A { + + address public mySistersAddress = 0x999999cf1046e68e36E1aA2E0E07105eDDD1f08E; + + function () external { + used = 0; + } + + function setUsed(uint a) public { + if (msg.sender == MY_ADDRESS) { + used = a; + } + } +} + +contract MyConc{ + + uint constant A = 1; + bytes32 should_be_constant = sha256('abc'); + uint should_be_constant_2 = A + 1; + B should_be_constant_3 = B(address(0)); + address not_constant = msg.sender; + uint not_constant_2 = getNumber(); + uint not_constant_3 = 10 + block.number; + uint not_constant_5; + + constructor(uint b) public { + not_constant_5 = b; + } + + function getNumber() public returns(uint){ + return block.number; + } + +} diff --git a/tests/detectors/immutable-states/0.5.16/immut_state_variables.sol.0.5.16.CouldBeImmutable.json b/tests/detectors/immutable-states/0.5.16/immut_state_variables.sol.0.5.16.CouldBeImmutable.json new file mode 100644 index 000000000..5825bcacc --- /dev/null +++ b/tests/detectors/immutable-states/0.5.16/immut_state_variables.sol.0.5.16.CouldBeImmutable.json @@ -0,0 +1,3 @@ +[ + [] +] \ No newline at end of file diff --git a/tests/detectors/immutable-states/0.6.11/immut_state_variables.sol b/tests/detectors/immutable-states/0.6.11/immut_state_variables.sol new file mode 100644 index 000000000..17548f46f --- /dev/null +++ b/tests/detectors/immutable-states/0.6.11/immut_state_variables.sol @@ -0,0 +1,79 @@ + +contract A { + + address constant public MY_ADDRESS = 0xE0f5206BBD039e7b0592d8918820024e2a7437b9; + address public myFriendsAddress = 0xc0ffee254729296a45a3885639AC7E10F9d54979; + + uint public used; + uint public test = 5; + + uint constant X = 32**22 + 8; + string constant TEXT1 = "abc"; + string text2 = "xyz"; + + function setUsed() public { + if (msg.sender == MY_ADDRESS) { + used = test; + } + } +} + + +contract B is A { + + address public mySistersAddress = 0x999999cf1046e68e36E1aA2E0E07105eDDD1f08E; + + fallback () external { + used = 0; + } + + function setUsed(uint a) public { + if (msg.sender == MY_ADDRESS) { + used = a; + } + } +} + +contract Bad { + + uint constant A = 1; + bytes32 should_be_constant = sha256('abc'); + uint should_be_constant_2 = A + 1; + B should_be_constant_3 = B(address(0)); + address should_be_immutable = msg.sender; + uint should_be_immutable_2 = getNumber(); + uint should_be_immutable_3 = 10 + block.number; + B should_be_immutable_4 = new B(); + uint should_be_immutable_5; + + constructor(uint b) public { + should_be_immutable_5 = b; + } + + function getNumber() public returns(uint){ + return block.number; + } + +} + +contract Good { + + uint constant A = 1; + bytes32 constant should_be_constant = sha256('abc'); + uint constant should_be_constant_2 = A + 1; + B constant should_be_constant_3 = B(address(0)); + address immutable should_be_immutable = msg.sender; + uint immutable should_be_immutable_2 = getNumber(); + uint immutable should_be_immutable_3 = 10 + block.number; + B immutable should_be_immutable_4 = new B(); + uint immutable should_be_immutable_5; + + constructor(uint b) public { + should_be_immutable_5 = b; + } + + function getNumber() public returns(uint){ + return block.number; + } + +} \ No newline at end of file diff --git a/tests/detectors/immutable-states/0.6.11/immut_state_variables.sol.0.6.11.CouldBeImmutable.json b/tests/detectors/immutable-states/0.6.11/immut_state_variables.sol.0.6.11.CouldBeImmutable.json new file mode 100644 index 000000000..15064ca99 --- /dev/null +++ b/tests/detectors/immutable-states/0.6.11/immut_state_variables.sol.0.6.11.CouldBeImmutable.json @@ -0,0 +1,339 @@ +[ + [ + { + "elements": [ + { + "type": "variable", + "name": "should_be_immutable_5", + "source_mapping": { + "start": 1077, + "length": 26, + "filename_relative": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 47 + ], + "starting_column": 5, + "ending_column": 31 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 539, + "filename_relative": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56, + 57 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_immutable_5 (tests/detectors/immutable-states/0.6.11/immut_state_variables.sol#47) should be immutable \n", + "markdown": "[Bad.should_be_immutable_5](tests/detectors/immutable-states/0.6.11/immut_state_variables.sol#L47) should be immutable \n", + "first_markdown_element": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol#L47", + "id": "42d50245236163ceca90dea732165e65c2155934b149a5a1a5c51bddc0b5b02a", + "check": "immutable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_immutable_2", + "source_mapping": { + "start": 940, + "length": 40, + "filename_relative": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 44 + ], + "starting_column": 5, + "ending_column": 45 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 539, + "filename_relative": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56, + 57 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_immutable_2 (tests/detectors/immutable-states/0.6.11/immut_state_variables.sol#44) should be immutable \n", + "markdown": "[Bad.should_be_immutable_2](tests/detectors/immutable-states/0.6.11/immut_state_variables.sol#L44) should be immutable \n", + "first_markdown_element": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol#L44", + "id": "70d57aa51dda92c28444a466db8567fa783c85d484259aa5eee2ebc63f97a200", + "check": "immutable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_immutable_4", + "source_mapping": { + "start": 1038, + "length": 33, + "filename_relative": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 46 + ], + "starting_column": 5, + "ending_column": 38 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 539, + "filename_relative": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56, + 57 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_immutable_4 (tests/detectors/immutable-states/0.6.11/immut_state_variables.sol#46) should be immutable \n", + "markdown": "[Bad.should_be_immutable_4](tests/detectors/immutable-states/0.6.11/immut_state_variables.sol#L46) should be immutable \n", + "first_markdown_element": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol#L46", + "id": "a26d6df4087ac010928bc4bd18aa70ac58a28e584b1288e348d9c255473c300d", + "check": "immutable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_immutable", + "source_mapping": { + "start": 894, + "length": 40, + "filename_relative": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 43 + ], + "starting_column": 5, + "ending_column": 45 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 539, + "filename_relative": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56, + 57 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_immutable (tests/detectors/immutable-states/0.6.11/immut_state_variables.sol#43) should be immutable \n", + "markdown": "[Bad.should_be_immutable](tests/detectors/immutable-states/0.6.11/immut_state_variables.sol#L43) should be immutable \n", + "first_markdown_element": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol#L43", + "id": "b163d277f544f7f05ed4bcddda61e444be893e65ba0469688abd7b401a1db222", + "check": "immutable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_immutable_3", + "source_mapping": { + "start": 986, + "length": 46, + "filename_relative": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 45 + ], + "starting_column": 5, + "ending_column": 51 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 539, + "filename_relative": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56, + 57 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_immutable_3 (tests/detectors/immutable-states/0.6.11/immut_state_variables.sol#45) should be immutable \n", + "markdown": "[Bad.should_be_immutable_3](tests/detectors/immutable-states/0.6.11/immut_state_variables.sol#L45) should be immutable \n", + "first_markdown_element": "tests/detectors/immutable-states/0.6.11/immut_state_variables.sol#L45", + "id": "f19f7a22a6f17ffd8b5c29021226388aab7548f996b686a8e0b2bc861f72d447", + "check": "immutable-states", + "impact": "Optimization", + "confidence": "High" + } + ] +] \ No newline at end of file diff --git a/tests/detectors/immutable-states/0.7.6/immut_state_variables.sol b/tests/detectors/immutable-states/0.7.6/immut_state_variables.sol new file mode 100644 index 000000000..297dd6294 --- /dev/null +++ b/tests/detectors/immutable-states/0.7.6/immut_state_variables.sol @@ -0,0 +1,78 @@ + +contract A { + + address constant public MY_ADDRESS = 0xE0f5206BBD039e7b0592d8918820024e2a7437b9; + address public myFriendsAddress = 0xc0ffee254729296a45a3885639AC7E10F9d54979; + + uint public used; + uint public test = 5; + + uint constant X = 32**22 + 8; + string constant TEXT1 = "abc"; + string text2 = "xyz"; + + function setUsed() public { + if (msg.sender == MY_ADDRESS) { + used = test; + } + } +} + + +contract B is A { + + address public mySistersAddress = 0x999999cf1046e68e36E1aA2E0E07105eDDD1f08E; + + fallback () external { + used = 0; + } + + function setUsed(uint a) public { + if (msg.sender == MY_ADDRESS) { + used = a; + } + } +} + +contract Bad { + + uint constant A = 1; + bytes32 should_be_constant = sha256('abc'); + uint should_be_constant_2 = A + 1; + B should_be_constant_3 = B(address(0)); + address should_be_immutable = msg.sender; + uint should_be_immutable_2 = getNumber(); + uint should_be_immutable_3 = 10 + block.number; + B should_be_immutable_4 = new B(); + uint should_be_immutable_5; + + constructor(uint b) { + should_be_immutable_5 = b; + } + + function getNumber() public returns(uint){ + return block.number; + } +} + +contract Good { + + uint constant A = 1; + bytes32 constant should_be_constant = sha256('abc'); + uint constant should_be_constant_2 = A + 1; + B constant should_be_constant_3 = B(address(0)); + address immutable should_be_immutable = msg.sender; + uint immutable should_be_immutable_2 = getNumber(); + uint immutable should_be_immutable_3 = 10 + block.number; + B immutable should_be_immutable_4 = new B(); + uint immutable should_be_immutable_5; + + constructor(uint b) { + should_be_immutable_5 = b; + } + + function getNumber() public returns(uint){ + return block.number; + } + +} diff --git a/tests/detectors/immutable-states/0.7.6/immut_state_variables.sol.0.7.6.CouldBeImmutable.json b/tests/detectors/immutable-states/0.7.6/immut_state_variables.sol.0.7.6.CouldBeImmutable.json new file mode 100644 index 000000000..40c8b1368 --- /dev/null +++ b/tests/detectors/immutable-states/0.7.6/immut_state_variables.sol.0.7.6.CouldBeImmutable.json @@ -0,0 +1,334 @@ +[ + [ + { + "elements": [ + { + "type": "variable", + "name": "should_be_immutable_5", + "source_mapping": { + "start": 1077, + "length": 26, + "filename_relative": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 47 + ], + "starting_column": 5, + "ending_column": 31 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 531, + "filename_relative": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_immutable_5 (tests/detectors/immutable-states/0.7.6/immut_state_variables.sol#47) should be immutable \n", + "markdown": "[Bad.should_be_immutable_5](tests/detectors/immutable-states/0.7.6/immut_state_variables.sol#L47) should be immutable \n", + "first_markdown_element": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol#L47", + "id": "42d50245236163ceca90dea732165e65c2155934b149a5a1a5c51bddc0b5b02a", + "check": "immutable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_immutable_2", + "source_mapping": { + "start": 940, + "length": 40, + "filename_relative": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 44 + ], + "starting_column": 5, + "ending_column": 45 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 531, + "filename_relative": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_immutable_2 (tests/detectors/immutable-states/0.7.6/immut_state_variables.sol#44) should be immutable \n", + "markdown": "[Bad.should_be_immutable_2](tests/detectors/immutable-states/0.7.6/immut_state_variables.sol#L44) should be immutable \n", + "first_markdown_element": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol#L44", + "id": "70d57aa51dda92c28444a466db8567fa783c85d484259aa5eee2ebc63f97a200", + "check": "immutable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_immutable_4", + "source_mapping": { + "start": 1038, + "length": 33, + "filename_relative": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 46 + ], + "starting_column": 5, + "ending_column": 38 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 531, + "filename_relative": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_immutable_4 (tests/detectors/immutable-states/0.7.6/immut_state_variables.sol#46) should be immutable \n", + "markdown": "[Bad.should_be_immutable_4](tests/detectors/immutable-states/0.7.6/immut_state_variables.sol#L46) should be immutable \n", + "first_markdown_element": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol#L46", + "id": "a26d6df4087ac010928bc4bd18aa70ac58a28e584b1288e348d9c255473c300d", + "check": "immutable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_immutable", + "source_mapping": { + "start": 894, + "length": 40, + "filename_relative": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 43 + ], + "starting_column": 5, + "ending_column": 45 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 531, + "filename_relative": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_immutable (tests/detectors/immutable-states/0.7.6/immut_state_variables.sol#43) should be immutable \n", + "markdown": "[Bad.should_be_immutable](tests/detectors/immutable-states/0.7.6/immut_state_variables.sol#L43) should be immutable \n", + "first_markdown_element": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol#L43", + "id": "b163d277f544f7f05ed4bcddda61e444be893e65ba0469688abd7b401a1db222", + "check": "immutable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_immutable_3", + "source_mapping": { + "start": 986, + "length": 46, + "filename_relative": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 45 + ], + "starting_column": 5, + "ending_column": 51 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 531, + "filename_relative": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_immutable_3 (tests/detectors/immutable-states/0.7.6/immut_state_variables.sol#45) should be immutable \n", + "markdown": "[Bad.should_be_immutable_3](tests/detectors/immutable-states/0.7.6/immut_state_variables.sol#L45) should be immutable \n", + "first_markdown_element": "tests/detectors/immutable-states/0.7.6/immut_state_variables.sol#L45", + "id": "f19f7a22a6f17ffd8b5c29021226388aab7548f996b686a8e0b2bc861f72d447", + "check": "immutable-states", + "impact": "Optimization", + "confidence": "High" + } + ] +] \ No newline at end of file diff --git a/tests/detectors/immutable-states/0.8.0/immut_state_variables.sol b/tests/detectors/immutable-states/0.8.0/immut_state_variables.sol new file mode 100644 index 000000000..f405a1587 --- /dev/null +++ b/tests/detectors/immutable-states/0.8.0/immut_state_variables.sol @@ -0,0 +1,78 @@ + +contract A { + + address constant public MY_ADDRESS = 0xE0f5206BBD039e7b0592d8918820024e2a7437b9; + address public myFriendsAddress = 0xc0ffee254729296a45a3885639AC7E10F9d54979; + + uint public used; + uint public test = 5; + + uint constant X = 32**22 + 8; + string constant TEXT1 = "abc"; + string text2 = "xyz"; + + function setUsed() public { + if (msg.sender == MY_ADDRESS) { + used = test; + } + } +} + + +contract B is A { + + address public mySistersAddress = 0x999999cf1046e68e36E1aA2E0E07105eDDD1f08E; + + fallback () external { + used = 0; + } + + function setUsed(uint a) public { + if (msg.sender == MY_ADDRESS) { + used = a; + } + } +} + +contract Bad { + + uint constant A = 1; + bytes32 should_be_constant = sha256('abc'); + uint should_be_constant_2 = A + 1; + B should_be_constant_3 = B(address(0)); + address should_be_immutable = msg.sender; + uint should_be_immutable_2 = getNumber(); + uint should_be_immutable_3 = 10 + block.number; + uint should_be_immutable_5; + + constructor(uint b) { + should_be_immutable_5 = b; + } + + function getNumber() public returns(uint){ + return block.number; + } + +} + +contract Good { + + uint constant A = 1; + bytes32 constant should_be_constant = sha256('abc'); + uint constant should_be_constant_2 = A + 1; + B constant should_be_constant_3 = B(address(0)); + address immutable should_be_immutable = msg.sender; + uint immutable should_be_immutable_2 = getNumber(); + uint immutable should_be_immutable_3 = 10 + block.number; + B immutable should_be_immutable_4 = new B(); + uint immutable should_be_immutable_5; + + constructor(uint b) { + should_be_immutable_5 = b; + } + + function getNumber() public returns(uint){ + return block.number; + } + +} \ No newline at end of file diff --git a/tests/detectors/immutable-states/0.8.0/immut_state_variables.sol.0.8.0.CouldBeImmutable.json b/tests/detectors/immutable-states/0.8.0/immut_state_variables.sol.0.8.0.CouldBeImmutable.json new file mode 100644 index 000000000..afa2e3bb2 --- /dev/null +++ b/tests/detectors/immutable-states/0.8.0/immut_state_variables.sol.0.8.0.CouldBeImmutable.json @@ -0,0 +1,268 @@ +[ + [ + { + "elements": [ + { + "type": "variable", + "name": "should_be_immutable_5", + "source_mapping": { + "start": 1038, + "length": 26, + "filename_relative": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 46 + ], + "starting_column": 5, + "ending_column": 31 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 493, + "filename_relative": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_immutable_5 (tests/detectors/immutable-states/0.8.0/immut_state_variables.sol#46) should be immutable \n", + "markdown": "[Bad.should_be_immutable_5](tests/detectors/immutable-states/0.8.0/immut_state_variables.sol#L46) should be immutable \n", + "first_markdown_element": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol#L46", + "id": "42d50245236163ceca90dea732165e65c2155934b149a5a1a5c51bddc0b5b02a", + "check": "immutable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_immutable_2", + "source_mapping": { + "start": 940, + "length": 40, + "filename_relative": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 44 + ], + "starting_column": 5, + "ending_column": 45 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 493, + "filename_relative": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_immutable_2 (tests/detectors/immutable-states/0.8.0/immut_state_variables.sol#44) should be immutable \n", + "markdown": "[Bad.should_be_immutable_2](tests/detectors/immutable-states/0.8.0/immut_state_variables.sol#L44) should be immutable \n", + "first_markdown_element": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol#L44", + "id": "70d57aa51dda92c28444a466db8567fa783c85d484259aa5eee2ebc63f97a200", + "check": "immutable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_immutable", + "source_mapping": { + "start": 894, + "length": 40, + "filename_relative": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 43 + ], + "starting_column": 5, + "ending_column": 45 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 493, + "filename_relative": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_immutable (tests/detectors/immutable-states/0.8.0/immut_state_variables.sol#43) should be immutable \n", + "markdown": "[Bad.should_be_immutable](tests/detectors/immutable-states/0.8.0/immut_state_variables.sol#L43) should be immutable \n", + "first_markdown_element": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol#L43", + "id": "b163d277f544f7f05ed4bcddda61e444be893e65ba0469688abd7b401a1db222", + "check": "immutable-states", + "impact": "Optimization", + "confidence": "High" + }, + { + "elements": [ + { + "type": "variable", + "name": "should_be_immutable_3", + "source_mapping": { + "start": 986, + "length": 46, + "filename_relative": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 45 + ], + "starting_column": 5, + "ending_column": 51 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "Bad", + "source_mapping": { + "start": 718, + "length": 493, + "filename_relative": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol", + "is_dependency": false, + "lines": [ + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 55, + 56 + ], + "starting_column": 1, + "ending_column": 2 + } + } + } + } + ], + "description": "Bad.should_be_immutable_3 (tests/detectors/immutable-states/0.8.0/immut_state_variables.sol#45) should be immutable \n", + "markdown": "[Bad.should_be_immutable_3](tests/detectors/immutable-states/0.8.0/immut_state_variables.sol#L45) should be immutable \n", + "first_markdown_element": "tests/detectors/immutable-states/0.8.0/immut_state_variables.sol#L45", + "id": "f19f7a22a6f17ffd8b5c29021226388aab7548f996b686a8e0b2bc861f72d447", + "check": "immutable-states", + "impact": "Optimization", + "confidence": "High" + } + ] +] \ No newline at end of file diff --git a/tests/test_detectors.py b/tests/test_detectors.py index a45369fcd..994f87aac 100644 --- a/tests/test_detectors.py +++ b/tests/test_detectors.py @@ -480,28 +480,53 @@ ALL_TEST_OBJECTS = [ "0.7.6", ), Test( - all_detectors.ConstCandidateStateVars, + all_detectors.CouldBeConstant, "const_state_variables.sol", "0.4.25", ), Test( - all_detectors.ConstCandidateStateVars, + all_detectors.CouldBeConstant, "const_state_variables.sol", "0.5.16", ), Test( - all_detectors.ConstCandidateStateVars, + all_detectors.CouldBeConstant, "const_state_variables.sol", "0.6.11", ), Test( - all_detectors.ConstCandidateStateVars, + all_detectors.CouldBeConstant, "const_state_variables.sol", "0.7.6", ), Test( - all_detectors.ConstCandidateStateVars, - "immutable.sol", + all_detectors.CouldBeConstant, + "const_state_variables.sol", + "0.8.0", + ), + Test( + all_detectors.CouldBeImmutable, + "immut_state_variables.sol", + "0.4.25", + ), + Test( + all_detectors.CouldBeImmutable, + "immut_state_variables.sol", + "0.5.16", + ), + Test( + all_detectors.CouldBeImmutable, + "immut_state_variables.sol", + "0.6.11", + ), + Test( + all_detectors.CouldBeImmutable, + "immut_state_variables.sol", + "0.7.6", + ), + Test( + all_detectors.CouldBeImmutable, + "immut_state_variables.sol", "0.8.0", ), Test(