slopguard-core 0.1.9

Core analysis engine for slopguard
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
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
use std::collections::HashSet;
use std::fmt;
use std::fs::read_to_string;
use std::io::Error as IoError;
use std::path::{Component, Path, PathBuf};
use std::str::{from_utf8, Utf8Error};

use ast_grep_language::SupportLang;
use ignore::WalkBuilder;
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use slopguard_rules::BuiltinRules;
use strum::{Display, EnumIter, EnumString};
use thiserror::Error;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RuleId(pub String);

impl fmt::Display for RuleId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl RuleId {
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl From<String> for RuleId {
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<&str> for RuleId {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

#[derive(Debug, Display, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum Severity {
    Error,
    Warning,
}

#[derive(Debug, Display, EnumIter, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum Language {
    Rust,
    TypeScript,
}

impl Language {
    /// The ast-grep languages a rule written for this language is compiled
    /// against. TypeScript rules also cover TSX files.
    pub fn ast_grep_langs(&self) -> &'static [SupportLang] {
        match self {
            Language::Rust => &[SupportLang::Rust],
            Language::TypeScript => &[SupportLang::TypeScript, SupportLang::Tsx],
        }
    }
}

#[derive(Debug, Display, EnumString, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum Category {
    Slop,
    Security,
    #[default]
    Correctness,
}

