diff --git a/doc/whatsnew/fragments/9074.bugfix b/doc/whatsnew/fragments/9074.bugfix new file mode 100644 index 0000000000..16b3432fb7 --- /dev/null +++ b/doc/whatsnew/fragments/9074.bugfix @@ -0,0 +1,3 @@ +Fix crash in refactoring checker when unary operand used with variable in for loop. + +Closes #9074 diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py index f1c21842b3..ffcf34e17b 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -2362,7 +2362,7 @@ def _get_start_value(self, node: nodes.NodeNG) -> tuple[int | None, Confidence]: if ( isinstance(node, (nodes.Name, nodes.Call, nodes.Attribute)) or isinstance(node, nodes.UnaryOp) - and isinstance(node.operand, nodes.Attribute) + and isinstance(node.operand, (nodes.Attribute, nodes.Name)) ): inferred = utils.safe_infer(node) start_val = inferred.value if inferred else None diff --git a/tests/functional/r/regression/regression_9074_refactor_loop_with_unary_variable.py b/tests/functional/r/regression/regression_9074_refactor_loop_with_unary_variable.py new file mode 100644 index 0000000000..8fe929bd9e --- /dev/null +++ b/tests/functional/r/regression/regression_9074_refactor_loop_with_unary_variable.py @@ -0,0 +1,7 @@ +"""Regression test.""" +def crash_on_unary_op_with_name(): + """Should not crash with -idx.""" + mylist = [] + idx = 5 + for _i, _val in enumerate(mylist, start=-idx): + pass diff --git a/tests/functional/r/regression/regression_9074_refactor_loop_with_unary_variable.txt b/tests/functional/r/regression/regression_9074_refactor_loop_with_unary_variable.txt new file mode 100644 index 0000000000..e69de29bb2