import re
import sys
def find_function_end(lines, start_idx):
brace_count = 0
in_function = False
for i in range(start_idx, len(lines)):
line = lines[i]
for char in line:
if char == '{':
brace_count += 1
in_function = True
elif char == '}':
brace_count -= 1
if in_function and brace_count == 0:
return i
return len(lines) - 1
def remove_dead_code(filename):
with open(filename, 'r') as f:
lines = f.readlines()
dead_code_indices = []
for i, line in enumerate(lines):
if '#[allow(dead_code)]' in line:
dead_code_indices.append(i)
dead_code_indices.reverse()
removed_functions = []
for idx in dead_code_indices:
func_start = idx
for j in range(idx, min(idx + 5, len(lines))):
if 'fn ' in lines[j]:
match = re.search(r'fn\s+(\w+)', lines[j])
if match:
func_name = match.group(1)
removed_functions.append(func_name)
func_start = j
break
func_end = find_function_end(lines, func_start)
start_remove = idx
for j in range(idx - 1, max(0, idx - 10), -1):
if lines[j].strip().startswith('///') or lines[j].strip().startswith('//'):
start_remove = j
elif lines[j].strip() == '':
continue
else:
break
del lines[start_remove:func_end + 1]
while start_remove < len(lines) and lines[start_remove].strip() == '':
del lines[start_remove]
with open(filename, 'w') as f:
f.writelines(lines)
return removed_functions
if __name__ == '__main__':
filename = 'src/services/deep_context.rs'
removed = remove_dead_code(filename)
print(f"Removed {len(removed)} dead code functions:")
for func in removed:
print(f" - {func}")