diff --git a/flake8_idom_hooks/rules_of_hooks.py b/flake8_idom_hooks/rules_of_hooks.py index c87599b..8280a76 100644 --- a/flake8_idom_hooks/rules_of_hooks.py +++ b/flake8_idom_hooks/rules_of_hooks.py @@ -81,7 +81,8 @@ def _visit_loop(self, node: ast.AST) -> None: visit_While = _visit_loop def visit_Return(self, node: ast.Return) -> None: - self._current_early_return = node + if self._current_component is self._current_function: + self._current_early_return = node def _check_if_hook_defined_in_function(self, node: ast.FunctionDef) -> None: if self._current_function is not None: @@ -111,7 +112,7 @@ def _check_if_propper_hook_usage(self, node: ast.Name | ast.Attribute) -> None: self._context.add_error( 103, node, - f"hook {name!r} used after an early return", + f"hook {name!r} used after an early return on line {self._current_early_return.lineno}", ) diff --git a/tests/cases/hook_usage.py b/tests/cases/hook_usage.py index a6228cf..3595911 100644 --- a/tests/cases/hook_usage.py +++ b/tests/cases/hook_usage.py @@ -188,7 +188,7 @@ def use_other(): def example(): if True: return None - # error: ROH103 hook 'use_state' used after an early return + # error: ROH103 hook 'use_state' used after an early return on line 190 use_state() @@ -211,3 +211,11 @@ def some_effect(): # Ok: no early return error use_state() + + +@idom.component +def regression_check(): + @use_effect + def effect(): + # this return caused false passitive early return error in use_effect usage + return cleanup