gruff-rs 0.1.0

Rust static analyzer and quality linter for CI: dead-code, complexity, security, secrets, and architecture rules with deterministic SARIF/JSON output and baseline support.
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
use super::*;

#[test]
pub(crate) fn sensitive_data_rules_skip_common_placeholder_and_detector_contexts() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    baseline_with_lib(
        dir.path(),
        r#"/// Probe.
pub fn runtime_secret_values(secret_access_key: String, output: Vec<u8>) {
    let secret_access_key = secret_access_key.trim().to_string();
    let secret_json = String::from_utf8_lossy(&output);
    println!("{secret_access_key} {secret_json}");
}
"#,
    );
    fs::create_dir_all(dir.path().join(".github/workflows")).expect("workflow dir");
    fs::create_dir_all(dir.path().join("scripts")).expect("scripts dir");
    fs::create_dir_all(dir.path().join("src-tauri")).expect("src-tauri dir");
    fs::write(
        dir.path().join(".env.example"),
        r#"GITHUB_PAT=your_github_pat_here
AWS_DEV_SECRET_ACCESS_KEY=your_dev_secret_access_key_here
NPM_AUTH_TOKEN=your_npm_auth_token_here
"#,
    )
    .expect("env example write");
    fs::write(
        dir.path().join(".github/workflows/release.yml"),
        r#"env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
"#,
    )
    .expect("workflow write");
    fs::write(
        dir.path().join("package-lock.json"),
        r#"{"packages":{"node_modules/demo":{"dependencies":{"js-tokens":"^4.0.0"}}}}"#,
    )
    .expect("package lock write");
    fs::write(
        dir.path().join("src-tauri/Cargo.toml"),
        r#"[dependencies]
aws-sdk-secretsmanager = "1"
"#,
    )
    .expect("tauri manifest write");
    fs::write(
        dir.path().join("scripts/oss-gate-check.sh"),
        r#"SECRET_PATTERNS=(
  '-----BEGIN PRIVATE KEY-----'
  '-----BEGIN RSA PRIVATE KEY-----'
  '-----BEGIN EC PRIVATE KEY-----'
)
"#,
    )
    .expect("detector script write");

    let report = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from(".")],
            no_config: true,
            no_baseline: true,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    for rule in [
        "sensitive-data.hardcoded-env-value",
        "sensitive-data.private-key",
    ] {
        let findings: Vec<&Finding> = report
            .findings
            .iter()
            .filter(|finding| finding.rule_id == rule)
            .collect();
        assert!(
            findings.is_empty(),
            "{rule} must skip placeholders, runtime values, dependency names, and detector patterns; findings={findings:?}"
        );
    }
}

#[test]
pub(crate) fn public_field_skips_serde_transport_structs() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    baseline_with_lib(
        dir.path(),
        r#"use serde::{Deserialize, Serialize};

/// API response DTO.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiResponse {
    pub id: String,
    pub status: String,
}
"#,
    );
    let report = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from(".")],
            no_config: true,
            no_baseline: true,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    let public_field_findings: Vec<&Finding> = report
        .findings
        .iter()
        .filter(|finding| finding.rule_id == "modernisation.public-field")
        .collect();
    assert!(
        public_field_findings.is_empty(),
        "serde DTO public fields must stay silent; findings={public_field_findings:?}"
    );
}

#[test]
pub(crate) fn dead_code_unused_private_function_recognises_indirect_references() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    baseline_with_lib(
        dir.path(),
        r#"use serde::Deserialize;

fn check_ai_tool(value: &i32) -> bool {
    *value > 0
}

fn default_branch() -> String {
    "main".to_string()
}

#[derive(Deserialize)]
pub struct ForgeConfig {
    #[serde(default = "default_branch")]
    pub branch: String,
}

impl std::fmt::Debug for ForgeConfig {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.debug_struct("ForgeConfig").finish()
    }
}

pub fn entry(values: &[i32]) -> Vec<bool> {
    values.iter().map(check_ai_tool).collect()
}
"#,
    );
    let report = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from(".")],
            no_config: true,
            no_baseline: true,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    for symbol in ["check_ai_tool", "default_branch", "fmt"] {
        assert!(
            !report.findings.iter().any(|finding| {
                finding.rule_id == "dead-code.unused-private-function"
                    && finding.symbol.as_deref() == Some(symbol)
            }),
            "dead-code.unused-private-function must recognise indirect reference `{symbol}`; findings={:?}",
            report
                .findings
                .iter()
                .map(|finding| (&finding.rule_id, finding.symbol.as_deref(), finding.line))
                .collect::<Vec<_>>()
        );
    }
}

