rpm-spec-analyzer 0.1.1

Visitor-based static analyzer library for RPM .spec files
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
//! Bcond hygiene: RPM401 + RPM402.
//!
//! - **RPM401 `bcond-defined-but-unused`** — a `%bcond_with NAME` /
//!   `%bcond_without NAME` / `%bcond NAME DEFAULT` declaration without
//!   any matching `%{with NAME}` / `%{without NAME}` reference. Dead
//!   build toggles confuse the spec audit ("did this flag ever do
//!   anything?") and slow down `mock --without` invocations because
//!   the option silently has no effect.
//! - **RPM402 `with-condition-without-bcond`** — `%{with NAME}` or
//!   `%{without NAME}` referenced where no `%bcond[_with|_without]
//!   NAME` was declared. RPM treats an undeclared `%{with foo}` as
//!   false-ish (the macro expands empty), so the branch silently
//!   never fires — a bug-prone setup the maintainer almost certainly
//!   didn't intend.
//!
//! Both rules walk the spec once via the [`Visit`] trait to collect
//! the declared and referenced bcond names, then emit the two
//! set-difference diagnostics. Names are compared verbatim — that's
//! how RPM resolves them at build time.

use std::collections::BTreeMap;

use rpm_spec::ast::{
    BoolDep, BuildCondStyle, BuildCondition, CondExpr, Conditional, DepExpr, ExprAst, MacroRef,
    PreambleContent, PreambleItem, Section, Span, SpecFile, SpecItem, TagValue, Text, TextSegment,
};

use crate::diagnostic::{Diagnostic, LintCategory, Severity};
use crate::lint::{Lint, LintMetadata};
use crate::visit::Visit;

/// Macro head names that mark a `%{with NAME}` / `%{without NAME}`
/// reference. Used both when probing parsed `MacroRef` nodes and when
/// scanning verbatim macro text inside legacy `CondExpr::Raw` bodies.
const WITH_NAMES: &[&str] = &["with", "without"];

// =====================================================================
// RPM401 bcond-defined-but-unused
// =====================================================================

pub static DEFINED_BUT_UNUSED_METADATA: LintMetadata = LintMetadata {
    id: "RPM401",
    name: "bcond-defined-but-unused",
    description: "A `%bcond` / `%bcond_with` / `%bcond_without` declaration has no matching \
                  `%{with name}` / `%{without name}` reference anywhere in the spec. The toggle \
                  has no effect; remove it or wire it up.",
    default_severity: Severity::Warn,
    category: LintCategory::Style,
};

/// Lint state for RPM401. Collects every bcond declaration with its
/// span; checked against the referenced set at end of visit.
#[derive(Debug, Default)]
pub struct BcondDefinedButUnused {
    diagnostics: Vec<Diagnostic>,
}

impl BcondDefinedButUnused {
    /// Construct an empty lint instance with no diagnostics buffered.
    pub fn new() -> Self {
        Self::default()
    }
}

impl<'ast> Visit<'ast> for BcondDefinedButUnused {
    fn visit_spec(&mut self, spec: &'ast SpecFile<Span>) {
        let usage = BcondUsage::collect(spec);
        for (name, decl) in &usage.declared {
            if usage.referenced.contains_key(name.as_str()) {
                continue;
            }
            self.diagnostics.push(Diagnostic::new(
                &DEFINED_BUT_UNUSED_METADATA,
                Severity::Warn,
                format!(
                    "bcond `{name}` is declared but never referenced via `%{{with {name}}}` or \
                     `%{{without {name}}}`; the toggle has no effect"
                ),
                decl.span,
            ));
        }
    }
}

impl Lint for BcondDefinedButUnused {
    fn metadata(&self) -> &'static LintMetadata {
        &DEFINED_BUT_UNUSED_METADATA
    }
    fn take_diagnostics(&mut self) -> Vec<Diagnostic> {
        std::mem::take(&mut self.diagnostics)
    }
}

// =====================================================================
// RPM402 with-condition-without-bcond
// =====================================================================