impl Category {
    /// Derive the category from the first directory of a rule path relative to
    /// its ruleset root (e.g. `security/no-debug.yml`). Returns `None` when the
    /// path has no directory or the directory is not a known category.
    fn from_rule_path(path: &Path) -> Option<Self> {
        match path.components().next()? {
            Component::Normal(dir) => dir.to_str()?.parse().ok(),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RuleTests {
    #[serde(default)]
    pub should_match: Vec<String>,
    #[serde(default)]
    pub should_not_match: Vec<String>,
}

/// Optional AI confirmation step for a rule. When present, the rule's `rule`
/// AST pattern acts as a cheap pre-filter: matches become candidates that an
/// LLM must confirm before they are reported. The AST layer never executes
/// this field; the AI pipeline in `slopguard-ai` reads it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AiCheck {
    /// Prompt template sent to the model. Supports the variables `{{code}}`,
    /// `{{filename}}`, and `{{rule_context}}`.
    pub prompt: String,
    /// Model override for this rule. Falls back to `[ai].model` in config.
    #[serde(default)]
    pub model: Option<String>,
}

fn default_enabled() -> bool {
    true
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Rule {
    pub id: RuleId,
    pub language: Language,
    pub severity: Severity,
    pub message: String,
    #[serde(default)]
    pub note: Option<String>,
    #[serde(default)]
    pub category: Option<Category>,
    #[serde(default)]
    pub fix: Option<String>,
    pub rule: Value,
    #[serde(default)]
    pub files: Option<Vec<String>>,
    #[serde(default)]
    pub ignores: Option<Vec<String>>,
    /// Drop findings located inside `#[cfg(test)]` blocks of scanned Rust files.
    /// Complements `ignores`, which only filters whole files by path.
    #[serde(default = "default_enabled")]
    pub enabled: bool,
    #[serde(default)]
    pub skip_test_code: bool,
    #[serde(default)]
    pub tests: Option<RuleTests>,
    /// When set, matches of `rule` are candidates confirmed by an LLM before
    /// being reported. See [`AiCheck`]. The AST scanner ignores this field.
    #[serde(default)]
    pub ai_check: Option<AiCheck>,
}

#[derive(Debug, Error)]
pub enum RuleError {
    #[error("failed to parse rule YAML: {0}")]
    Parse(#[from] serde_yaml::Error),

    #[error("rule '{id}': rule field must not be empty")]
    EmptyRule { id: RuleId },

    #[error("duplicate rule id: '{id}'")]
    DuplicateId { id: RuleId },

    #[error("failed to read rule file '{path}': {source}")]
    Io {
        path: String,
        #[source]
        source: IoError,
    },

    #[error("rule file '{path}' is not valid UTF-8: {source}")]
    Utf8 {
        path: String,
        #[source]
        source: Utf8Error,
    },
}

impl RuleError {
    fn io(path: &Path, source: IoError) -> Self {
        Self::Io {
            path: path.display().to_string(),
            source,
        }
    }
}

/// Parse a single YAML string into a Rule, validating required fields.
pub fn parse_rule(yaml: &str) -> Result<Rule, RuleError> {
    let rule: Rule = serde_yaml::from_str(yaml)?;
    if rule.rule.is_null() {
        return Err(RuleError::EmptyRule { id: rule.id });
    }
    Ok(rule)
}

/// Parse a rule and fill in its category from the ruleset-relative path when
/// the YAML omits it.
fn parse_rule_at(yaml: &str, relative_path: &Path) -> Result<Rule, RuleError> {
    let mut rule = parse_rule(yaml)?;
    if rule.category.is_none() {
        rule.category = Category::from_rule_path(relative_path);
    }
    Ok(rule)
}

fn is_rule_file(path: &Path) -> bool {
    matches!(
        path.extension().and_then(|e| e.to_str()),
        Some("yml" | "yaml")
    )
}

/// Load all builtin rules embedded via rust-embed.
pub fn load_builtin_rules() -> Result<Vec<Rule>, RuleError> {
    BuiltinRules::iter()
        .filter(|path| is_rule_file(Path::new(path.as_ref())))
        .filter_map(|path| BuiltinRules::get(&path).map(|file| (path, file)))
        .map(|(path, file)| {
            let yaml = from_utf8(&file.data).map_err(|e| RuleError::Utf8 {
                path: path.to_string(),
                source: e,
            })?;
            parse_rule_at(yaml, Path::new(path.as_ref()))
        })
        .collect()
}

/// Load rules from custom directories on the filesystem. Missing directories
/// are skipped; subdirectories are searched recursively.
pub fn load_custom_rules(dirs: &[PathBuf]) -> Result<Vec<Rule>, RuleError> {
    let mut rules = Vec::new();
    for dir in dirs.iter().filter(|dir| dir.is_dir()) {
        // Custom rule directories are plain file trees: gitignore and
        // hidden-file filtering would silently drop rules.
        for entry in WalkBuilder::new(dir).standard_filters(false).build() {
            let entry = entry.map_err(|e| RuleError::io(dir, IoError::other(e)))?;
            let path = entry.path();
            let is_file = entry.file_type().is_some_and(|t| t.is_file());
            if !is_file || !is_rule_file(path) {
                continue;
            }
            let yaml = read_to_string(path).map_err(|e| RuleError::io(path, e))?;
            let relative = path.strip_prefix(dir).unwrap_or(path);
            rules.push(parse_rule_at(&yaml, relative)?);
        }
    }
    Ok(rules)
}

/// Whether a builtin rule takes part in a scan under `config`.
///
/// A rule listed in `rules.enable` is always active. Otherwise it must be
/// enabled by default, belong to an active ruleset, and not be listed in
/// `rules.disable`.
pub fn is_rule_active(rule: &Rule, config: &crate::config::Config) -> bool {
    let listed = |ids: &[String]| ids.iter().any(|id| id == rule.id.as_str());
    if listed(&config.rules.enable) {
        return true;
    }
    let ruleset_on = match rule.category.as_ref().unwrap_or(&Category::Correctness) {
        Category::Slop => config.rulesets.slop,
        Category::Security => config.rulesets.security,
        Category::Correctness => config.rulesets.correctness,
    };
    ruleset_on && rule.enabled && !listed(&config.rules.disable)
}

/// Load all effective rules based on config: builtin (filtered by rulesets and
/// the enable/disable lists) plus custom rules from configured directories.
pub fn load_effective_rules(config: &crate::config::Config) -> Result<Vec<Rule>, RuleError> {
    let mut rules: Vec<Rule> = load_builtin_rules()?
        .into_iter()
        .filter(|rule| is_rule_active(rule, config))
        .collect();

    let custom = load_custom_rules(&config.rules.custom_dirs)?;
    rules.extend(custom);
    validate_unique_ids(&rules)?;
    Ok(rules)
}

/// Load all rules (builtin + custom) without filtering by activation status.
/// Used by `explain` to look up any rule regardless of config.
pub fn load_all_rules(config: &crate::config::Config) -> Result<Vec<Rule>, RuleError> {
    let mut rules = load_builtin_rules()?;
    let custom = load_custom_rules(&config.rules.custom_dirs)?;
    rules.extend(custom);
    Ok(rules)
}

/// Validate that all rule (id, language) pairs are unique. The same rule id
/// may appear for different languages (e.g. `no-todo-fixme` for both Rust and
/// TypeScript), so a single `rules.disable` entry covers both variants.
pub fn validate_unique_ids(rules: &[Rule]) -> Result<(), RuleError> {
    let mut seen = HashSet::new();
    for rule in rules {
        if !seen.insert((&rule.id, &rule.language)) {
            return Err(RuleError::DuplicateId {
                id: rule.id.clone(),
            });
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::fs::{create_dir, write};

    use tempfile::tempdir;

    use super::*;

    #[test]
    fn parse_valid_rule_all_fields() {
        let yaml = r#"
id: test-rule
language: rust
severity: error
message: "Test message"
note: "Test note"
category: security
fix: "Use X instead"
rule:
  kind: call_expression
  regex: '\.unwrap\(\)\s*$'
files:
  - "**/src/**/*.rs"
ignores:
  - "**/tests/**"
skip_test_code: true
tests:
  should_match:
    - "let x = foo().unwrap();"
  should_not_match:
    - "let x = foo()?;"
"#;
        let rule = parse_rule(yaml).unwrap();
        assert!(rule.skip_test_code);
        assert_eq!(rule.id, RuleId::from("test-rule"));
        assert_eq!(rule.language, Language::Rust);
        assert_eq!(rule.severity, Severity::Error);
        assert_eq!(rule.message, "Test message");
        assert_eq!(rule.note.as_deref(), Some("Test note"));
        assert_eq!(rule.category, Some(Category::Security));
        assert_eq!(rule.fix.as_deref(), Some("Use X instead"));
        assert!(!rule.rule.is_null());
        assert_eq!(rule.files.as_ref().unwrap().len(), 1);
        assert_eq!(rule.ignores.as_ref().unwrap().len(), 1);
        let tests = rule.tests.as_ref().unwrap();
        assert_eq!(tests.should_match.len(), 1);
        assert_eq!(tests.should_not_match.len(), 1);
    }

    #[test]
    fn parse_rule_with_ai_check() {
        let yaml = r#"
id: ai-rule
language: rust
severity: warning
category: slop
message: "needs AI confirmation"
rule:
  kind: line_comment
  regex: 'SAFETY'
ai_check:
  prompt: "Is this SAFETY comment meaningful?\n{{code}}"
  model: "claude-haiku-4-5"
"#;
        let rule = parse_rule(yaml).unwrap();
        let ai = rule.ai_check.as_ref().expect("ai_check should be present");
        assert_eq!(ai.prompt, "Is this SAFETY comment meaningful?\n{{code}}");
        assert_eq!(ai.model.as_deref(), Some("claude-haiku-4-5"));
    }

    #[test]
    fn parse_rule_ai_check_without_model() {
        let yaml = r#"
id: ai-rule-no-model
language: rust
severity: warning
category: slop
message: "needs AI confirmation"
rule:
  kind: line_comment
ai_check:
  prompt: "check {{filename}}"
"#;
        let rule = parse_rule(yaml).unwrap();
        let ai = rule.ai_check.as_ref().unwrap();
        assert_eq!(ai.prompt, "check {{filename}}");
        assert!(
            ai.model.is_none(),
            "model falls back to config when omitted"
        );
    }

    #[test]
    fn parse_minimal_rule() {
        let yaml = r#"
id: minimal-rule
language: rust
severity: warning
message: "Minimal"
rule:
  pattern: $X.unwrap()
"#;
        let rule = parse_rule(yaml).unwrap();
        assert_eq!(rule.id, RuleId::from("minimal-rule"));
        assert_eq!(rule.severity, Severity::Warning);
        assert!(rule.note.is_none());
        assert!(rule.category.is_none());
        assert!(rule.fix.is_none());
        assert!(rule.files.is_none());
        assert!(rule.ignores.is_none());
        assert!(!rule.skip_test_code);
        assert!(rule.tests.is_none());
        assert!(rule.enabled);
        assert!(rule.ai_check.is_none());
    }

    #[test]
    fn rule_activation_precedence() {
        let opt_in = parse_rule(
            r#"
id: opt-in
language: rust
severity: warning
category: slop
enabled: false
message: "Opt-in"
rule:
  pattern: $X.unwrap()
"#,
        )
        .unwrap();
        let mut config = crate::config::Config::default();

        assert!(!is_rule_active(&opt_in, &config), "enabled: false is off");

        config.rules.enable.push("opt-in".to_string());
        assert!(is_rule_active(&opt_in, &config), "enable list turns it on");

        config.rules.disable.push("opt-in".to_string());
        assert!(is_rule_active(&opt_in, &config), "enable wins over disable");

        config.rulesets.slop = false;
        assert!(is_rule_active(&opt_in, &config), "enable wins over ruleset");

        config.rules.enable.clear();
        config.rules.disable.clear();
        config.rulesets.slop = true;
        let mut default_on = opt_in.clone();
        default_on.enabled = true;
        assert!(is_rule_active(&default_on, &config));

        config.rules.disable.push("opt-in".to_string());
        assert!(!is_rule_active(&default_on, &config), "disable list");

        config.rules.disable.clear();
        config.rulesets.slop = false;
        assert!(!is_rule_active(&default_on, &config), "ruleset off");
    }

    #[test]
    fn reject_missing_id() {
        let yaml = r#"
language: rust
severity: error
message: "No id"
rule:
  pattern: $X
"#;
        let err = parse_rule(yaml).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("missing field"),
            "expected missing field error, got: {msg}"
        );
    }

    #[test]
    fn reject_unsupported_language() {
        let yaml = r#"
id: bad-lang
language: python
severity: error
message: "Bad lang"
rule:
  pattern: $X
"#;
        let err = parse_rule(yaml).unwrap_err();
        assert!(matches!(err, RuleError::Parse(_)));
        let msg = err.to_string();
        assert!(
            msg.contains("unknown variant"),
            "expected unknown variant error, got: {msg}"
        );
    }

    #[test]
    fn reject_empty_rule() {
        let yaml = r#"
id: empty-rule
language: rust
severity: error
message: "Empty rule"
rule:
"#;
        let err = parse_rule(yaml).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("must not be empty"),
            "expected empty rule error, got: {msg}"
        );
    }

    #[test]
    fn reject_malformed_yaml() {
        let yaml = "this is: [not: valid: yaml: {{{";
        let err = parse_rule(yaml).unwrap_err();
        assert!(matches!(err, RuleError::Parse(_)));
    }

    #[test]
    fn load_all_builtin_rules() {
        let rules = load_builtin_rules().unwrap();
        assert_eq!(
            rules.len(),
            81,
            "expected 81 builtin rules, got {}",
            rules.len()
        );

        let slop_count = rules
            .iter()
            .filter(|r| r.category == Some(Category::Slop))
            .count();
        let security_count = rules
            .iter()
            .filter(|r| r.category == Some(Category::Security))
            .count();
        let correctness_count = rules
            .iter()
            .filter(|r| r.category == Some(Category::Correctness))
            .count();
        assert_eq!(slop_count, 28, "expected 28 slop rules");
        assert_eq!(security_count, 15, "expected 15 security rules");
        assert_eq!(correctness_count, 38, "expected 38 correctness rules");

        assert!(rules
            .iter()
            .any(|r| r.id == RuleId::from("no-unwrap-in-prod")));
        assert!(rules
            .iter()
            .any(|r| r.id == RuleId::from("no-debug-on-secrets")));
        assert!(rules.iter().any(|r| r.id == RuleId::from("no-slop-words")));

        validate_unique_ids(&rules).unwrap();
    }

    #[test]
    fn load_custom_rules_from_dir() {
        let dir = tempdir().unwrap();
        let rule_yaml = r#"
id: custom-rule
language: rust
severity: warning
message: "Custom rule"
rule:
  pattern: $X.clone()
"#;
        write(dir.path().join("custom.yml"), rule_yaml).unwrap();

        let rules = load_custom_rules(&[dir.path().to_path_buf()]).unwrap();
        assert_eq!(rules.len(), 1);
        assert_eq!(rules[0].id, RuleId::from("custom-rule"));
    }

    #[test]
    fn reject_duplicate_ids_same_language() {
        let rule1 = parse_rule(
            r#"
id: same-id
language: rust
severity: error
message: "First"
rule:
  pattern: $X
"#,
        )
        .unwrap();
        let rule2 = parse_rule(
            r#"
id: same-id
language: rust
severity: warning
message: "Second"
rule:
  pattern: $Y
"#,
        )
        .unwrap();
        let err = validate_unique_ids(&[rule1, rule2]).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("duplicate rule id"),
            "expected duplicate id error, got: {msg}"
        );
        assert!(
            msg.contains("same-id"),
            "expected same-id in error, got: {msg}"
        );
    }

    #[test]
    fn allow_same_id_different_language() {
        let rust_rule = parse_rule(
            r#"
id: shared-rule
language: rust
severity: error
message: "Rust variant"
rule:
  pattern: $X
"#,
        )
        .unwrap();
        let ts_rule = parse_rule(
            r#"
id: shared-rule
language: typescript
severity: error
message: "TypeScript variant"
rule:
  pattern: $X
"#,
        )
        .unwrap();
        validate_unique_ids(&[rust_rule, ts_rule]).unwrap();
    }

    #[test]
    fn category_from_rule_path() {
        let category = |p: &str| Category::from_rule_path(Path::new(p));
        assert_eq!(category("slop/no-slop-words.yml"), Some(Category::Slop));
        assert_eq!(category("security/no-debug.yml"), Some(Category::Security));
        assert_eq!(
            category("correctness/no-unwrap.yml"),
            Some(Category::Correctness)
        );
        assert_eq!(category("unknown/something.yml"), None);
        assert_eq!(category("rule.yml"), None);
    }

    #[test]
    fn all_builtin_rules_valid_messages() {
        for path in BuiltinRules::iter() {
            if !is_rule_file(Path::new(path.as_ref())) {
                continue;
            }
            let file = BuiltinRules::get(&path).unwrap();
            let yaml = from_utf8(&file.data).unwrap();
            let rule: Rule = serde_yaml::from_str(yaml).unwrap();
            assert!(
                !rule.message.contains('\u{2014}'),
                "rule '{}' message contains em dash: {}",
                rule.id,
                rule.message
            );
            assert!(
                !rule.message.contains('\u{2013}'),
                "rule '{}' message contains en dash: {}",
                rule.id,
                rule.message
            );
            assert!(
                rule.message.is_ascii() || rule.message.chars().all(|c| c.is_ascii() || c == '\''),
                "rule '{}' message contains non-ASCII characters: {}",
                rule.id,
                rule.message
            );
        }
    }

    #[test]
    fn all_builtin_rules_have_tests() {
        for path in BuiltinRules::iter() {
            if !is_rule_file(Path::new(path.as_ref())) {
                continue;
            }
            let file = BuiltinRules::get(&path).unwrap();
            let yaml = from_utf8(&file.data).unwrap();
            let rule: Rule = serde_yaml::from_str(yaml).unwrap();
            let tests = rule.tests.as_ref().unwrap_or_else(|| {
                panic!("rule '{}' (at {path}) is missing `tests` block", rule.id)
            });
            assert!(
                tests.should_match.len() >= 2,
                "rule '{}' (at {path}) needs >= 2 should_match, got {}",
                rule.id,
                tests.should_match.len()
            );
            assert!(
                tests.should_not_match.len() >= 2,
                "rule '{}' (at {path}) needs >= 2 should_not_match, got {}",
                rule.id,
                tests.should_not_match.len()
            );
        }
    }

    #[test]
    fn all_builtin_rules_have_explicit_category() {
        for path in BuiltinRules::iter() {
            if !is_rule_file(Path::new(path.as_ref())) {
                continue;
            }
            let file = BuiltinRules::get(&path).unwrap();
            let yaml = from_utf8(&file.data).unwrap();
            let rule: Rule = serde_yaml::from_str(yaml).unwrap();
            assert!(
                rule.category.is_some(),
                "rule '{}' (at {path}) is missing an explicit `category` field",
                rule.id
            );
        }
    }

    #[test]
    fn custom_rules_category_from_subdir() {
        let dir = tempdir().unwrap();
        let security_dir = dir.path().join("security");
        create_dir(&security_dir).unwrap();
        let rule_yaml = r#"
id: custom-sec
language: rust
severity: error
message: "Custom security"
rule:
  pattern: $X
"#;
        write(security_dir.join("rule.yml"), rule_yaml).unwrap();

        let rules = load_custom_rules(&[dir.path().to_path_buf()]).unwrap();
        assert_eq!(rules.len(), 1);
        assert_eq!(rules[0].category, Some(Category::Security));
    }
}