slopguard-cli 0.1.27

CLI tool to catch AI-generated code patterns via static analysis
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
//! The `stats` subcommand: aggregate findings into a distribution report.

use std::collections::HashMap;
use std::env;
use std::io;
use std::path::{Path, PathBuf};

use serde::Serialize;

use slopguard_core::config::OutputFormat;
use slopguard_core::escalation::apply_escalation;
use slopguard_core::finding::ScanResult;
use slopguard_core::rule::{Category, Language, Severity};

use crate::baseline_cmd::apply_baseline;
use crate::cli::Format;
use crate::output::stats::{format_stats_json, format_stats_text};
use crate::{collect_findings, language_for_path, AppError, CollectOpts};

/// How many rules the report keeps in `top_rules`.
pub const TOP_RULES: usize = 10;
/// How many files the report keeps in `top_files`.
pub const TOP_FILES: usize = 5;

#[derive(Debug, Clone, Default, Serialize)]
pub struct BySeverity {
    pub error: usize,
    pub warning: usize,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct ByCategory {
    pub slop: usize,
    pub security: usize,
    pub correctness: usize,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct ByLanguage {
    pub rust: usize,
    pub typescript: usize,
    /// Findings in files with no known rule language. Normally 0, omitted then.
    #[serde(skip_serializing_if = "is_zero")]
    pub other: usize,
}

fn is_zero(n: &usize) -> bool {
    *n == 0
}

#[derive(Debug, Clone, Serialize)]
pub struct RuleCount {
    pub rule_id: String,
    pub count: usize,
}

#[derive(Debug, Clone, Serialize)]
pub struct FileCount {
    pub file: String,
    pub count: usize,
}

#[derive(Debug, Clone, Serialize)]
pub struct StatsReport {
    pub total: usize,
    pub files_scanned: usize,
    pub baseline_filtered: usize,
    pub by_severity: BySeverity,
    pub by_category: ByCategory,
    pub by_language: ByLanguage,
    pub top_rules: Vec<RuleCount>,
    pub top_files: Vec<FileCount>,
}

/// Aggregate a scan result into the distribution report `stats` prints.
///
/// Totals come from `result.stats` rather than from the findings vector, so
/// the report always agrees with the summary line `scan` prints for the same
/// run. Call this after `apply_baseline` so the counts are post-filter.
pub fn compute_report(result: &ScanResult) -> StatsReport {
    let mut by_severity = BySeverity::default();
    let mut by_category = ByCategory::default();
    let mut by_language = ByLanguage::default();
    let mut rule_tally: HashMap<&str, usize> = HashMap::new();
    let mut file_tally: HashMap<&Path, usize> = HashMap::new();

    for f in &result.findings {
        match f.severity {
            Severity::Error => by_severity.error += 1,
            Severity::Warning => by_severity.warning += 1,
        }
        match f.category {
            Category::Slop => by_category.slop += 1,
            Category::Security => by_category.security += 1,
            Category::Correctness => by_category.correctness += 1,
        }
        match language_for_path(&f.file) {
            Some(Language::Rust) => by_language.rust += 1,
            Some(Language::TypeScript) => by_language.typescript += 1,
            None => by_language.other += 1,
        }
        *rule_tally.entry(f.rule_id.as_str()).or_insert(0) += 1;
        *file_tally.entry(f.file.as_path()).or_insert(0) += 1;
    }

    let mut top_rules = Vec::with_capacity(rule_tally.len());
    for (rule_id, count) in rule_tally {
        top_rules.push(RuleCount {
            rule_id: rule_id.to_string(),
            count,
        });
    }
    // Count descending, then rule id ascending so ties are deterministic.
    top_rules.sort_by(|a, b| (b.count, &a.rule_id).cmp(&(a.count, &b.rule_id)));
    top_rules.truncate(TOP_RULES);

    let mut top_files = Vec::with_capacity(file_tally.len());
    for (file, count) in file_tally {
        top_files.push(FileCount {
            file: file.display().to_string(),
            count,
        });
    }
    top_files.sort_by(|a, b| (b.count, &a.file).cmp(&(a.count, &b.file)));
    top_files.truncate(TOP_FILES);

    StatsReport {
        total: result.stats.total,
        files_scanned: result.stats.files_scanned,
        baseline_filtered: result.stats.baseline_filtered,
        by_severity,
        by_category,
        by_language,
        top_rules,
        top_files,
    }
}

pub struct StatsOpts {
    pub paths: Vec<PathBuf>,
    pub format: Option<Format>,
    pub config_path: Option<PathBuf>,
    pub no_colors: bool,
    pub cli_disable: Vec<String>,
    pub cli_enable: Vec<String>,
    pub rule_filter: Option<String>,
    pub no_cache: bool,
    pub cache_dir: Option<PathBuf>,
    pub no_ai: bool,
    pub no_baseline: bool,
    pub baseline_path: Option<PathBuf>,
    pub no_escalation: bool,
    pub offline: bool,
}

/// Run the same pipeline as `scan`, then print the aggregated distribution
/// instead of the individual findings.
///
/// Always exits 0 when it ran successfully, findings or not: like `list` and
/// `explain` this is a reporting command, and a non-zero exit would break
/// `slopguard stats --format json | jq ...` under `set -o pipefail`.
pub fn run_stats(opts: StatsOpts) -> Result<(), AppError> {
    let StatsOpts {
        paths,
        format,
        config_path,
        no_colors,
        cli_disable,
        cli_enable,
        rule_filter,
        no_cache,
        cache_dir,
        no_ai,
        no_baseline,
        baseline_path,
        no_escalation,
        offline,
    } = opts;
    let use_colors = !no_colors && env::var_os("NO_COLOR").is_none();

    if let Some(Format::Sarif) = format {
        // CLI user-facing diagnostic on stderr, not application logging.
        // slopguard-disable-next-line no-println-in-prod
        eprintln!("error: SARIF format is not supported for stats");
        return Err(AppError::Io(io::Error::new(
            io::ErrorKind::InvalidInput,
            "SARIF format is not supported for stats",
        )));
    }

    if let Some(Format::Html) = format {
        return Err(AppError::Io(io::Error::new(
            io::ErrorKind::InvalidInput,
            "HTML format is not supported for stats",
        )));
    }

    let (mut result, config) = collect_findings(CollectOpts {
        paths,
        config_path,
        cli_disable,
        cli_enable,
        cli_test_paths: Vec::new(),
        rule_filter,
        no_cache,
        cache_dir,
        no_ai,
        report_unused: false,
        diff: false,
        diff_base: None,
        offline,
    })?;

    let _baseline_active = apply_baseline(&mut result, no_baseline, baseline_path)?;

    // Same pipeline position as `scan`, so the aggregated severity counts
    // match what `scan` reports for the same run.
    if !no_escalation {
        apply_escalation(&mut result, &config.escalation);
    }

    // A config-level `format = "sarif"` or `"html"` falls back to text rather
    // than failing: only an explicit `--format sarif|html` is an error.
    let format = format.unwrap_or(match config.output.format {
        OutputFormat::Text => Format::Text,
        OutputFormat::Json => Format::Json,
        OutputFormat::Sarif | OutputFormat::Html => Format::Text,
    });

    let report = compute_report(&result);

    let stdout = io::stdout();
    let mut out = stdout.lock();
    match format {
        Format::Json => format_stats_json(&report, &mut out)?,
        Format::Text | Format::Sarif | Format::Html => {
            format_stats_text(&report, &mut out, use_colors)?
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use slopguard_core::finding::{Finding, ScanResult, ScanStats};
    use slopguard_core::rule::{Category, Severity};

    use super::compute_report;

    fn finding(rule_id: &str, severity: Severity, category: Category, file: &str) -> Finding {
        Finding {
            rule_id: rule_id.into(),
            severity,
            category,
            message: "m".to_string(),
            note: None,
            fix: None,
            file: PathBuf::from(file),
            line: 1,
            column: 1,
            end_line: 1,
            end_column: 2,
            matched_text: "x".to_string(),
            confidence: None,
            escalated: false,
        }
    }

    fn result_of(findings: Vec<Finding>) -> ScanResult {
        let mut errors = 0;
        let mut warnings = 0;
        for f in &findings {
            match f.severity {
                Severity::Error => errors += 1,
                Severity::Warning => warnings += 1,
            }
        }
        ScanResult {
            findings,
            stats: ScanStats {
                errors,
                warnings,
                total: errors + warnings,
                files_scanned: 3,
                baseline_filtered: 0,
                diff_base: None,
                files_changed: None,
            },
            cache_stats: None,
        }
    }

    #[test]
    fn counts_by_severity() {
        let result = result_of(vec![
            finding("a", Severity::Error, Category::Slop, "a.rs"),
            finding("b", Severity::Error, Category::Slop, "b.rs"),
            finding("c", Severity::Warning, Category::Slop, "c.rs"),
        ]);

        let report = compute_report(&result);
        assert_eq!(report.by_severity.error, 2);
        assert_eq!(report.by_severity.warning, 1);
    }

    #[test]
    fn counts_by_category() {
        let result = result_of(vec![
            finding("a", Severity::Warning, Category::Slop, "a.rs"),
            finding("b", Severity::Warning, Category::Security, "b.rs"),
            finding("c", Severity::Warning, Category::Correctness, "c.rs"),
        ]);

        let report = compute_report(&result);
        assert_eq!(report.by_category.slop, 1);
        assert_eq!(report.by_category.security, 1);
        assert_eq!(report.by_category.correctness, 1);
    }

    #[test]
    fn counts_by_language_from_extension() {
        let result = result_of(vec![
            finding("a", Severity::Warning, Category::Slop, "a.rs"),
            finding("b", Severity::Warning, Category::Slop, "b.ts"),
            finding("c", Severity::Warning, Category::Slop, "c.tsx"),
        ]);

        let report = compute_report(&result);
        assert_eq!(report.by_language.rust, 1);
        assert_eq!(report.by_language.typescript, 2);
        assert_eq!(report.by_language.other, 0);
    }

    #[test]
    fn unknown_extension_counts_as_other() {
        let f = finding("a", Severity::Warning, Category::Slop, "a.py");

        let report = compute_report(&result_of(vec![f]));
        assert_eq!(report.by_language.other, 1);
        assert_eq!(report.by_language.rust, 0);
    }

    #[test]
    fn top_rules_capped_at_ten() {
        let mut findings = Vec::new();
        for i in 0..12 {
            let id = format!("rule-{i:02}");
            findings.push(finding(&id, Severity::Warning, Category::Slop, "a.rs"));
        }

        let report = compute_report(&result_of(findings));
        assert_eq!(report.top_rules.len(), 10);
    }

    #[test]
    fn top_files_capped_at_five() {
        let mut findings = Vec::new();
        for i in 0..7 {
            let file = format!("file-{i}.rs");
            findings.push(finding("a", Severity::Warning, Category::Slop, &file));
        }

        let report = compute_report(&result_of(findings));
        assert_eq!(report.top_files.len(), 5);
    }

    #[test]
    fn top_rules_sorted_by_count_then_id() {
        let mut findings = Vec::new();
        for _ in 0..2 {
            findings.push(finding("b", Severity::Warning, Category::Slop, "a.rs"));
            findings.push(finding("a", Severity::Warning, Category::Slop, "a.rs"));
        }
        for _ in 0..3 {
            findings.push(finding("c", Severity::Warning, Category::Slop, "a.rs"));
        }

        let report = compute_report(&result_of(findings));
        assert_eq!(report.top_rules[0].rule_id, "c");
        assert_eq!(report.top_rules[1].rule_id, "a");
        assert_eq!(report.top_rules[2].rule_id, "b");
    }

    #[test]
    fn empty_result_has_zeroed_axes() {
        let report = compute_report(&result_of(Vec::new()));

        assert_eq!(report.total, 0);
        assert_eq!(report.by_severity.error, 0);
        assert_eq!(report.by_severity.warning, 0);
        assert_eq!(report.by_category.slop, 0);
        assert_eq!(report.by_category.security, 0);
        assert_eq!(report.by_category.correctness, 0);
        assert_eq!(report.by_language.rust, 0);
        assert_eq!(report.by_language.typescript, 0);
        assert!(report.top_rules.is_empty());
        assert!(report.top_files.is_empty());
    }

    #[test]
    fn report_uses_scan_stats_for_totals() {
        let f = finding("a", Severity::Warning, Category::Slop, "a.rs");
        let mut result = result_of(vec![f]);
        result.stats.baseline_filtered = 4;

        let report = compute_report(&result);
        assert_eq!(report.baseline_filtered, 4);
        assert_eq!(report.total, result.stats.total);
        assert_eq!(report.files_scanned, 3);
    }
}