@@ -1227,8 +1227,6 @@ def has_return_statement(self, function_node: FunctionNode, source: str) -> bool
12271227 True if the function has a return statement.
12281228
12291229 """
1230- source_bytes = source .encode ("utf8" )
1231-
12321230 # Generator functions always implicitly return a Generator/Iterator
12331231 if function_node .is_generator :
12341232 return True
@@ -1244,20 +1242,32 @@ def has_return_statement(self, function_node: FunctionNode, source: str) -> bool
12441242
12451243 def _node_has_return (self , node : Node ) -> bool :
12461244 """Recursively check if a node contains a return statement."""
1247- if node .type == "return_statement" :
1248- return True
1245+ # Use an explicit stack to avoid recursion overhead while preserving traversal order.
1246+ func_types = ("function_declaration" , "function_expression" , "arrow_function" , "method_definition" )
1247+ stack = [node ]
1248+ while stack :
1249+ current = stack .pop ()
1250+ # Direct return statement check
1251+ if current .type == "return_statement" :
1252+ return True
1253+
1254+ # If this node is a function-like node, only traverse its body children
1255+ if current .type in func_types :
1256+ body_node = current .child_by_field_name ("body" )
1257+ if body_node :
1258+ # Push children in reverse so they are processed in original order
1259+ children = body_node .children
1260+ if children :
1261+ stack .extend (reversed (children ))
1262+ # Do not traverse other parts of the function node
1263+ continue
1264+
1265+ # General case: traverse all children
1266+ children = current .children
1267+ if children :
1268+ stack .extend (reversed (children ))
12491269
1250- # Don't recurse into nested function definitions
1251- if node .type in ("function_declaration" , "function_expression" , "arrow_function" , "method_definition" ):
1252- # Only check the current function, not nested ones
1253- body_node = node .child_by_field_name ("body" )
1254- if body_node :
1255- for child in body_node .children :
1256- if self ._node_has_return (child ):
1257- return True
1258- return False
1259-
1260- return any (self ._node_has_return (child ) for child in node .children )
1270+ return False
12611271
12621272 def extract_type_annotations (self , source : str , function_name : str , function_line : int ) -> set [str ]:
12631273 """Extract type annotation names from a function's parameters and return type.
0 commit comments