pub static REF_WITHOUT_BCOND_METADATA: LintMetadata = LintMetadata {
    id: "RPM402",
    name: "with-condition-without-bcond",
    description: "A `%{with name}` or `%{without name}` reference has no matching `%bcond` \
                  declaration. RPM expands the reference to nothing, so the conditional silently \
                  never fires — declare the bcond or fix the typo.",
    default_severity: Severity::Warn,
    category: LintCategory::Correctness,
};

/// Lint state for RPM402.
#[derive(Debug, Default)]
pub struct WithConditionWithoutBcond {
    diagnostics: Vec<Diagnostic>,
}

impl WithConditionWithoutBcond {
    /// Construct an empty lint instance with no diagnostics buffered.
    pub fn new() -> Self {
        Self::default()
    }
}

impl<'ast> Visit<'ast> for WithConditionWithoutBcond {
    fn visit_spec(&mut self, spec: &'ast SpecFile<Span>) {
        let usage = BcondUsage::collect(spec);
        // Dedup by name — multiple references to the same undeclared
        // bcond yield one diagnostic at the first reference span.
        for (name, reference) in &usage.referenced {
            if usage.declared.contains_key(name) {
                continue;
            }
            self.diagnostics.push(Diagnostic::new(
                &REF_WITHOUT_BCOND_METADATA,
                Severity::Warn,
                format!(
                    "reference to `%{{with {name}}}` / `%{{without {name}}}` without a matching \
                     `%bcond_with {name}` / `%bcond_without {name}` declaration; RPM expands \
                     it to nothing — the branch silently never fires"
                ),
                reference.span,
            ));
        }
    }
}

impl Lint for WithConditionWithoutBcond {
    fn metadata(&self) -> &'static LintMetadata {
        &REF_WITHOUT_BCOND_METADATA
    }
    fn take_diagnostics(&mut self) -> Vec<Diagnostic> {
        std::mem::take(&mut self.diagnostics)
    }
}

// =====================================================================
// Shared collector
// =====================================================================

/// Polarity of a `%bcond*` declaration. Preserved so cross-rule
/// consumers (e.g. a future "redundant `%{with}` in default-on bcond"
/// lint) can reason about default state without re-parsing the AST.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BcondPolarity {
    /// `%bcond_with NAME` — default off.
    With,
    /// `%bcond_without NAME` — default on.
    Without,
    /// `%bcond NAME DEFAULT` (rpm ≥ 4.17.1) — default expression
    /// determines polarity at runtime; we only record the syntactic form.
    Modern,
}

impl BcondPolarity {
    fn from_style(style: BuildCondStyle) -> Self {
        match style {
            BuildCondStyle::BcondWith => Self::With,
            BuildCondStyle::BcondWithout => Self::Without,
            BuildCondStyle::Bcond => Self::Modern,
            // `BuildCondStyle` is `#[non_exhaustive]` in the rpm-spec
            // crate. Future variants default to Modern until we have
            // a reason to distinguish them — the polarity field is
            // future-proofing for cross-rule queries today.
            _ => Self::Modern,
        }
    }
}

/// Polarity of a `%{with NAME}` / `%{without NAME}` reference.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum WithKind {
    With,
    Without,
}

impl WithKind {
    fn from_head(head: &str) -> Option<Self> {
        match head {
            "with" => Some(Self::With),
            "without" => Some(Self::Without),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, Copy)]
struct BcondDecl {
    #[allow(dead_code)] // future-proof for cross-rule queries.
    polarity: BcondPolarity,
    span: Span,
}

#[derive(Debug, Clone, Copy)]
struct BcondRef {
    #[allow(dead_code)] // future-proof for cross-rule queries.
    kind: WithKind,
    span: Span,
}

/// Pair of "what was declared" + "what was referenced" for a spec.
///
/// Names are stored as `String` because the AST owns them (`%bcond`
/// declarations store the name as `String`; macro references as
/// `String`). Spans on declarations are the `BuildCondition.data`
/// (`Span` from the parser).
#[derive(Debug, Default)]
struct BcondUsage {
    /// `name → declaration`. Last write wins on duplicate names; a
    /// separate rule could flag redefinition.
    declared: BTreeMap<String, BcondDecl>,
    /// `name → first reference`. Used as the diagnostic anchor for
    /// RPM402.
    referenced: BTreeMap<String, BcondRef>,
}

impl BcondUsage {
    fn collect(spec: &SpecFile<Span>) -> Self {
        let mut out = BcondUsage::default();
        walk_items(&spec.items, &mut out);
        out
    }

