syncable_cli/analyzer/hadolint/rules/
dl1001.rs

1//! DL1001: Please refrain from using inline ignore pragmas
2//!
3//! This is a meta-rule that warns when inline ignore pragmas are used.
4//! It's disabled by default but can be enabled for strict linting.
5
6use crate::analyzer::hadolint::parser::instruction::Instruction;
7use crate::analyzer::hadolint::rules::{SimpleRule, simple_rule};
8use crate::analyzer::hadolint::shell::ParsedShell;
9use crate::analyzer::hadolint::types::Severity;
10
11pub fn rule() -> SimpleRule<impl Fn(&Instruction, Option<&ParsedShell>) -> bool + Send + Sync> {
12    simple_rule(
13        "DL1001",
14        Severity::Info,
15        "Please refrain from using inline ignore pragmas `# hadolint ignore=...`.",
16        |instr, _shell| {
17            match instr {
18                Instruction::Comment(comment) => {
19                    // Check if it's a hadolint ignore pragma
20                    let lower = comment.to_lowercase();
21                    !lower.contains("hadolint") || !lower.contains("ignore")
22                }
23                _ => true,
24            }
25        },
26    )
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32    use crate::analyzer::hadolint::rules::{Rule, RuleState};
33
34    #[test]
35    fn test_ignore_pragma() {
36        let rule = rule();
37        let mut state = RuleState::new();
38        let instr = Instruction::Comment("hadolint ignore=DL3008".to_string());
39        rule.check(&mut state, 1, &instr, None);
40        assert_eq!(state.failures.len(), 1);
41    }
42
43    #[test]
44    fn test_regular_comment() {
45        let rule = rule();
46        let mut state = RuleState::new();
47        let instr = Instruction::Comment("This is a regular comment".to_string());
48        rule.check(&mut state, 1, &instr, None);
49        assert!(state.failures.is_empty());
50    }
51}