#[test]
pub(crate) fn file_length_skips_dependency_lockfiles() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    baseline_with_lib(dir.path(), "/// Probe.\npub fn entry() {}\n");
    let mut cargo_lock = String::from("# This is intentionally large lockfile metadata.\n");
    let mut package_lock = String::from("{\n");
    for index in 0..620 {
        cargo_lock.push_str(&format!("# package row {index}\n"));
        package_lock.push_str(&format!("  \"package-{index}\": \"1.0.0\",\n"));
    }
    package_lock.push_str("  \"tail\": \"1.0.0\"\n}\n");
    fs::write(dir.path().join("Cargo.lock"), cargo_lock).expect("cargo lock write");
    fs::write(dir.path().join("package-lock.json"), package_lock).expect("package lock write");

    let report = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from(".")],
            no_config: true,
            no_baseline: true,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    let lockfile_size_findings: Vec<&Finding> = report
        .findings
        .iter()
        .filter(|finding| {
            finding.rule_id == "size.file-length"
                && matches!(
                    finding.file_path.as_str(),
                    "Cargo.lock" | "package-lock.json"
                )
        })
        .collect();
    assert!(
        lockfile_size_findings.is_empty(),
        "dependency lockfiles must not produce file-length findings; findings={lockfile_size_findings:?}"
    );
}

#[test]
pub(crate) fn short_variable_skips_single_letter_bindings() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    baseline_with_lib(
        dir.path(),
        r#"/// Probe.
pub fn entry(values: &[String]) -> Vec<String> {
    values
        .iter()
        .map(|s| s.trim())
        .filter_map(|v| v.parse::<u32>().map_err(|e| e.to_string()).ok())
        .map(|n| n.to_string())
        .collect()
}
"#,
    );
    let report = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from(".")],
            no_config: true,
            no_baseline: true,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    let short_names: Vec<&Finding> = report
        .findings
        .iter()
        .filter(|finding| finding.rule_id == "naming.short-variable")
        .collect();
    assert!(
        short_names.is_empty(),
        "single-letter closure/error bindings must stay silent; findings={short_names:?}"
    );
}

#[test]
pub(crate) fn performance_loop_rules_ignore_loop_words_in_comments() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    baseline_with_lib(
        dir.path(),
        r#"/// Load favorites for a workspace.
pub fn load_favorites(path: &std::path::Path) -> Result<String, String> {
    std::fs::read_to_string(path).map_err(|e| format!("Failed to read favorites: {}", e))
}

/// Resize while preserving the terminal state.
pub fn ai_resize_chat(session_id: String) -> Result<(), String> {
    Err(format!("No active chat session '{}'", session_id))
}

/// Cancel for reset mode only.
pub fn forge_cancel(current_distro: Option<String>) -> Option<String> {
    let distro = current_distro.clone();
    distro
}
"#,
    );
    let report = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from(".")],
            no_config: true,
            no_baseline: true,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    for rule in ["performance.format-in-loop", "performance.clone-in-loop"] {
        assert!(
            !report
                .findings
                .iter()
                .any(|finding| finding.rule_id == rule),
            "{rule} must ignore loop keywords that appear only in comments; findings={:?}",
            report
                .findings
                .iter()
                .map(|finding| (&finding.rule_id, finding.symbol.as_deref(), finding.line))
                .collect::<Vec<_>>()
        );
    }
}

#[test]
pub(crate) fn format_in_loop_skips_static_probe_and_report_message_construction() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    baseline_with_lib(
        dir.path(),
        r#"const AI_TOOL_SPECS: &[&str] = &["claude", "codex"];

/// Build a bounded probe script from known tool specs.
pub fn build_wsl_batch_probe_script() -> String {
    let mut lines = vec!["set -e".to_string()];
    for spec in AI_TOOL_SPECS {
        lines.push(format!("check_tool {}", spec));
    }
    lines.join("\n")
}

/// Build user-facing security group findings.
pub fn scan_security_groups(rules: &[(i32, i32)]) -> Vec<String> {
    let mut findings = Vec::new();
    for (from_port, to_port) in rules {
        let port_desc = if from_port == to_port {
            from_port.to_string()
        } else {
            format!("{from_port}-{to_port}")
        };
        let port_label = match *from_port {
            22 => format!("{port_desc} (SSH)"),
            3389 => format!("{port_desc} (RDP)"),
            _ => port_desc,
        };
        findings.push(
            format!("Port {port_label} open to 0.0.0.0/0"),
        );
    }
    findings
}

/// Build dynamic per-item output.
pub fn dynamic_format_loop(values: &[String]) -> Vec<String> {
    let mut output = Vec::new();
    for value in values {
        output.push(format!("{}", value));
    }
    output
}
"#,
    );
    let report = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from(".")],
            no_config: true,
            no_baseline: true,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");

    for symbol in ["build_wsl_batch_probe_script", "scan_security_groups"] {
        assert!(
            !report.findings.iter().any(|finding| {
                finding.rule_id == "performance.format-in-loop"
                    && finding.symbol.as_deref() == Some(symbol)
            }),
            "bounded static probes and report message construction must stay silent for `{symbol}`; findings={:?}",
            report
                .findings
                .iter()
                .map(|finding| (&finding.rule_id, finding.symbol.as_deref(), finding.line))
                .collect::<Vec<_>>()
        );
    }
    assert!(
        report.findings.iter().any(|finding| {
            finding.rule_id == "performance.format-in-loop"
                && finding.symbol.as_deref() == Some("dynamic_format_loop")
        }),
        "dynamic same-line push(format!(...)) loops must still be reported; findings={:?}",
        report
            .findings
            .iter()
            .map(|finding| (&finding.rule_id, finding.symbol.as_deref(), finding.line))
            .collect::<Vec<_>>()
    );
}

