Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion codeflash/discovery/functions_to_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,19 @@ def filter_files_optimized(file_path: Path, tests_root: Path, ignore_paths: list

def function_has_return_statement(function_node: FunctionDef | AsyncFunctionDef) -> bool:
# Custom DFS, return True as soon as a Return node is found
stack: list[ast.AST] = [function_node]
# Handle edge case where a Return node is passed directly
if isinstance(function_node, ast.Return):
return True

body = function_node.body
if not body:
return False
# Fast-path: check top-level body statements first (common case)
for node in body:
if isinstance(node, ast.Return):
return True
# Continue DFS from the body nodes (skip the wrapper function node)
stack: list[ast.AST] = list(body)
while stack:
node = stack.pop()
if isinstance(node, ast.Return):
Expand Down
Loading