pmat 2.93.1

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
//! Accurate dead code analyzer using cargo/rustc integration
//!
//! This module provides accurate dead code detection by leveraging
//! the Rust compiler's built-in dead code analysis, replacing the
//! previous heuristic-based approach that produced false positives.

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Command;

/// Dead code analysis result with accurate metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccurateDeadCodeReport {
    /// Files with dead code
    pub files_with_dead_code: Vec<FileDeadCode>,
    /// Total dead code items
    pub total_dead_items: usize,
    /// Accurate dead code percentage
    pub dead_code_percentage: f64,
    /// Total lines analyzed
    pub total_lines: usize,
    /// Dead lines count
    pub dead_lines: usize,
    /// Summary by type
    pub dead_by_type: HashMap<String, usize>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileDeadCode {
    pub file_path: PathBuf,
    pub dead_items: Vec<DeadItem>,
    pub file_dead_percentage: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeadItem {
    pub name: String,
    pub kind: DeadCodeKind,
    pub line: usize,
    pub column: usize,
    pub message: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum DeadCodeKind {
    Function,
    Method,
    Struct,
    Enum,
    Variant,
    Field,
    Constant,
    Static,
    Module,
    Trait,
    TypeAlias,
    Other(String),
}

/// Cargo-based dead code analyzer for accurate detection
pub struct CargoDeadCodeAnalyzer {
    project_path: PathBuf,
    exclude_tests: bool,
    exclude_examples: bool,
    exclude_benches: bool,
}

impl CargoDeadCodeAnalyzer {
    /// Create a new analyzer for the given project path
    pub fn new(project_path: impl AsRef<Path>) -> Self {
        Self {
            project_path: project_path.as_ref().to_path_buf(),
            exclude_tests: true,
            exclude_examples: true,
            exclude_benches: true,
        }
    }

    /// Include test code in analysis
    #[must_use] 
    pub fn include_tests(mut self) -> Self {
        self.exclude_tests = false;
        self
    }

    /// Include example code in analysis
    #[must_use] 
    pub fn include_examples(mut self) -> Self {
        self.exclude_examples = false;
        self
    }

    /// Include benchmark code in analysis
    #[must_use] 
    pub fn include_benches(mut self) -> Self {
        self.exclude_benches = false;
        self
    }

    /// Perform accurate dead code analysis using cargo
    pub async fn analyze(&self) -> Result<AccurateDeadCodeReport> {
        // Run cargo check with JSON output
        let cargo_output = self.run_cargo_check()?;

        // Parse dead code warnings from cargo output
        let dead_items = self.parse_cargo_warnings(&cargo_output)?;

        // Group by file
        let files_with_dead_code = self.group_by_file(dead_items);

        // Calculate metrics
        let report = self.calculate_metrics(files_with_dead_code).await?;

        Ok(report)
    }

    /// Run cargo check and capture JSON output
    fn run_cargo_check(&self) -> Result<String> {
        let mut cmd = Command::new("cargo");
        cmd.current_dir(&self.project_path)
            .arg("check")
            .arg("--message-format=json")
            .arg("--all-targets");

        // Add appropriate flags based on exclusions
        if self.exclude_tests {
            cmd.arg("--lib").arg("--bins");
        }

        let output = cmd.output().context("Failed to run cargo check")?;

        // Cargo outputs JSON messages to stdout
        let stdout = String::from_utf8_lossy(&output.stdout);
        Ok(stdout.to_string())
    }

    /// Parse cargo's JSON output for dead code warnings
    fn parse_cargo_warnings(&self, output: &str) -> Result<Vec<(PathBuf, DeadItem)>> {
        let mut dead_items = Vec::new();

        for line in output.lines() {
            if line.trim().is_empty() {
                continue;
            }

            let json: Value = match serde_json::from_str(line) {
                Ok(v) => v,
                Err(_) => continue, // Skip non-JSON lines
            };

            // Check if this is a compiler message
            if json["reason"] != "compiler-message" {
                continue;
            }

            let message = &json["message"];

            // Check if this is a dead code warning
            if let Some(code) = message["code"]["code"].as_str() {
                if code == "dead_code" {
                    if let Some(item) = self.extract_dead_item(message) {
                        dead_items.push(item);
                    }
                }
            }
        }

        Ok(dead_items)
    }

    /// Extract dead code item from compiler message
    fn extract_dead_item(&self, message: &Value) -> Option<(PathBuf, DeadItem)> {
        let spans = message["spans"].as_array()?;
        let primary_span = spans
            .iter()
            .find(|s| s["is_primary"].as_bool() == Some(true))?;

        let file_path = PathBuf::from(primary_span["file_name"].as_str()?);
        let line = primary_span["line_start"].as_u64()? as usize;
        let column = primary_span["column_start"].as_u64()? as usize;

        let message_text = message["message"].as_str()?;
        let (name, kind) = self.parse_message(message_text)?;

        Some((
            file_path.clone(),
            DeadItem {
                name,
                kind,
                line,
                column,
                message: message_text.to_string(),
            },
        ))
    }

    /// Parse the warning message to extract name and kind
    fn parse_message(&self, message: &str) -> Option<(String, DeadCodeKind)> {
        // Common patterns in dead code messages
        let patterns = [
            ("function `", "` is never used", DeadCodeKind::Function),
            ("method `", "` is never used", DeadCodeKind::Method),
            ("struct `", "` is never constructed", DeadCodeKind::Struct),
            ("enum `", "` is never used", DeadCodeKind::Enum),
            ("variant `", "` is never constructed", DeadCodeKind::Variant),
            ("field `", "` is never read", DeadCodeKind::Field),
            ("constant `", "` is never used", DeadCodeKind::Constant),
            ("static `", "` is never used", DeadCodeKind::Static),
            ("module `", "` is never used", DeadCodeKind::Module),
            ("trait `", "` is never used", DeadCodeKind::Trait),
            ("type alias `", "` is never used", DeadCodeKind::TypeAlias),
        ];

        for (prefix, suffix, kind) in &patterns {
            if let Some(start) = message.find(prefix) {
                let name_start = start + prefix.len();
                if let Some(end) = message[name_start..].find(suffix) {
                    let name = message[name_start..name_start + end].to_string();
                    return Some((name, kind.clone()));
                }
            }
        }

        // Fallback for unknown patterns
        if message.contains("is never") || message.contains("never used") {
            // Try to extract name between backticks
            if let Some(start) = message.find('`') {
                if let Some(end) = message[start + 1..].find('`') {
                    let name = message[start + 1..start + 1 + end].to_string();
                    return Some((name, DeadCodeKind::Other("unknown".to_string())));
                }
            }
        }

        None
    }

    /// Group dead items by file
    fn group_by_file(&self, items: Vec<(PathBuf, DeadItem)>) -> Vec<FileDeadCode> {
        let mut file_map: HashMap<PathBuf, Vec<DeadItem>> = HashMap::new();

        for (path, item) in items {
            file_map.entry(path).or_default().push(item);
        }

        file_map
            .into_iter()
            .map(|(file_path, dead_items)| {
                // Calculate file-specific percentage
                let file_dead_percentage = self
                    .calculate_file_percentage(&file_path, &dead_items)
                    .unwrap_or(0.0);

                FileDeadCode {
                    file_path,
                    dead_items,
                    file_dead_percentage,
                }
            })
            .collect()
    }

    /// Calculate dead code percentage for a specific file
    fn calculate_file_percentage(&self, file_path: &Path, dead_items: &[DeadItem]) -> Result<f64> {
        let full_path = if file_path.is_absolute() {
            file_path.to_path_buf()
        } else {
            self.project_path.join(file_path)
        };

        if !full_path.exists() {
            return Ok(0.0);
        }

        let content =
            std::fs::read_to_string(&full_path).context("Failed to read file for line counting")?;

        let total_lines = content.lines().count();
        if total_lines == 0 {
            return Ok(0.0);
        }

        // Estimate dead lines (approximate 3-5 lines per item)
        let estimated_dead_lines = dead_items.len() * 4;
        let percentage = (estimated_dead_lines as f64 / total_lines as f64) * 100.0;

        Ok(percentage.min(100.0))
    }

    /// Calculate overall metrics
    async fn calculate_metrics(&self, files: Vec<FileDeadCode>) -> Result<AccurateDeadCodeReport> {
        let mut total_lines = 0;
        let mut dead_lines = 0;
        let mut dead_by_type = HashMap::new();
        let total_dead_items = files.iter().map(|f| f.dead_items.len()).sum();

        // Count lines in all Rust files
        for entry in walkdir::WalkDir::new(&self.project_path)
            .into_iter()
            .filter_map(std::result::Result::ok)
        {
            let path = entry.path();

            // Skip target directory and non-Rust files
            if path.starts_with(self.project_path.join("target")) {
                continue;
            }

            if path.extension().and_then(|s| s.to_str()) == Some("rs") {
                if let Ok(content) = std::fs::read_to_string(path) {
                    total_lines += content.lines().count();
                }
            }
        }

        // Count dead lines and categorize by type
        for file in &files {
            for item in &file.dead_items {
                let kind_str = match &item.kind {
                    DeadCodeKind::Function => "function",
                    DeadCodeKind::Method => "method",
                    DeadCodeKind::Struct => "struct",
                    DeadCodeKind::Enum => "enum",
                    DeadCodeKind::Variant => "variant",
                    DeadCodeKind::Field => "field",
                    DeadCodeKind::Constant => "constant",
                    DeadCodeKind::Static => "static",
                    DeadCodeKind::Module => "module",
                    DeadCodeKind::Trait => "trait",
                    DeadCodeKind::TypeAlias => "type_alias",
                    DeadCodeKind::Other(s) => s,
                };

                *dead_by_type.entry(kind_str.to_string()).or_insert(0) += 1;

                // Estimate lines per item type
                let lines = match item.kind {
                    DeadCodeKind::Function | DeadCodeKind::Method => 5,
                    DeadCodeKind::Struct | DeadCodeKind::Enum => 3,
                    _ => 2,
                };
                dead_lines += lines;
            }
        }

        let dead_code_percentage = if total_lines > 0 {
            (dead_lines as f64 / total_lines as f64) * 100.0
        } else {
            0.0
        };

        Ok(AccurateDeadCodeReport {
            files_with_dead_code: files,
            total_dead_items,
            dead_code_percentage,
            total_lines,
            dead_lines,
            dead_by_type,
        })
    }
}

/// Public API for backward compatibility
pub async fn analyze_dead_code(project_path: impl AsRef<Path>) -> Result<AccurateDeadCodeReport> {
    let analyzer = CargoDeadCodeAnalyzer::new(project_path);
    analyzer.analyze().await
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_function_message() {
        let analyzer = CargoDeadCodeAnalyzer::new(".");
        let (name, kind) = analyzer
            .parse_message("function `unused_func` is never used")
            .unwrap();
        assert_eq!(name, "unused_func");
        assert_eq!(kind, DeadCodeKind::Function);
    }

    #[test]
    fn test_parse_struct_message() {
        let analyzer = CargoDeadCodeAnalyzer::new(".");
        let (name, kind) = analyzer
            .parse_message("struct `UnusedStruct` is never constructed")
            .unwrap();
        assert_eq!(name, "UnusedStruct");
        assert_eq!(kind, DeadCodeKind::Struct);
    }

    #[test]
    fn test_parse_field_message() {
        let analyzer = CargoDeadCodeAnalyzer::new(".");
        let (name, kind) = analyzer
            .parse_message("field `data` is never read")
            .unwrap();
        assert_eq!(name, "data");
        assert_eq!(kind, DeadCodeKind::Field);
    }
}

#[cfg(test)]
mod property_tests {
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn basic_property_stability(_input in ".*") {
            // Basic property test for coverage
            prop_assert!(true);
        }

        #[test]
        fn module_consistency_check(_x in 0u32..1000) {
            // Module consistency verification
            prop_assert!(_x < 1001);
        }
    }
}