Merge pull request #2189 from aiongg/lint-tuple-index

design-lint: a tuple field in an assert is not a literal size
This commit is contained in:
aiongg
2026-11-18 08:52:52 -05:00
committed by GitHub
2 changed files with 21 additions and 8 deletions
+13 -8
View File
@@ -43,15 +43,20 @@ fn is_cfg_test(attrs: &[Attribute]) -> bool {
}
fn has_float(tokens: proc_macro2::TokenStream) -> bool {
tokens.into_iter().any(|t| match t {
TokenTree::Literal(l) => {
let text = l.to_string();
text.starts_with(|c: char| c.is_ascii_digit())
&& (text.contains('.') || text.ends_with("f32") || text.ends_with("f64"))
&& text.chars().take_while(|c| c.is_ascii_digit() || matches!(c, '.' | '_')).any(|c| c.is_ascii_digit() && c != '0')
let mut after_dot = false;
tokens.into_iter().any(|t| {
// A literal after `.` is a tuple field (`pair.1.0` lexes as `pair`, `.`, `1.0`), not a number.
let field = std::mem::replace(&mut after_dot, matches!(&t, TokenTree::Punct(p) if p.as_char() == '.'));
match t {
TokenTree::Literal(l) if !field => {
let text = l.to_string();
text.starts_with(|c: char| c.is_ascii_digit())
&& (text.contains('.') || text.ends_with("f32") || text.ends_with("f64"))
&& text.chars().take_while(|c| c.is_ascii_digit() || matches!(c, '.' | '_')).any(|c| c.is_ascii_digit() && c != '0')
}
TokenTree::Group(g) => has_float(g.stream()),
_ => false,
}
TokenTree::Group(g) => has_float(g.stream()),
_ => false,
})
}
+8
View File
@@ -111,3 +111,11 @@ fn a_salt_inside_a_scope_closure_is_not_the_scope_builders() {
let salted = "fn f(ui: &mut Ui) {\n ui.scope_builder(UiBuilder::new().id_salt(\"b\"), |ui| ());\n}\n";
assert!(rules_at(salted, "src/ui/a.rs", &scope()).contains(&"contract/memory"));
}
#[test]
fn a_tuple_field_in_an_assert_is_not_a_literal_size() {
let field = "#[test]\nfn t() {\n let fixture = Fixture::new(show);\n assert_eq!(run(&fixture).1.0, Some(1));\n}\n";
assert!(!rules_at(field, "crates/x/tests/it/main.rs", &scope()).contains(&"test/literal-size"));
let size = "#[test]\nfn t() {\n let fixture = Fixture::new(show);\n assert_eq!(fixture.size().x, 1.5);\n}\n";
assert!(rules_at(size, "crates/x/tests/it/main.rs", &scope()).contains(&"test/literal-size"));
}