Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 6c5abe7

Browse files
committed
another fix for early return + add early return lineno in msg
1 parent 81c9ca9 commit 6c5abe7

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

flake8_idom_hooks/rules_of_hooks.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def _visit_loop(self, node: ast.AST) -> None:
8181
visit_While = _visit_loop
8282

8383
def visit_Return(self, node: ast.Return) -> None:
84-
self._current_early_return = node
84+
if self._current_component is self._current_function:
85+
self._current_early_return = node
8586

8687
def _check_if_hook_defined_in_function(self, node: ast.FunctionDef) -> None:
8788
if self._current_function is not None:
@@ -111,7 +112,7 @@ def _check_if_propper_hook_usage(self, node: ast.Name | ast.Attribute) -> None:
111112
self._context.add_error(
112113
103,
113114
node,
114-
f"hook {name!r} used after an early return",
115+
f"hook {name!r} used after an early return on line {self._current_early_return.lineno}",
115116
)
116117

117118

tests/cases/hook_usage.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def use_other():
188188
def example():
189189
if True:
190190
return None
191-
# error: ROH103 hook 'use_state' used after an early return
191+
# error: ROH103 hook 'use_state' used after an early return on line 190
192192
use_state()
193193

194194

@@ -211,3 +211,11 @@ def some_effect():
211211

212212
# Ok: no early return error
213213
use_state()
214+
215+
216+
@idom.component
217+
def regression_check():
218+
@use_effect
219+
def effect():
220+
# this return caused false passitive early return error in use_effect usage
221+
return cleanup

0 commit comments

Comments
 (0)