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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// Parsing and metrics calculation for CargoDeadCodeAnalyzer
// Included from cargo_dead_code_analyzer.rs - shares parent module scope

impl CargoDeadCodeAnalyzer {
    /// 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) {
                        // One scope predicate for both layers. The suppression
                        // walk already consults `is_excluded_source`; the
                        // compiler layer did not, so any target cargo happened
                        // to build (a lib's implicit `cfg(test)` bench target,
                        // say) could put files into the report that the walk
                        // that produced the denominator never opened.
                        if self.is_excluded_source(&item.0) {
                            continue;
                        }
                        dead_items.push(item);
                    }
                } else if code == "unreachable_code" {
                    // rustc's OTHER dead-code lint. It was discarded here, so
                    // nothing on the CLI path could ever produce an unreachable
                    // block and `--include-unreachable` was inert on every
                    // input — a fixture with four statements after a `return`
                    // still printed "Unreachable blocks: 0". Tagged with
                    // `DeadCodeKind::UnreachableCode` so `group_by_file` can
                    // keep it out of `dead_items` and out of every count a
                    // default run prints.
                    if let Some(item) = self.extract_unreachable_item(message) {
                        if self.is_excluded_source(&item.0) {
                            continue;
                        }
                        dead_items.push(item);
                    }
                }
            }
        }

        Ok(dead_items)
    }

    /// Extract an `unreachable_code` finding from a compiler message.
    ///
    /// `parse_message` cannot be reused: its patterns all key off "` is never
    /// used`", and rustc words this lint "unreachable statement" / "unreachable
    /// expression" with no item name. The name is the source line, so the
    /// report can point at something.
    fn extract_unreachable_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 = primary_span["text"]
            .as_array()
            .and_then(|texts| texts.first())
            .and_then(|t| t["text"].as_str())
            .map_or_else(|| format!("line {line}"), |t| t.trim().to_string());

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

    /// 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,
            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, items)| {
                // SPLIT, DO NOT MERGE: unreachable findings must not reach any
                // counter a default run prints, so they leave `dead_items`
                // here — before the percentage, the estimated line counts and
                // `dead_by_type` are computed from it.
                let (unreachable_items, dead_items): (Vec<DeadItem>, Vec<DeadItem>) = items
                    .into_iter()
                    .partition(|item| item.kind == DeadCodeKind::UnreachableCode);

                // Measure the file once and carry BOTH numbers. The percentage
                // used to be derived from a line count that was then thrown
                // away, and the renderer substituted the constant 100.
                let total_lines = self.count_file_lines(&file_path);
                let file_dead_percentage = file_percentage(total_lines, &dead_items);

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

    /// Physical line count for a file, or `None` when it cannot be read.
    ///
    /// `None` means "not measured" and must never be rendered as a number —
    /// see `contracts/pmat-no-fabrication-v1.yaml`, `measured_or_absent`.
    fn count_file_lines(&self, file_path: &Path) -> Option<usize> {
        let full_path = if file_path.is_absolute() {
            file_path.to_path_buf()
        } else {
            self.project_path.join(file_path)
        };

        std::fs::read_to_string(&full_path)
            .ok()
            .map(|content| content.lines().count())
    }

    /// Calculate overall metrics
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    async fn calculate_metrics(&self, files: Vec<FileDeadCode>) -> Result<AccurateDeadCodeReport> {
        let mut total_lines = 0;
        let mut total_files = 0;
        let mut project_files = 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. Use ignore::WalkBuilder so the walk
        // respects .gitignore and skips hidden dirs (e.g. `.claude/worktrees/`
        // git-worktree copies) — a raw walkdir here counted ~26M lines across
        // worktree duplicates, so the `total_files_analyzed` estimate
        // (total_lines / 100) ballooned to ~263k instead of ~4.2k.
        for entry in ignore::WalkBuilder::new(&self.project_path)
            .max_depth(Some(self.max_depth)) // limit traversal depth
            .hidden(true)
            .git_ignore(true)
            .git_global(true)
            .build()
            .filter_map(std::result::Result::ok)
        {
            let path = entry.path();

            // Belt-and-suspenders: also skip target/ explicitly.
            if path.starts_with(self.project_path.join("target")) {
                continue;
            }

            if path.extension().and_then(|s| s.to_str()) == Some("rs") {
                project_files += 1;
                // Layer 1 stopped scanning the excluded trees (#915), but this
                // walk kept counting them, so the totals described a wider set
                // than the dead items did: a default run printed "4273 files
                // analyzed, 0 with dead code" over 1236 test files it never
                // opened, and divided the dead lines by their lines too.
                if self.is_excluded_source(path) {
                    continue;
                }
                total_files += 1;
                if let Ok(content) = std::fs::read_to_string(path) {
                    total_lines += content.lines().count();
                }
            }
        }

        // Count dead lines and categorize by type. The line estimate uses the
        // SAME per-kind weights as the per-file figure (`estimated_dead_lines`)
        // — the two used to disagree (project total 94 from 5/3/2 weights vs
        // 76 from `items * 4` summed over the listed files), so the summary
        // contradicted the list underneath it.
        for file in &files {
            for item in &file.dead_items {
                let kind_str = dead_code_kind_to_str(&item.kind);
                *dead_by_type.entry(kind_str.to_string()).or_insert(0) += 1;
            }
            // Bounded by the file's own length. `estimated_dead_lines` charges
            // 5 lines per dead function, which is an estimate from an item
            // count, not a measured span — four dead one-line functions in a
            // five-line file estimated 20 dead lines. Unbounded, that summed
            // into a project figure of 400%, which `--fail-on-violation`
            // compared against its threshold and printed as a percentage. A
            // file cannot contain more dead lines than lines, so the bound goes
            // here, at the accumulation, rather than as a clamp on the ratio:
            // clamping the output would still leave `dead_lines` itself larger
            // than the code it describes.
            dead_lines += estimated_dead_lines_bounded(&file.dead_items, file.total_lines);
        }

        let dead_code_percentage = if total_lines > 0 {
            #[allow(clippy::cast_precision_loss)]
            let pct = (dead_lines as f64 / total_lines as f64) * 100.0;
            // The same ceiling `file_percentage` already applies. It was missing
            // here, so the project figure was the one surface that could report
            // an impossible percentage.
            pct.min(100.0)
        } else {
            0.0
        };

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

/// Estimated dead lines for a set of dead items.
///
/// This is the single estimator for the whole command: a function or method is
/// charged 5 lines, a struct or enum 3, anything else 2. It is an estimate of
/// lines from a measured item count, not a measured line span — the summary and
/// the per-file rows must at least agree with each other.
pub(crate) fn estimated_dead_lines(items: &[DeadItem]) -> usize {
    items
        .iter()
        .map(|item| match item.kind {
            DeadCodeKind::Function | DeadCodeKind::Method => 5,
            DeadCodeKind::Struct | DeadCodeKind::Enum => 3,
            _ => 2,
        })
        .sum()
}

/// Estimated dead lines for one file, bounded by that file's own length.
///
/// The bound lives here rather than at the call sites because it is part of what
/// the estimate MEANS: `estimated_dead_lines` charges 5 lines per dead function
/// from an item count, not a measured span, so four dead one-line functions in a
/// five-line file estimate 20. Bounding at one call site and not the other is
/// how the summary came to print "Total dead lines: 20" for a 5-line file while
/// the project percentage had already been capped. `None` means the length is
/// unknown, and the raw estimate is the best available answer.
pub(crate) fn estimated_dead_lines_bounded(items: &[DeadItem], total_lines: Option<usize>) -> usize {
    let estimate = estimated_dead_lines(items);
    total_lines.map_or(estimate, |lines| estimate.min(lines))
}

/// Dead-code percentage for one file: estimated dead lines over the file's
/// measured line count. `0.0` when the line count is unavailable — the caller
/// reports `total_lines: null` alongside, so the zero is not read as a
/// measurement of "no dead code".
fn file_percentage(total_lines: Option<usize>, items: &[DeadItem]) -> f64 {
    match total_lines {
        Some(lines) if lines > 0 => {
            #[allow(clippy::cast_precision_loss)]
            let pct = (estimated_dead_lines(items) as f64 / lines as f64) * 100.0;
            pct.min(100.0)
        }
        _ => 0.0,
    }
}

/// Convert DeadCodeKind to string representation
fn dead_code_kind_to_str(kind: &DeadCodeKind) -> &str {
    match 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::UnreachableCode => "unreachable",
        DeadCodeKind::Other(s) => s,
    }
}

#[cfg(test)]
mod unreachable_lint_tests {
    //! rustc reports UNUSED items and UNREACHABLE statements as two different
    //! lints. `parse_cargo_warnings` matched only `dead_code`, so nothing on the
    //! CLI path could ever produce an unreachable block and
    //! `analyze dead-code --include-unreachable` was inert on every input.
    use super::*;

    fn diagnostic(code: &str, message: &str, file: &str) -> String {
        serde_json::json!({
            "reason": "compiler-message",
            "message": {
                "code": { "code": code },
                "message": message,
                "spans": [{
                    "is_primary": true,
                    "file_name": file,
                    "line_start": 3,
                    "column_start": 5,
                    "text": [{ "text": "    let y = x * 2;" }],
                }],
            }
        })
        .to_string()
    }

    #[test]
    fn unreachable_code_warnings_are_collected_and_tagged() {
        let analyzer = CargoDeadCodeAnalyzer::new(std::path::Path::new("."));
        let output = format!(
            "{}\n{}\n",
            diagnostic("unreachable_code", "unreachable statement", "src/lib.rs"),
            diagnostic("dead_code", "function `helper` is never used", "src/lib.rs"),
        );

        let items = analyzer
            .parse_cargo_warnings(&output)
            .expect("parse cargo output");

        let unreachable: Vec<_> = items
            .iter()
            .filter(|(_, i)| i.kind == DeadCodeKind::UnreachableCode)
            .collect();
        assert_eq!(
            unreachable.len(),
            1,
            "the unreachable_code warning was dropped: {items:?}"
        );
        assert_eq!(unreachable[0].1.line, 3);
        assert_eq!(
            unreachable[0].1.name, "let y = x * 2;",
            "the source line is the only name rustc gives this lint"
        );
    }

    /// The split is what keeps a default run byte-identical: an unreachable
    /// finding must never reach `dead_items`, which every count and every
    /// estimated line total is computed from.
    #[test]
    fn grouping_keeps_unreachable_out_of_dead_items() {
        let analyzer = CargoDeadCodeAnalyzer::new(std::path::Path::new("."));
        let path = PathBuf::from("src/lib.rs");
        let items = vec![
            (
                path.clone(),
                DeadItem {
                    name: "helper".to_string(),
                    kind: DeadCodeKind::Function,
                    line: 10,
                    column: 1,
                    message: "`helper` is never used".to_string(),
                },
            ),
            (
                path.clone(),
                DeadItem {
                    name: "let y = 1;".to_string(),
                    kind: DeadCodeKind::UnreachableCode,
                    line: 3,
                    column: 5,
                    message: "unreachable statement".to_string(),
                },
            ),
        ];

        let grouped = analyzer.group_by_file(items);
        assert_eq!(grouped.len(), 1);
        assert_eq!(grouped[0].dead_items.len(), 1);
        assert_eq!(grouped[0].unreachable_items.len(), 1);
        assert_eq!(
            estimated_dead_lines(&grouped[0].dead_items),
            5,
            "an unreachable statement must not be charged as dead lines"
        );
    }
}