1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// =============================================================================
// Rust Dead Code Strategy
// =============================================================================
struct RustDeadCodeStrategy;
impl DeadCodeStrategy for RustDeadCodeStrategy {
fn language(&self) -> &str {
"rust"
}
fn analyze(&self, path: &Path) -> Result<DeadCodeResult> {
debug!("Running Rust dead code analysis with cargo check");
// Use existing cargo-based dead code detection
// This is the current working implementation for Rust
let dead_functions = analyze_rust_dead_code_with_cargo(path)?;
let (total_functions, total_files) = count_rust_functions_and_files(path)?;
let dead_percentage = if total_functions > 0 {
(dead_functions.len() as f64 / total_functions as f64) * 100.0
} else {
0.0
};
Ok(DeadCodeResult {
language: "rust".to_string(),
dead_functions,
total_functions,
total_files,
dead_code_percentage: dead_percentage,
})
}
}
// =============================================================================
// C Dead Code Strategy
// =============================================================================
struct CDeadCodeStrategy;
impl DeadCodeStrategy for CDeadCodeStrategy {
fn language(&self) -> &str {
"c"
}
fn analyze(&self, path: &Path) -> Result<DeadCodeResult> {
debug!("Running C dead code analysis (AST-based)");
// Find C source files (.c only, not .h headers which are just declarations)
let c_impl_files = find_files_by_extension(path, &["c"]);
// Find all C files (including headers) for call analysis
let c_all_files = find_files_by_extension(path, &["c", "h"]);
// Extract function definitions from .c files only
let (defined_functions, _) = analyze_c_files(&c_impl_files)?;
// Extract calls from all files (including headers)
let (_, called_functions) = analyze_c_files(&c_all_files)?;
// Find dead functions (defined but never called)
let dead_functions = find_uncalled_functions(&defined_functions, &called_functions);
let total_functions = defined_functions.len();
let dead_percentage = if total_functions > 0 {
(dead_functions.len() as f64 / total_functions as f64) * 100.0
} else {
0.0
};
Ok(DeadCodeResult {
language: "c".to_string(),
dead_functions,
total_functions,
// Every .c/.h file walked for call analysis (#720).
total_files: c_all_files.len(),
dead_code_percentage: dead_percentage,
})
}
}
// =============================================================================
// C++ Dead Code Strategy
// =============================================================================
struct CppDeadCodeStrategy;
impl DeadCodeStrategy for CppDeadCodeStrategy {
fn language(&self) -> &str {
"cpp"
}
fn analyze(&self, path: &Path) -> Result<DeadCodeResult> {
debug!("Running C++ dead code analysis (AST-based)");
// Find all C++ source files
let cpp_files = find_files_by_extension(path, &["cpp", "cc", "cxx", "hpp", "hxx", "h"]);
// Extract function definitions and calls (similar to C)
let (defined_functions, called_functions) = analyze_cpp_files(&cpp_files)?;
// Find dead functions
let dead_functions = find_uncalled_functions(&defined_functions, &called_functions);
let total_functions = defined_functions.len();
let dead_percentage = if total_functions > 0 {
(dead_functions.len() as f64 / total_functions as f64) * 100.0
} else {
0.0
};
Ok(DeadCodeResult {
language: "cpp".to_string(),
dead_functions,
total_functions,
// Every C++ source/header file walked (#720).
total_files: cpp_files.len(),
dead_code_percentage: dead_percentage,
})
}
}
// =============================================================================
// Python Dead Code Strategy
// =============================================================================
struct PythonDeadCodeStrategy;
impl DeadCodeStrategy for PythonDeadCodeStrategy {
fn language(&self) -> &str {
"python"
}
fn analyze(&self, path: &Path) -> Result<DeadCodeResult> {
debug!("Running Python dead code analysis (AST-based)");
// Find all Python files
let py_files = find_files_by_extension(path, &["py"]);
// Extract function definitions and calls
let (defined_functions, called_functions) = analyze_python_files(&py_files)?;
// Find dead functions
let dead_functions = find_uncalled_functions(&defined_functions, &called_functions);
let total_functions = defined_functions.len();
let dead_percentage = if total_functions > 0 {
(dead_functions.len() as f64 / total_functions as f64) * 100.0
} else {
0.0
};
Ok(DeadCodeResult {
language: "python".to_string(),
dead_functions,
total_functions,
// Every .py file walked (#720): a 2-file fixture with 4 functions
// used to report 4 files analyzed.
total_files: py_files.len(),
dead_code_percentage: dead_percentage,
})
}
}
// =============================================================================
// Lua Dead Code Strategy (with module export awareness)
// =============================================================================
struct LuaDeadCodeStrategy;
impl DeadCodeStrategy for LuaDeadCodeStrategy {
fn language(&self) -> &str {
"lua"
}
fn analyze(&self, path: &Path) -> Result<DeadCodeResult> {
debug!("Running Lua dead code analysis (module-export-aware)");
let lua_files = find_files_by_extension(path, &["lua"]);
let (defined_functions, called_functions) = analyze_lua_files(&lua_files)?;
// Find dead functions (defined but never called AND not exported)
let dead_functions = find_uncalled_functions(&defined_functions, &called_functions);
let total_functions = defined_functions.len();
let dead_percentage = if total_functions > 0 {
(dead_functions.len() as f64 / total_functions as f64) * 100.0
} else {
0.0
};
Ok(DeadCodeResult {
language: "lua".to_string(),
dead_functions,
total_functions,
// Every .lua file walked (#720).
total_files: lua_files.len(),
dead_code_percentage: dead_percentage,
})
}
}
// =============================================================================
// Helper Functions
// =============================================================================