    fn record_ref(&mut self, name: String, kind: WithKind, span: Span) {
        self.referenced
            .entry(name)
            .or_insert(BcondRef { kind, span });
    }
}

/// Walk `&[SpecItem<Span>]` recursively, descending into conditionals
/// and subpackage sections, recording bcond declarations and `%{with
/// NAME}` / `%{without NAME}` references.
fn walk_items(items: &[SpecItem<Span>], out: &mut BcondUsage) {
    for item in items {
        match item {
            SpecItem::BuildCondition(b) => record_declaration(out, b),
            SpecItem::Preamble(p) => walk_preamble_item(p, out),
            SpecItem::Conditional(c) => walk_top_cond(c, out),
            SpecItem::Section(boxed) => walk_section(boxed.as_ref(), out),
            SpecItem::MacroDef(m) => walk_text_for_refs(&m.body, m.data, out),
            SpecItem::Statement(m) => {
                walk_macro_ref(m, out, items_anchor(items).unwrap_or_default())
            }
            _ => {}
        }
    }
}

fn walk_top_cond(cond: &Conditional<Span, SpecItem<Span>>, out: &mut BcondUsage) {
    for branch in &cond.branches {
        walk_cond_expr(&branch.expr, cond.data, out);
        walk_items(&branch.body, out);
    }
    if let Some(els) = &cond.otherwise {
        walk_items(els, out);
    }
}

/// Walk a `CondExpr` for `%{with foo}` / `%{without foo}` references.
/// The parser uses two shapes — structured `Parsed(ExprAst)` and
/// legacy `Raw(Text)` — both can carry macro references and both are
/// covered here.
fn walk_cond_expr(expr: &CondExpr<Span>, anchor: Span, out: &mut BcondUsage) {
    match expr {
        CondExpr::Raw(t) => walk_text_for_refs(t, anchor, out),
        CondExpr::Parsed(ast) => walk_expr_ast(ast, out),
        CondExpr::ArchList(items) => {
            for t in items {
                walk_text_for_refs(t, anchor, out);
            }
        }
        _ => {}
    }
}

/// Walk an `ExprAst` for `%{with foo}` / `%{without foo}` macros.
/// `ExprAst::Macro` stores the macro verbatim as a `String`; parse the
/// `with`/`without` form out of that text. Each node carries its own
/// span via the `data` field, so no anchor parameter is needed.
fn walk_expr_ast(ast: &ExprAst<Span>, out: &mut BcondUsage) {
    match ast {
        ExprAst::Macro { text, data } => {
            if let Some((kind, name)) = parse_with_without_from_text(text) {
                out.record_ref(name.to_owned(), kind, *data);
            }
        }
        ExprAst::Paren { inner, .. } | ExprAst::Not { inner, .. } => {
            walk_expr_ast(inner, out);
        }
        ExprAst::Binary { lhs, rhs, .. } => {
            walk_expr_ast(lhs, out);
            walk_expr_ast(rhs, out);
        }
        _ => {}
    }
}

/// Extract `(kind, NAME)` from the verbatim text of a `%{with NAME}`
/// or `%{without NAME}` macro reference. Tolerant of optional `?` /
/// `!?` prefixes and whitespace. Returns `None` for any other shape.
fn parse_with_without_from_text(s: &str) -> Option<(WithKind, &str)> {
    let body = s.strip_prefix("%{")?.strip_suffix('}')?;
    let body = body.trim_start_matches(['?', '!']);
    let mut parts = body.split_whitespace();
    let head = parts.next()?;
    if !WITH_NAMES.contains(&head) {
        return None;
    }
    let kind = WithKind::from_head(head)?;
    let name = parts.next()?;
    if name.is_empty() {
        return None;
    }
    Some((kind, name))
}

fn walk_section(section: &Section<Span>, out: &mut BcondUsage) {
    if let Section::Package { content, .. } = section {
        walk_preamble_content(content, out);
    }
    // Other section bodies (shell, files, changelog) rarely contain
    // bcond declarations or with-references. The parser stores them
    // as Text; bcond usage there is exotic enough to skip until a
    // real-world hit appears.
}

fn walk_preamble_content(items: &[PreambleContent<Span>], out: &mut BcondUsage) {
    for item in items {
        match item {
            PreambleContent::Item(p) => walk_preamble_item(p, out),
            PreambleContent::Conditional(c) => {
                for branch in &c.branches {
                    walk_cond_expr(&branch.expr, c.data, out);
                    walk_preamble_content(&branch.body, out);
                }
                if let Some(els) = &c.otherwise {
                    walk_preamble_content(els, out);
                }
            }
            _ => {}
        }
    }
}

fn walk_preamble_item(p: &PreambleItem<Span>, out: &mut BcondUsage) {
    let anchor = p.data;
    match &p.value {
        TagValue::Text(t) => walk_text_for_refs(t, anchor, out),
        TagValue::ArchList(items) => {
            for t in items {
                walk_text_for_refs(t, anchor, out);
            }
        }
        // `TagValue::Dep` — `Requires`, `BuildRequires`, etc. The
        // atom name is a `Text`, so a `%{with foo}` macro reference
        // shows up as a `TextSegment::Macro` we can walk. Rich
        // boolean deps are recursed into.
        TagValue::Dep(dep) => walk_dep_expr(dep, anchor, out),
        _ => {}
    }
}

fn walk_dep_expr(dep: &DepExpr, anchor: Span, out: &mut BcondUsage) {
    match dep {
        DepExpr::Atom(atom) => {
            walk_text_for_refs(&atom.name, anchor, out);
            if let Some(arch) = &atom.arch {
                walk_text_for_refs(arch, anchor, out);
            }
            if let Some(c) = &atom.constraint {
                walk_text_for_refs(&c.evr.version, anchor, out);
                if let Some(rel) = &c.evr.release {
                    walk_text_for_refs(rel, anchor, out);
                }
            }
        }
        DepExpr::Rich(bool_dep) => walk_bool_dep(bool_dep, anchor, out),
        // `DepExpr` is `#[non_exhaustive]`; future variants are
        // ignored until they exist.
        _ => {}
    }
}

fn walk_bool_dep(node: &BoolDep, anchor: Span, out: &mut BcondUsage) {
    match node {
        BoolDep::And(children) | BoolDep::Or(children) | BoolDep::With(children) => {
            for d in children {
                walk_dep_expr(d, anchor, out);
            }
        }
        BoolDep::If {
            cond,
            then,
            otherwise,
        }
        | BoolDep::Unless {
            cond,
            then,
            otherwise,
        } => {
            walk_dep_expr(cond, anchor, out);
            walk_dep_expr(then, anchor, out);
            if let Some(o) = otherwise {
                walk_dep_expr(o, anchor, out);
            }
        }
        BoolDep::Without { left, right } => {
            walk_dep_expr(left, anchor, out);
            walk_dep_expr(right, anchor, out);
        }
        // `BoolDep` is `#[non_exhaustive]`; future variants are
        // ignored until they exist.
        _ => {}
    }
}

fn walk_text_for_refs(text: &Text, anchor: Span, out: &mut BcondUsage) {
    for seg in &text.segments {
        if let TextSegment::Macro(m) = seg {
            walk_macro_ref(m, out, anchor);
        }
    }
}

fn walk_macro_ref(m: &MacroRef, out: &mut BcondUsage, anchor: Span) {
    if let Some((kind, name)) = with_or_without_target(m) {
        out.record_ref(name, kind, anchor);
    }
    // Macro args may themselves contain nested macro refs (e.g.
    // `%{with %{computed_name}}` — rare). Walk them too.
    for arg in &m.args {
        walk_text_for_refs(arg, anchor, out);
    }
    if let Some(t) = &m.with_value {
        walk_text_for_refs(t, anchor, out);
    }
}

/// Pick an anchor span for `SpecItem::Statement` macros — they don't
/// carry their own per-statement span in this AST shape. Returns the
/// first preamble item's span as a best-effort fallback; caller falls
/// back to `Span::default()` when nothing is anchorable.
fn items_anchor(items: &[SpecItem<Span>]) -> Option<Span> {
    for item in items {
        if let SpecItem::Preamble(p) = item {
            return Some(p.data);
        }
    }
    None
}

fn record_declaration(out: &mut BcondUsage, node: &BuildCondition<Span>) {
    // Last declaration wins on duplicate names — a separate rule could
    // flag redefinition (RPM003-style); not our concern here.
    let polarity = BcondPolarity::from_style(node.style);
    out.declared.insert(
        node.name.clone(),
        BcondDecl {
            polarity,
            span: node.data,
        },
    );
    // The default expression on `%bcond name DEFAULT` is plain Text;
    // walk it too in case it contains `%{with other}` (unlikely but
    // possible).
    if let Some(default) = &node.default {
        walk_text_for_refs(default, node.data, out);
    }
}

/// If `m` is `%{with NAME}` or `%{without NAME}`, return
/// `(WithKind, NAME)`. `%bcond_value(name)` is *not* a usage reference
/// — it's another declaration form (rpm ≥ 4.17.1), counted separately.
fn with_or_without_target(m: &MacroRef) -> Option<(WithKind, String)> {
    if !WITH_NAMES.contains(&m.name.as_str()) {
        return None;
    }
    let kind = WithKind::from_head(m.name.as_str())?;
    let arg = m.args.first()?;
    let lit = arg.literal_str()?;
    let trimmed = lit.trim();
    if trimmed.is_empty() {
        return None;
    }
    Some((kind, trimmed.to_owned()))
}

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

