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
15 changes: 13 additions & 2 deletions codeflash/discovery/functions_to_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,11 +604,22 @@ 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 = [function_node]
pop = stack.pop
push = stack.append
# Directly access _fields to avoid function call overhead of ast.iter_child_nodes
while stack:
node = stack.pop()
node = pop()
if isinstance(node, ast.Return):
return True
stack.extend(ast.iter_child_nodes(node))
# Fast field inspection to avoid ast.iter_child_nodes overhead
for field in getattr(node, "_fields", ()):
value = getattr(node, field, None)
if isinstance(value, list):
for item in value:
if isinstance(item, ast.AST):
push(item)
elif isinstance(value, ast.AST):
push(value)
return False


Expand Down
Loading