xbp-analysis 10.57.0

Language-agnostic static analysis (error-handling / Aspirator-style) for XBP
Documentation
use super::{base_caps, concepts_of, finding_from_concept};
use crate::domain::concepts::ConceptKind;
use crate::domain::model::LanguageModel;
use crate::domain::rule::{Rule, RuleMeta};
use crate::domain::types::{Confidence, Finding, Severity};
use std::sync::OnceLock;

#[derive(Default)]
pub struct NonIdempotentRetryRule;

impl Rule for NonIdempotentRetryRule {
    fn meta(&self) -> &RuleMeta {
        static META: OnceLock<RuleMeta> = OnceLock::new();
        META.get_or_init(|| RuleMeta {
            id: "non-idempotent-retry".into(),
            name: "Non-idempotent retry".into(),
            description: "Retry of an operation that may not be safe to repeat.".into(),
            default_severity: Severity::High,
            required_capabilities: base_caps(),
            autofix_safe: false,
        })
    }

    fn evaluate(&self, model: &LanguageModel) -> Vec<Finding> {
        concepts_of(model, ConceptKind::Retry)
            .filter(|(_, c)| c.idempotent == Some(false))
            .map(|(lang, c)| {
                finding_from_concept(
                    &self.meta().id,
                    lang,
                    c,
                    Severity::High,
                    Confidence::Low,
                    "Retry may re-execute a non-idempotent side effect.",
                    "Duplicate charges, double writes, or split-brain updates.",
                    "Make the operation idempotent or use a single-flight / ledger guard.",
                )
            })
            .collect()
    }
}