#[test]
pub(crate) fn external_public_module_declaration_uses_module_file_docs() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    baseline_with_lib(
        dir.path(),
        r#"//! Root docs.

pub mod commands;
"#,
    );
    fs::write(
        dir.path().join("src/commands.rs"),
        r#"//! Command module docs.

/// Entry command.
pub fn entry() {}
"#,
    )
    .expect("commands module write");
    let report = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from(".")],
            no_config: true,
            no_baseline: true,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    assert!(
        !report.findings.iter().any(|finding| {
            finding.rule_id == "docs.missing-public-doc"
                && finding.symbol.as_deref() == Some("commands")
        }),
        "external module declarations should not require duplicate outer docs; findings={:?}",
        report
            .findings
            .iter()
            .map(|finding| (&finding.rule_id, finding.symbol.as_deref(), finding.line))
            .collect::<Vec<_>>()
    );
}

#[test]
pub(crate) fn unnecessary_clone_candidate_skips_standalone_call_argument() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    baseline_with_lib(
        dir.path(),
        r#"use std::collections::HashMap;

/// Start a chat session.
pub fn start_chat(session_id: String) -> String {
    let mut sessions = HashMap::new();
    sessions.insert(
        session_id.clone(),
        1,
    );
    session_id
}
"#,
    );
    let report = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from(".")],
            no_config: true,
            no_baseline: true,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");
    assert!(
        !report
            .findings
            .iter()
            .any(|finding| finding.rule_id == "waste.unnecessary-clone-candidate"),
        "standalone clone arguments in multi-line calls require ownership context; findings={:?}",
        report
            .findings
            .iter()
            .map(|finding| (&finding.rule_id, finding.symbol.as_deref(), finding.line))
            .collect::<Vec<_>>()
    );
}

#[test]
pub(crate) fn unwrap_in_test_skips_assertion_subject_but_reports_setup_unwrap() {
    let _guard = analysis_lock();
    let dir = tempdir().expect("tempdir");
    baseline_with_lib(
        dir.path(),
        r#"/// Normalize a shell profile id.
pub fn normalize_shell_profile_id(value: Option<String>) -> Result<Option<String>, String> {
    Ok(value.map(|id| if id == "windows" { "powershell".to_string() } else { id }))
}

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

    #[test]
    fn assertion_subject_unwrap() {
        assert_eq!(
            normalize_shell_profile_id(Some("windows".to_string())).unwrap(),
            Some("powershell".to_string())
        );
    }

    #[test]
    fn setup_unwrap_still_reports() {
        let value = normalize_shell_profile_id(Some("windows".to_string())).unwrap();
        assert_eq!(value, Some("powershell".to_string()));
    }
}
"#,
    );
    let report = run_project_analysis(
        dir.path(),
        AnalysisOptions {
            paths: vec![PathBuf::from(".")],
            no_config: true,
            no_baseline: true,
            ..default_test_options()
        },
    )
    .expect("analysis succeeds");

    assert!(
        !report.findings.iter().any(|finding| {
            finding.rule_id == "test-quality.unwrap-in-test"
                && finding.symbol.as_deref() == Some("assertion_subject_unwrap")
        }),
        "unwrap used as the asserted subject should stay silent; findings={:?}",
        report
            .findings
            .iter()
            .map(|finding| (&finding.rule_id, finding.symbol.as_deref(), finding.line))
            .collect::<Vec<_>>()
    );
    assert!(
        report.findings.iter().any(|finding| {
            finding.rule_id == "test-quality.unwrap-in-test"
                && finding.symbol.as_deref() == Some("setup_unwrap_still_reports")
        }),
        "setup unwraps must still be reported; findings={:?}",
        report
            .findings
            .iter()
            .map(|finding| (&finding.rule_id, finding.symbol.as_deref(), finding.line))
            .collect::<Vec<_>>()
    );
}