alint_rules/
no_case_conflicts.rs1use std::collections::BTreeMap;
10
11use alint_core::{Context, Error, Level, Result, Rule, RuleSpec, Scope, Violation};
12
13#[derive(Debug)]
14pub struct NoCaseConflictsRule {
15 id: String,
16 level: Level,
17 policy_url: Option<String>,
18 message: Option<String>,
19 scope: Scope,
20}
21
22impl Rule for NoCaseConflictsRule {
23 alint_core::rule_common_impl!();
24
25 fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
26 let mut groups: BTreeMap<String, Vec<std::sync::Arc<std::path::Path>>> = BTreeMap::new();
30 for entry in ctx.index.files() {
31 if !self.scope.matches(&entry.path, ctx.index) {
32 continue;
33 }
34 let Some(as_str) = entry.path.to_str() else {
35 continue;
36 };
37 groups
38 .entry(as_str.to_ascii_lowercase())
39 .or_default()
40 .push(entry.path.clone());
41 }
42 let mut violations = Vec::new();
43 for (_lower, paths) in groups {
44 if paths.len() < 2 {
45 continue;
46 }
47 let names: Vec<String> = paths.iter().map(|p| p.display().to_string()).collect();
48 for p in &paths {
49 let msg = self.message.clone().unwrap_or_else(|| {
50 format!(
51 "case-insensitive collision: {} (collides with: {})",
52 p.display(),
53 names
54 .iter()
55 .filter(|n| *n != &p.display().to_string())
56 .cloned()
57 .collect::<Vec<_>>()
58 .join(", ")
59 )
60 });
61 violations.push(Violation::new(msg).with_path(p.clone()));
62 }
63 }
64 Ok(violations)
65 }
66}
67
68pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
69 let _paths = spec.paths.as_ref().ok_or_else(|| {
70 Error::rule_config(
71 &spec.id,
72 "no_case_conflicts requires a `paths` field (often `\"**\"`)",
73 )
74 })?;
75 if spec.fix.is_some() {
76 return Err(Error::rule_config(
77 &spec.id,
78 "no_case_conflicts has no fix op — renaming which path to keep is a human decision",
79 ));
80 }
81 Ok(Box::new(NoCaseConflictsRule {
82 id: spec.id.clone(),
83 level: spec.level,
84 policy_url: spec.policy_url.clone(),
85 message: spec.message.clone(),
86 scope: Scope::from_spec(spec)?,
87 }))
88}
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93 use crate::test_support::{ctx, index, spec_yaml};
94 use std::path::Path;
95
96 #[test]
97 fn build_rejects_missing_paths_field() {
98 let spec = spec_yaml(
99 "id: t\n\
100 kind: no_case_conflicts\n\
101 level: warning\n",
102 );
103 assert!(build(&spec).is_err());
104 }
105
106 #[test]
107 fn build_rejects_fix_block() {
108 let spec = spec_yaml(
109 "id: t\n\
110 kind: no_case_conflicts\n\
111 paths: \"**\"\n\
112 level: warning\n\
113 fix:\n \
114 file_remove: {}\n",
115 );
116 assert!(build(&spec).is_err());
117 }
118
119 #[test]
120 fn evaluate_passes_when_paths_unique_after_lowercase() {
121 let spec = spec_yaml(
122 "id: t\n\
123 kind: no_case_conflicts\n\
124 paths: \"**\"\n\
125 level: warning\n",
126 );
127 let rule = build(&spec).unwrap();
128 let i = index(&["README.md", "src/main.rs", "Cargo.toml"]);
129 let v = rule.evaluate(&ctx(Path::new("/fake"), &i)).unwrap();
130 assert!(v.is_empty());
131 }
132
133 #[test]
134 fn evaluate_fires_one_violation_per_collision_member() {
135 let spec = spec_yaml(
136 "id: t\n\
137 kind: no_case_conflicts\n\
138 paths: \"**\"\n\
139 level: warning\n",
140 );
141 let rule = build(&spec).unwrap();
142 let i = index(&["README.md", "readme.md", "Cargo.toml"]);
144 let v = rule.evaluate(&ctx(Path::new("/fake"), &i)).unwrap();
145 assert_eq!(v.len(), 2, "two collision members should fire");
146 }
147
148 #[test]
149 fn evaluate_fires_on_three_way_collision() {
150 let spec = spec_yaml(
151 "id: t\n\
152 kind: no_case_conflicts\n\
153 paths: \"**\"\n\
154 level: warning\n",
155 );
156 let rule = build(&spec).unwrap();
157 let i = index(&["README.md", "readme.md", "ReadMe.md"]);
158 let v = rule.evaluate(&ctx(Path::new("/fake"), &i)).unwrap();
159 assert_eq!(v.len(), 3, "three collision members should fire");
160 }
161
162 #[test]
163 fn scope_filter_narrows() {
164 let spec = spec_yaml(
168 "id: t\n\
169 kind: no_case_conflicts\n\
170 paths: \"**\"\n\
171 scope_filter:\n \
172 has_ancestor: marker.lock\n\
173 level: warning\n",
174 );
175 let rule = build(&spec).unwrap();
176 let i = index(&[
177 "pkg/marker.lock",
178 "pkg/README.md",
179 "pkg/readme.md",
180 "other/README.md",
181 "other/readme.md",
182 ]);
183 let v = rule.evaluate(&ctx(Path::new("/fake"), &i)).unwrap();
184 assert_eq!(v.len(), 2, "only the pkg/ pair should fire: {v:?}");
185 for vio in &v {
186 assert!(
187 vio.path.as_deref().is_some_and(|p| p.starts_with("pkg/")),
188 "unexpected path: {:?}",
189 vio.path
190 );
191 }
192 }
193}