pmat 3.30.1

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP)
Documentation
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Multi-Language Dead Code Analysis Module (BUG-004 Fix)
//!
//! This module provides dead code detection across multiple programming languages
//! without requiring Cargo.toml or assuming Rust projects.
//!
//! Fixes:
//! - BUG-004: Dead code analyzer broken for non-Rust projects

#![cfg_attr(coverage_nightly, coverage(off))]
use anyhow::{Context, Result};
use regex::Regex;
use std::collections::HashSet;
use std::path::Path;
use tracing::{debug, info};
use walkdir::WalkDir;

// Pre-compiled regex patterns
include!("dead_code_multi_language_regex.rs");

// Types, traits, and dispatch
include!("dead_code_multi_language_types.rs");

// Language strategy implementations
include!("dead_code_multi_language_strategies.rs");

// C, C++, Python analysis helpers
include!("dead_code_multi_language_c_python.rs");

// Lua analysis helpers
include!("dead_code_multi_language_lua.rs");

// Rust-specific analysis helpers
include!("dead_code_multi_language_rust.rs");

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_c_dead_code_detection() {
        let temp = create_test_c_project();
        let result = analyze_dead_code_multi_language(temp.path()).unwrap();

        eprintln!("C dead code result: {:?}", result);
        eprintln!("Dead functions: {:?}", result.dead_functions);

        assert_eq!(result.language, "c");
        assert_eq!(
            result.total_functions, 2,
            "Should find 2 functions: used_function and unused_function"
        );
        assert_eq!(
            result.dead_functions.len(),
            1,
            "Should find 1 dead function"
        );
        assert_eq!(result.dead_functions[0].name, "unused_function");
    }

    #[test]
    fn test_python_dead_code_detection() {
        let temp = create_test_python_project();
        let result = analyze_dead_code_multi_language(temp.path()).unwrap();

        assert_eq!(result.language, "python");
        assert!(!result.dead_functions.is_empty());
    }

    /// #720: `total_files` must be a FILE count. The caller used to report
    /// `total_functions.max(1)` as its file count, so this 1-file / 3-function
    /// Python project printed "Files Analyzed | 3".
    #[test]
    fn test_total_files_is_a_file_count_not_a_function_count() {
        let temp = create_test_python_project();
        let result = analyze_dead_code_multi_language(temp.path()).unwrap();

        assert_eq!(
            result.total_files, 1,
            "one .py file was walked, but total_files reported {}",
            result.total_files
        );
        // `main` is deliberately skipped by the extractor, leaving
        // used_function + unused_function.
        assert_eq!(
            result.total_functions, 2,
            "sanity: the fixture yields 2 counted functions"
        );
        assert_ne!(
            result.total_files, result.total_functions,
            "a function count must never be reported as a file count"
        );
    }

    /// #720: two C files, two functions -- the two counts happen to be equal in
    /// the other fixture, so this one separates them in the opposite direction.
    #[test]
    fn test_total_files_counts_every_walked_file() {
        let temp = TempDir::new().unwrap();
        std::fs::write(
            temp.path().join("main.c"),
            "int main() { used_function(); return 0; }\n",
        )
        .unwrap();
        std::fs::write(temp.path().join("a.c"), "void used_function() {}\n").unwrap();
        std::fs::write(temp.path().join("b.c"), "void dead_one() {}\n").unwrap();

        let result = analyze_dead_code_multi_language(temp.path()).unwrap();

        assert_eq!(
            result.total_files, 3,
            "three .c files were walked, got {}",
            result.total_files
        );
    }

    /// A nonexistent path used to be reported as "not supported for language:
    /// unknown" — a language verdict for a path that was never read.
    #[test]
    fn test_missing_path_is_a_path_error_not_a_language_verdict() {
        let err = analyze_dead_code_multi_language(Path::new(
            "/does/not/exist/pmat-dead-code-missing-path",
        ))
        .expect_err("a missing path must be an error");
        let msg = err.to_string();
        assert!(msg.contains("Path not found"), "{msg}");
        assert!(
            !msg.contains("unknown"),
            "must not report a detected language for a path that does not exist: {msg}"
        );
    }

    /// One unsupported dominant language used to abort the whole run, even when
    /// every supported file under the path was analysable.
    #[test]
    fn test_unsupported_dominant_language_still_analyses_supported_files() {
        let temp = TempDir::new().unwrap();
        // Enough TypeScript to win language detection...
        for i in 0..5 {
            std::fs::write(
                temp.path().join(format!("app{i}.ts")),
                "export function f(): number { return 1; }\n",
            )
            .unwrap();
        }
        // ...plus one analysable Python file.
        std::fs::write(
            temp.path().join("main.py"),
            "def main():\n    used()\n\ndef used():\n    pass\n\ndef dead_one():\n    pass\n",
        )
        .unwrap();

        let result = analyze_dead_code_multi_language(temp.path())
            .expect("supported files are present, so the run must not abort");
        assert_eq!(result.language, "python");
        assert!(
            result.dead_functions.iter().any(|d| d.name == "dead_one"),
            "the Python file must actually have been analysed: {result:?}"
        );
    }

    /// ...but a tree with nothing analysable in it must still say so.
    #[test]
    fn test_no_supported_files_still_errors() {
        let temp = TempDir::new().unwrap();
        std::fs::write(
            temp.path().join("app.ts"),
            "export function f(): number { return 1; }\n",
        )
        .unwrap();
        let err =
            analyze_dead_code_multi_language(temp.path()).expect_err("nothing analysable here");
        assert!(err.to_string().contains("no rust, c, cpp, python or lua"));
    }

    fn create_test_c_project() -> TempDir {
        let temp = TempDir::new().unwrap();
        std::fs::write(
            temp.path().join("main.c"),
            "int main() { used_function(); return 0; }\nvoid used_function() {}\nvoid unused_function() {}\n",
        ).unwrap();
        temp
    }

    fn create_test_python_project() -> TempDir {
        let temp = TempDir::new().unwrap();
        std::fs::write(
            temp.path().join("main.py"),
            "def main():\n    used_function()\n\ndef used_function():\n    pass\n\ndef unused_function():\n    pass\n",
        ).unwrap();
        std::fs::write(
            temp.path().join("pyproject.toml"),
            "[project]\nname=\"test\"\n",
        )
        .unwrap();
        temp
    }

    #[test]
    fn test_lua_dead_code_detection_basic() {
        let temp = TempDir::new().unwrap();
        // Create a Lua project with used and unused functions
        std::fs::write(
            temp.path().join("main.lua"),
            concat!(
                "local function used_helper()\n",
                "    return 42\n",
                "end\n",
                "\n",
                "local function dead_helper()\n",
                "    return 99\n",
                "end\n",
                "\n",
                "function run()\n",
                "    local x = used_helper()\n",
                "    return x\n",
                "end\n",
            ),
        )
        .unwrap();

        let lua_files = find_files_by_extension(temp.path(), &["lua"]);
        let (defined, called) = analyze_lua_files(&lua_files).unwrap();

        assert_eq!(defined.len(), 3, "Should find 3 functions");
        assert!(
            called.contains("used_helper"),
            "used_helper should be in calls"
        );
        assert!(
            !called.contains("dead_helper"),
            "dead_helper should NOT be in calls"
        );

        let dead = find_uncalled_functions(&defined, &called);
        let dead_names: Vec<&str> = dead.iter().map(|d| d.name.as_str()).collect();
        assert!(
            dead_names.contains(&"dead_helper"),
            "dead_helper should be dead"
        );
        assert!(
            !dead_names.contains(&"used_helper"),
            "used_helper should not be dead"
        );
    }

    #[test]
    fn test_lua_module_export_awareness() {
        let temp = TempDir::new().unwrap();
        // Module pattern: functions on M are exported via `return M`
        std::fs::write(
            temp.path().join("mymodule.lua"),
            concat!(
                "local M = {}\n",
                "\n",
                "function M.public_api()\n",
                "    return M.internal_calc()\n",
                "end\n",
                "\n",
                "function M.internal_calc()\n",
                "    return 42\n",
                "end\n",
                "\n",
                "local function truly_dead()\n",
                "    return 0\n",
                "end\n",
                "\n",
                "return M\n",
            ),
        )
        .unwrap();

        let lua_files = find_files_by_extension(temp.path(), &["lua"]);
        let (defined, called) = analyze_lua_files(&lua_files).unwrap();

        // Module functions should be treated as exported (called)
        assert!(
            called.contains("public_api"),
            "M.public_api should be marked as exported"
        );
        assert!(
            called.contains("internal_calc"),
            "M.internal_calc should be marked as exported"
        );

        let dead = find_uncalled_functions(&defined, &called);
        let dead_names: Vec<&str> = dead.iter().map(|d| d.name.as_str()).collect();
        assert!(
            dead_names.contains(&"truly_dead"),
            "truly_dead should be dead"
        );
        assert!(
            !dead_names.contains(&"public_api"),
            "exported funcs should not be dead"
        );
        assert!(
            !dead_names.contains(&"internal_calc"),
            "exported funcs should not be dead"
        );
    }

    #[test]
    fn test_lua_table_field_function_export() {
        let temp = TempDir::new().unwrap();
        // Alternative module pattern: M.name = function(...)
        std::fs::write(
            temp.path().join("alt_module.lua"),
            concat!(
                "local M = {}\n",
                "\n",
                "M.handler = function(req)\n",
                "    return req\n",
                "end\n",
                "\n",
                "M.middleware = function(ctx)\n",
                "    return ctx\n",
                "end\n",
                "\n",
                "local function orphan()\n",
                "    return nil\n",
                "end\n",
                "\n",
                "return M\n",
            ),
        )
        .unwrap();

        let lua_files = find_files_by_extension(temp.path(), &["lua"]);
        let (defined, called) = analyze_lua_files(&lua_files).unwrap();

        assert!(called.contains("handler"), "M.handler should be exported");
        assert!(
            called.contains("middleware"),
            "M.middleware should be exported"
        );

        let dead = find_uncalled_functions(&defined, &called);
        let dead_names: Vec<&str> = dead.iter().map(|d| d.name.as_str()).collect();
        assert!(dead_names.contains(&"orphan"), "orphan should be dead");
        assert_eq!(dead.len(), 1, "Only orphan should be dead");
    }

    #[test]
    fn test_lua_no_module_return_no_exports() {
        let temp = TempDir::new().unwrap();
        // File without module return - no export awareness
        std::fs::write(
            temp.path().join("script.lua"),
            concat!(
                "local M = {}\n",
                "\n",
                "function M.something()\n",
                "    return 1\n",
                "end\n",
                "\n",
                "-- no return M at end\n",
                "print(\"hello\")\n",
            ),
        )
        .unwrap();

        let lua_files = find_files_by_extension(temp.path(), &["lua"]);
        let (defined, called) = analyze_lua_files(&lua_files).unwrap();

        // Without `return M`, M.something is NOT auto-exported
        assert!(
            !called.contains("something"),
            "Without module return, not auto-exported"
        );
        let dead = find_uncalled_functions(&defined, &called);
        assert_eq!(dead.len(), 1);
        assert_eq!(dead[0].name, "something");
    }

    #[test]
    fn test_lua_detect_module_return() {
        assert_eq!(
            detect_lua_module_return("return M\n"),
            Some("M".to_string())
        );
        assert_eq!(
            detect_lua_module_return("return MyModule\n"),
            Some("MyModule".to_string())
        );
        assert_eq!(
            detect_lua_module_return("x = 1\nreturn M\n"),
            Some("M".to_string())
        );
        assert_eq!(
            detect_lua_module_return("return M\n-- trailing comment\n"),
            Some("M".to_string())
        );
        assert_eq!(detect_lua_module_return("print('done')\n"), None);
        assert_eq!(detect_lua_module_return("return 1, 2, 3\n"), None);
        assert_eq!(detect_lua_module_return(""), None);
    }

    #[test]
    fn test_lua_test_files_excluded_from_definitions() {
        let temp = TempDir::new().unwrap();
        std::fs::create_dir_all(temp.path().join("tests")).unwrap();
        std::fs::write(
            temp.path().join("tests/test_main.lua"),
            concat!(
                "local function test_helper()\n",
                "    return true\n",
                "end\n",
                "\n",
                "function test_run()\n",
                "    used_in_prod()\n",
                "end\n",
            ),
        )
        .unwrap();
        std::fs::write(
            temp.path().join("main.lua"),
            concat!("local function used_in_prod()\n", "    return 1\n", "end\n",),
        )
        .unwrap();

        let lua_files = find_files_by_extension(temp.path(), &["lua"]);
        let (defined, called) = analyze_lua_files(&lua_files).unwrap();

        // Test file functions should NOT be in defined list
        let def_names: Vec<&str> = defined.iter().map(|d| d.name.as_str()).collect();
        assert!(
            !def_names.contains(&"test_helper"),
            "Test functions excluded"
        );
        assert!(!def_names.contains(&"test_run"), "Test functions excluded");
        assert!(
            def_names.contains(&"used_in_prod"),
            "Prod functions included"
        );

        // But calls FROM test files should still be tracked
        assert!(
            called.contains("used_in_prod"),
            "Calls from tests should be tracked"
        );
    }
}