diff --git a/xtask/src/test_lint.rs b/xtask/src/test_lint.rs index 57b9cd41d0..55b90d8b4a 100644 --- a/xtask/src/test_lint.rs +++ b/xtask/src/test_lint.rs @@ -2966,6 +2966,16 @@ fn last_literal(s: &str) -> Option<&str> { Some(&s[open + 1..close]) } +/// The literal that ends the path built by `arg`: the format string of a `format!(…)` call, since +/// its arguments fill placeholders inside it, otherwise the last string literal. +fn path_literal(arg: &str) -> Option<&str> { + let Some(at) = arg.find("format!(") else { return last_literal(arg) }; + let rest = &arg[at + "format!(".len()..]; + let open = rest.find('"')?; + let close = rest[open + 1..].find('"')? + open + 1; + Some(&rest[open + 1..close]) +} + fn reads_source_hits(file: &File) -> Vec { if SOURCE_AUDITS.contains(&file.path) { return vec![]; @@ -2989,8 +2999,8 @@ fn reads_source_hits(file: &File) -> Vec { let rs = match word { b"ends_with" => literal == "\".rs\"", // The last path literal in the argument: `root.join("src/lib.rs")`, `concat!(…, "/x.rs")`, but not - // `root.join("fixtures.rs").join("input.json")`. - b"read_to_string" => last_literal(&arg).is_some_and(|l| l.ends_with(".rs")), + // `root.join("fixtures.rs").join("input.json")` or `format!("{}/input.json", "fixtures.rs")`. + b"read_to_string" => path_literal(&arg).is_some_and(|l| l.ends_with(".rs")), _ => arg.trim_end_matches('#').ends_with(".rs\""), }; if rs { @@ -3404,9 +3414,29 @@ fn declared_cfg_spans(src: &str, name: &str, within: Option<&str>) -> Option> = within.map(|w| { words(&m, b"mod", 0, m.len()).into_iter().filter_map(|at| named(at, w, b'{').and_then(|o| matching(&m, o).map(|c| (o, c)))).collect() }); + // Every inline `mod { … }`: a declaration counts only when its innermost enclosing inline + // module is the one `within` names (or, without `within`, when it has none), since a declaration + // nested deeper loads a file under that other module's directory. + let inline: Vec<(usize, usize)> = words(&m, b"mod", 0, m.len()) + .into_iter() + .filter_map(|at| { + let start = skip_ws(&m, at + 3); + let end = start + m[start..].iter().take_while(|&&c| is_ident(c)).count(); + let o = skip_ws(&m, end); + (end > start && m.get(o) == Some(&b'{')).then(|| matching(&m, o).map(|c| (o, c))).flatten() + }) + .collect(); + let innermost = |at: usize| inline.iter().filter(|&&(o, c)| o < at && at < c).max_by_key(|&&(o, _)| o).copied(); let decls: Vec = words(&m, b"mod", 0, m.len()) .into_iter() - .filter(|&at| named(at, name, b';').is_some() && blocks.as_ref().is_none_or(|bs| bs.iter().any(|&(o, c)| o < at && at < c))) + .filter(|&at| { + named(at, name, b';').is_some() + && match (&blocks, innermost(at)) { + (None, inner) => inner.is_none(), + (Some(bs), Some(inner)) => bs.contains(&inner), + (Some(_), None) => false, + } + }) .collect(); let mut each = decls.iter().map(|&decl| decl_cfg_spans(&m, src, decl)); let first = each.next()?; @@ -3569,10 +3599,11 @@ fn names_rect(op: &str) -> bool { let rest = &op[after..]; // The access must end the operand or a tuple or array item: `rect.width() / panel.width()` // is a ratio, not a pixel size. - // A `)` must close a tuple group: in `column_for_x(rect.min.x)` it closes a call's arguments. + // A `)` or `,` must belong to a tuple group: in `column_for_x(rect.min.x)` and + // `f(rect.min.x, 2)` it is part of a call's arguments. let ends = |r: &str| { let r = r.strip_prefix(".x").or_else(|| r.strip_prefix(".y")).unwrap_or(r); - r.is_empty() || r.starts_with([',', ']']) || (r.starts_with(')') && !closes_call(b, op.len() - r.len())) + r.is_empty() || r.starts_with(']') || (r.starts_with([')', ',']) && !closes_call(b, op.len() - r.len())) }; if rest.is_empty() || RECT_PARTS.iter().any(|p| rest.strip_prefix(p).is_some_and(ends)) { return true; @@ -3583,8 +3614,8 @@ fn names_rect(op: &str) -> bool { false } -/// Whether the `)` at `close` ends the argument list of a call (`f(…)`, `f!(…)`, `x.f(…)`) rather -/// than a tuple or a parenthesized group. +/// Whether the `)` or `,` at `close` is in the argument list of a call (`f(…)`, `f!(…)`, `x.f(…)`) +/// rather than in a tuple or a parenthesized group. fn closes_call(b: &[u8], close: usize) -> bool { let mut depth = 0i32; for i in (0..close).rev() { @@ -5264,7 +5295,11 @@ mod tests { let inner = "#![cfg(target_os = \"macos\")]\nmod child;\n"; assert_eq!(declared_cfgs(inner, "child"), Some(vec!["target_os=\"macos\"".to_owned()])); let block = "#[cfg(windows)]\nmod platform {\n mod child;\n}\n"; - assert_eq!(declared_cfgs(block, "child"), Some(vec!["windows".to_owned()])); + assert_eq!(declared_cfg_spans(block, "child", Some("platform")).map(|(p, _)| p), Some(vec!["windows".to_owned()])); + assert_eq!(declared_cfgs(block, "child"), None, "the nested declaration loads platform/child.rs"); + let nested = "#[cfg(windows)]\nmod child;\nmod other {\n mod child;\n}\n"; + assert_eq!(declared_cfgs(nested, "child"), Some(vec!["windows".to_owned()]), "other/child.rs is another file"); + assert_eq!(declared_cfg_spans(nested, "child", Some("other")).map(|(p, _)| p), Some(vec![])); assert_eq!(declared_cfgs("mod child;\n", "child"), Some(vec![])); } @@ -5332,6 +5367,7 @@ mod tests { assert_eq!(px("assert_eq!((rect.x0, rect.y0), (200.0, 200.0));"), [], "document corners"); assert_eq!(px("assert_eq!(rgb8(&fill_color(&app, rect)), [255, 255, 255]);"), [], "a rect passed on"); assert_eq!(px("assert_eq!(column_for_x(button_rect.min.x), 2);"), [], "a coordinate passed on"); + assert_eq!(px("assert_eq!(cell_at(button_rect.min.x, 3), 2);"), [], "a coordinate passed on before another argument"); assert_eq!(px("assert_eq!((button_rect.min.x, button_rect.min.y), (1.0, 2.0));"), [(4, "exact-pixel")], "a tuple of coordinates"); let engine = "#[test]\nfn t() {\n assert_eq!(art_rect.width(), 10.0);\n}\n"; assert_eq!(only("exact-pixel", engine), [], "document geometry outside UI code"); @@ -5347,6 +5383,11 @@ mod tests { assert_eq!(only("reads-source", commented), [], "a path in a comment"); let dir = "#[test]\nfn t() {\n let s = fs::read_to_string(root.join(\"fixtures.rs\").join(\"input.json\")).unwrap();\n assert!(s.contains(\"x\"));\n}\n"; assert_eq!(only("reads-source", dir), [], "a directory whose name ends in .rs"); + let formatted = "#[test]\nfn t() {\n let s = fs::read_to_string(format!(\"{}/input.json\", \"fixtures.rs\")).unwrap();\n assert!(s.contains(\"x\"));\n}\n"; + assert_eq!(only("reads-source", formatted), [], "a format argument that ends in .rs"); + let formatted_rs = + "#[test]\nfn t() {\n let s = fs::read_to_string(format!(\"{}/lib.rs\", dir)).unwrap();\n assert!(s.contains(\"x\"));\n}\n"; + assert_eq!(only("reads-source", formatted_rs), [(3, "reads-source")]); } #[test]