    fn run_401(src: &str) -> Vec<Diagnostic> {
        let outcome = parse(src);
        let mut lint = BcondDefinedButUnused::new();
        lint.visit_spec(&outcome.spec);
        lint.take_diagnostics()
    }

    fn run_402(src: &str) -> Vec<Diagnostic> {
        let outcome = parse(src);
        let mut lint = WithConditionWithoutBcond::new();
        lint.visit_spec(&outcome.spec);
        lint.take_diagnostics()
    }

    // ----- RPM401 -----

    #[test]
    fn rpm401_flags_unused_bcond_with() {
        let src = "%bcond_with foo\nName: x\n";
        let diags = run_401(src);
        assert_eq!(diags.len(), 1, "{diags:?}");
        assert_eq!(diags[0].lint_id, "RPM401");
        assert!(diags[0].message.contains("foo"));
    }

    #[test]
    fn rpm401_flags_unused_bcond_without() {
        let src = "%bcond_without bar\nName: x\n";
        assert_eq!(run_401(src).len(), 1);
    }

    #[test]
    fn rpm401_silent_when_bcond_used_with_macro() {
        let src = "%bcond_with foo\nName: x\n\
%if %{with foo}\nBuildRequires: extra\n%endif\n";
        assert!(run_401(src).is_empty());
    }

