use std::path::{Path, PathBuf};
const NO_TYPED_NODE: &[&str] = &[
"ModuleName",
"MacroCall",
"TranslationUnit",
"BuiltinType",
"TemplateArgumentList",
"TemplateDecl",
];
const SPELLING_FILES: &[&str] = &[
"deduction.rs",
"resolve.rs",
"types.rs",
"param_type_check.rs",
];
fn sema_files() -> Vec<(PathBuf, String)> {
fn walk(directory: &Path, found: &mut Vec<(PathBuf, String)>) {
let Ok(entries) = std::fs::read_dir(directory) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
walk(&path, found);
} else if path.extension().is_some_and(|extension| extension == "rs")
&& let Ok(text) = std::fs::read_to_string(&path)
{
found.push((path, text));
}
}
}
let root = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("src")
.join("sema");
let mut found = Vec::new();
walk(&root, &mut found);
found
}
fn lines_with(text: &str, needle: &str) -> Vec<(usize, String)> {
text.lines()
.enumerate()
.filter(|(_, line)| line.contains(needle))
.map(|(index, line)| (index + 1, line.trim().to_string()))
.collect()
}
fn offenders(needle: &str) -> Vec<String> {
let mut offenders = Vec::new();
for (path, text) in sema_files() {
for (line, code) in lines_with(&text, needle) {
offenders.push(format!("{}:{line}: {code}", path.display()));
}
}
offenders
}
#[test]
fn no_kind_equality_replaces_a_typed_question() {
let mut offenders = offenders("CppSyntaxKind::from(");
offenders.retain(|offender| {
if !(offender.contains("== CppSyntaxKind::") || offender.contains("!= CppSyntaxKind::")) {
return false;
}
!NO_TYPED_NODE
.iter()
.any(|kind| offender.contains(&format!("CppSyntaxKind::{kind}")))
});
assert!(
offenders.is_empty(),
"a kind equality is a typed question's stand-in — ask `CppX::can_cast(kind.into())`:\n{}",
offenders.join("\n")
);
}
#[test]
fn text_is_not_searched_for_structure() {
let mut offenders = Vec::new();
for (path, text) in sema_files() {
let name = path
.file_name()
.map(|name| name.to_string_lossy().to_string())
.unwrap_or_default();
if SPELLING_FILES.contains(&name.as_str()) {
continue;
}
for needle in [".rfind(\"", ".rfind(&"] {
for (line, code) in lines_with(&text, needle) {
offenders.push(format!("{}:{line}: {code}", path.display()));
}
}
}
assert!(
offenders.is_empty(),
"a node's own text is being located by searching a string — use its range or its typed accessor:\n{}",
offenders.join("\n")
);
}
#[test]
fn whitespace_splitting_is_a_spelling_rule() {
let allowed = [
"constraint.rs",
"declarations.rs",
"deduction.rs",
"model.rs",
"overload.rs",
"types.rs",
"concept_not_satisfied.rs",
"macro_parentheses.rs",
"mod.rs",
];
let mut offenders = Vec::new();
for (path, text) in sema_files() {
let name = path
.file_name()
.map(|name| name.to_string_lossy().to_string())
.unwrap_or_default();
if allowed.contains(&name.as_str()) {
continue;
}
for (line, code) in lines_with(&text, "split_whitespace()") {
offenders.push(format!("{}:{line}: {code}", path.display()));
}
}
assert!(
offenders.is_empty(),
"`split_whitespace` over a node's text is a word search, not a reading — use the typed accessor:\n{}",
offenders.join("\n")
);
}