xtask: test-lint decodes string continuations in cfg_attr paths and keeps census hits for fixed ranges

A path string that continues on the next line with a backslash now
decodes to the joined file name, so the conditional route is kept.
A range argument with two fixed bounds (1..4, 1..=4) no longer counts
as an input, so the round-trip census diagnostic stays; a range with a
variable bound still suppresses it. Adds a test for a bracket in a
cfg_attr path filename keeping its sibling cfg gate.
This commit is contained in:
dotCooCoo committed 2026-12-02 05:55:33 -08:00
1 parent 1361bb3b5c
commit cf51c8894c
1 file changed
+44 -2
+44 -2
View File
@@ -1851,7 +1851,8 @@ fn feeds_entry(b: &[u8], pattern: &str, from: usize, to: usize) -> bool {
let mut operands = 0;
let mut numeric = true;
for (i, c) in s.char_indices().chain([(s.len(), '+')]) {
if !matches!(c, '+' | '-' | '*' | '/' | '%' | '<' | '>' | '&' | '|' | '^' | '(' | ')') {
let range = c == '.' && (s.as_bytes().get(i + 1) == Some(&b'.') || s[..i].ends_with('.')) || c == '=' && s[..i].ends_with("..");
if !range && !matches!(c, '+' | '-' | '*' | '/' | '%' | '<' | '>' | '&' | '|' | '^' | '(' | ')') {
continue;
}
let head = &s[start..i];
@@ -5067,6 +5068,9 @@ fn unescape(s: &str) -> String {
continue;
}
match chars.next() {
Some(c @ ('\n' | '\r')) if c == '\n' || chars.peek() == Some(&'\n') => {
while chars.next_if(|c| matches!(c, ' ' | '\t' | '\n' | '\r')).is_some() {}
}
Some('n') => out.push('\n'),
Some('t') => out.push('\t'),
Some('r') => out.push('\r'),
@@ -5579,7 +5583,7 @@ fn cfg_attr_paths(m: &[u8], src: &[u8], from: usize, to: usize) -> Vec<(String,
}
let open = skip_ws(m, at + "cfg_attr".len());
let Some(close) = (m.get(open) == Some(&b'(')).then(|| matching(m, open)).flatten() else { continue };
let text = pred_text(&src[open + 1..close]);
let text = norm_outside_literals(&mask_parts(&String::from_utf8_lossy(&src[open + 1..close]), true, false));
let cuts = attr_commas(&text);
let Some(&first) = cuts.first() else { continue };
let mut start = first + 1;
@@ -8618,6 +8622,30 @@ mod tests {
assert_eq!(cfg_attr_paths(&mask(raw_escape), raw_escape.as_bytes(), 0, raw_escape.len()), [("unix".to_owned(), "\\x61.rs".to_owned())]);
}
#[test]
fn conditional_paths_decode_string_continuations() {
for newline in ["\n", "\r\n"] {
let src = format!("#[cfg_attr(unix, path = \"chi\\{newline} \t\r\n ld.rs\")]\nmod c;\n");
assert_eq!(cfg_attr_paths(&mask(&src), src.as_bytes(), 0, src.len()), [("unix".to_owned(), "child.rs".to_owned())]);
}
let src = "#[cfg_attr(unix, path = \"child file.rs\")]\nmod c;\n";
assert_eq!(cfg_attr_paths(&mask(src), src.as_bytes(), 0, src.len()), [("unix".to_owned(), "child file.rs".to_owned())]);
let raw = "#[cfg_attr(unix, path = r\"chi\\\n ld.rs\")]\nmod c;\n";
assert_eq!(cfg_attr_paths(&mask(raw), raw.as_bytes(), 0, raw.len()), [("unix".to_owned(), "chi\\\n ld.rs".to_owned())]);
}
#[test]
fn conditional_paths_keep_sibling_cfg_gates_with_brackets_in_filenames() {
let files: HashMap<&str, &str> =
[("crates/x/src/lib.rs", "#[cfg_attr(unix, path = \"mac[.rs\", cfg(target_os = \"macos\"))]\nmod platform;\n")].into_iter().collect();
let read = |p: &str| files.get(p).map(|s| (*s).to_owned());
let overrides = path_overrides(files.iter().map(|(p, s)| (*p, *s)));
let child = "crates/x/src/mac[.rs".to_owned();
let ctx = Ctx { module_cfgs: module_cfgs(std::slice::from_ref(&child), &read, &overrides), ..ctx() };
let test = "#[test]\nfn t() {\n assert_eq!(f(), 1);\n}\n";
assert_eq!(hits_in(&child, test, &ctx), [(2, "never-run")]);
}
#[test]
fn path_attributes_accept_raw_strings() {
for src in ["#[path = r\"shared.rs\"]\nmod s;\n", "#[path = r#\"shared.rs\"#]\nmod s;\n", "#[path = \"shared.rs\"]\nmod s;\n"] {
@@ -8951,6 +8979,20 @@ mod tests {
assert_eq!(census(sibling), [], "an || in a sibling tuple element does not skip the call");
}
#[test]
fn census_keeps_diagnostics_for_fixed_range_arguments() {
for range in ["1..4", "1..=4", "-4..-1", "1..(2 + 2)"] {
let src =
format!("#[test]\nfn t() {{\n for k in Kind::ALL {{\n assert_eq!(Kind::from_id(k.id(), {range}), Some(k));\n }}\n}}\n");
assert_eq!(only("census", &src), [(2, "census")], "{range}");
}
for range in ["1..limit", "1..=limit", "start..4", "1..(limit + 2)"] {
let src =
format!("#[test]\nfn t() {{\n for k in Kind::ALL {{\n assert_eq!(Kind::from_id(k.id(), {range}), Some(k));\n }}\n}}\n");
assert_eq!(only("census", &src), [], "{range}");
}
}
#[test]
fn numeric_literals_are_one_token() {
for s in ["1", "0x1F", "2.5f32", "1_000", "1e-3", "7usize"] {