    #[test]
    fn rpm401_silent_when_bcond_used_without_macro() {
        let src = "%bcond_without bar\nName: x\n\
%if %{without bar}\nBuildRequires: alt\n%endif\n";
        assert!(run_401(src).is_empty());
    }

    #[test]
    fn rpm401_flags_one_of_two_when_only_one_referenced() {
        let src = "%bcond_with used\n%bcond_with unused\nName: x\n\
%if %{with used}\nBuildRequires: a\n%endif\n";
        let diags = run_401(src);
        assert_eq!(diags.len(), 1);
        assert!(diags[0].message.contains("unused"));
    }

    #[test]
    fn rpm401_silent_for_modern_bcond_form() {
        // `%bcond name DEFAULT` (rpm ≥ 4.17.1) — same usage check.
        let src = "%bcond newcond 1\nName: x\n\
%if %{with newcond}\nRequires: a\n%endif\n";
        assert!(run_401(src).is_empty());
    }

    #[test]
    fn rpm401_silent_when_bcond_used_via_optional_with() {
        // `%{?with foo}` is the tolerant optional form; RPM still
        // treats it as a `with` reference at evaluation time, so a
        // declared bcond is "used".
        let src = "%bcond_with foo\nName: x\n\
%if %{?with foo}\nBuildRequires: extra\n%endif\n";
        assert!(run_401(src).is_empty(), "{:?}", run_401(src));
    }

