From b600cae512474fb47406bdb471a94b2cebb8a955 Mon Sep 17 00:00:00 2001 From: Josselin Date: Wed, 11 Sep 2019 10:47:46 +0200 Subject: [PATCH] Improve comment patching + state variable constant patching --- .../tools/slither_format/formatters/constable_states.py | 4 +++- .../tools/slither_format/formatters/naming_convention.py | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/slither/tools/slither_format/formatters/constable_states.py b/slither/tools/slither_format/formatters/constable_states.py index 67f51536f..373b0c405 100644 --- a/slither/tools/slither_format/formatters/constable_states.py +++ b/slither/tools/slither_format/formatters/constable_states.py @@ -1,10 +1,12 @@ import re -from ..exceptions import FormatError +from ..exceptions import FormatError, FormatImpossible from ..utils.patches import create_patch def format(slither, result): elements = result['elements'] for element in elements: + if not element.expression: + raise FormatImpossible(f'{element.name} is uninitialized and cannot become constant.') _patch(slither, result, element['source_mapping']['filename_absolute'], element['name'], "constant " + element['name'], diff --git a/slither/tools/slither_format/formatters/naming_convention.py b/slither/tools/slither_format/formatters/naming_convention.py index c130c2006..be2abcc86 100644 --- a/slither/tools/slither_format/formatters/naming_convention.py +++ b/slither/tools/slither_format/formatters/naming_convention.py @@ -338,17 +338,17 @@ def _explore_variables_declaration(slither, variables, result, target, convert, # Patch comment only makes sense for local variable declaration in the parameter list if patch_comment and isinstance(variable, LocalVariable): if 'lines' in variable.source_mapping and variable.source_mapping['lines']: - end_line = variable.source_mapping['lines'][0] func = variable.function + end_line = func.source_mapping['lines'][0] if variable in func.parameters: - idx = len(func.parameters) - func.parameters.index(variable) + idx = len(func.parameters) - func.parameters.index(variable) + 1 first_line = end_line - idx - 2 potential_comments = slither.source_code[filename_source_code].encode('utf8') potential_comments = potential_comments.splitlines(keepends=True)[first_line:end_line-1] - idx_beginning = variable.source_mapping['start'] - idx_beginning += - variable.source_mapping['starting_column'] + 1 + idx_beginning = func.source_mapping['start'] + idx_beginning += - func.source_mapping['starting_column'] + 1 idx_beginning += - sum([len(c) for c in potential_comments]) old_comment = f'@param {old_str}'.encode('utf8')