Skip to main content

alint_rules/
no_case_conflicts.rs

1//! `no_case_conflicts` — flag two paths that differ only by
2//! case (e.g. `README.md` + `readme.md`). Such pairs cannot
3//! coexist on case-insensitive filesystems (macOS HFS+/APFS
4//! default, Windows NTFS in its default mode), so committing
5//! them breaks checkouts for those developers.
6//!
7//! Check-only — renaming which one to keep is a human decision.
8
9use std::collections::BTreeMap;
10use std::path::PathBuf;
11
12use alint_core::{Context, Error, Level, Result, Rule, RuleSpec, Scope, Violation};
13
14#[derive(Debug)]
15pub struct NoCaseConflictsRule {
16    id: String,
17    level: Level,
18    policy_url: Option<String>,
19    message: Option<String>,
20    scope: Scope,
21}
22
23impl Rule for NoCaseConflictsRule {
24    fn id(&self) -> &str {
25        &self.id
26    }
27    fn level(&self) -> Level {
28        self.level
29    }
30    fn policy_url(&self) -> Option<&str> {
31        self.policy_url.as_deref()
32    }
33
34    fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
35        // Group paths by their lowercased form.
36        let mut groups: BTreeMap<String, Vec<PathBuf>> = BTreeMap::new();
37        for entry in ctx.index.files() {
38            if !self.scope.matches(&entry.path) {
39                continue;
40            }
41            let Some(as_str) = entry.path.to_str() else {
42                continue;
43            };
44            groups
45                .entry(as_str.to_ascii_lowercase())
46                .or_default()
47                .push(entry.path.clone());
48        }
49        let mut violations = Vec::new();
50        for (_lower, paths) in groups {
51            if paths.len() < 2 {
52                continue;
53            }
54            let names: Vec<String> = paths.iter().map(|p| p.display().to_string()).collect();
55            for p in &paths {
56                let msg = self.message.clone().unwrap_or_else(|| {
57                    format!(
58                        "case-insensitive collision: {} (collides with: {})",
59                        p.display(),
60                        names
61                            .iter()
62                            .filter(|n| *n != &p.display().to_string())
63                            .cloned()
64                            .collect::<Vec<_>>()
65                            .join(", ")
66                    )
67                });
68                violations.push(Violation::new(msg).with_path(p));
69            }
70        }
71        Ok(violations)
72    }
73}
74
75pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
76    let paths = spec.paths.as_ref().ok_or_else(|| {
77        Error::rule_config(
78            &spec.id,
79            "no_case_conflicts requires a `paths` field (often `\"**\"`)",
80        )
81    })?;
82    if spec.fix.is_some() {
83        return Err(Error::rule_config(
84            &spec.id,
85            "no_case_conflicts has no fix op — renaming which path to keep is a human decision",
86        ));
87    }
88    Ok(Box::new(NoCaseConflictsRule {
89        id: spec.id.clone(),
90        level: spec.level,
91        policy_url: spec.policy_url.clone(),
92        message: spec.message.clone(),
93        scope: Scope::from_paths_spec(paths)?,
94    }))
95}