    #[test]
    fn rpm401_silent_when_bcond_declared_in_subpackage_used_in_main() {
        // Declaration at top level, usage inside a subpackage's
        // preamble via `%if %{with foo}` so the walker exercises the
        // Section::Package preamble-content path through a nested
        // conditional. The bcond is referenced, so RPM401 must stay
        // silent.
        let src = "%bcond_with foo\n\
Name: x\nVersion: 1\nRelease: 1\nSummary: s\nLicense: MIT\n\
%description\nmain\n\
%package devel\nSummary: d\n\
%if %{with foo}\nRequires: foo-extras\n%endif\n\
%description devel\ndevel\n";
        assert!(run_401(src).is_empty(), "{:?}", run_401(src));
    }

    // ----- RPM402 -----

    #[test]
    fn rpm402_flags_with_without_bcond() {
        let src = "Name: x\n%if %{with foo}\nBuildRequires: a\n%endif\n";
        let diags = run_402(src);
        assert_eq!(diags.len(), 1, "{diags:?}");
        assert_eq!(diags[0].lint_id, "RPM402");
        assert!(diags[0].message.contains("foo"));
    }

    #[test]
    fn rpm402_flags_without_without_bcond() {
        let src = "Name: x\n%if %{without bar}\nBuildRequires: a\n%endif\n";
        assert_eq!(run_402(src).len(), 1);
    }

    #[test]
    fn rpm402_silent_when_bcond_declared() {
        let src = "%bcond_with foo\nName: x\n%if %{with foo}\nBuildRequires: a\n%endif\n";
        assert!(run_402(src).is_empty());
    }

    #[test]
    fn rpm402_deduplicates_repeated_references() {
        // Two references to the same undeclared bcond → one diagnostic.
        let src = "Name: x\n\
%if %{with foo}\nBuildRequires: a\n%endif\n\
%if %{with foo}\nBuildRequires: b\n%endif\n";
        assert_eq!(run_402(src).len(), 1);
    }

    #[test]
    fn rpm402_silent_when_bcond_modern_form_declared() {
        let src = "%bcond foo 1\nName: x\n%if %{with foo}\nBuildRequires: a\n%endif\n";
        assert!(run_402(src).is_empty());
    }

    #[test]
    fn rpm402_flags_each_distinct_missing_name() {
        let src = "Name: x\n%if %{with foo}\nBuildRequires: a\n%endif\n\
%if %{without bar}\nBuildRequires: b\n%endif\n";
        let diags = run_402(src);
        assert_eq!(diags.len(), 2, "{diags:?}");
    }

    #[test]
    fn rpm402_silent_on_clean_spec() {
        let src = "Name: x\nVersion: 1\n";
        assert!(run_402(src).is_empty());
    }

    #[test]
    fn rpm402_flags_optional_with_without_bcond() {
        // `%{?with foo}` with no declared bcond — the optional form
        // doesn't excuse the undeclared bcond; RPM still expands it
        // to nothing, the branch silently never fires.
        let src = "Name: x\n%if %{?with foo}\nBuildRequires: a\n%endif\n";
        let diags = run_402(src);
        assert_eq!(diags.len(), 1, "{diags:?}");
        assert!(diags[0].message.contains("foo"));
    }

    #[test]
    fn rpm402_handles_negated_optional() {
        // `%{!?with foo}` — negated optional, still a `with`
        // reference for the purpose of bcond hygiene.
        let src = "Name: x\n%if %{!?with foo}\nBuildRequires: a\n%endif\n";
        let diags = run_402(src);
        assert_eq!(diags.len(), 1, "{diags:?}");
        assert!(diags[0].message.contains("foo"));
    }
}