diff --git a/crates/design-lint/src/contract/test_rules.rs b/crates/design-lint/src/contract/test_rules.rs index 310a60053..107ea8265 100644 --- a/crates/design-lint/src/contract/test_rules.rs +++ b/crates/design-lint/src/contract/test_rules.rs @@ -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, }) } diff --git a/crates/design-lint/tests/egui_rules.rs b/crates/design-lint/tests/egui_rules.rs index 5b6bc5fe2..56b29f7e5 100644 --- a/crates/design-lint/tests/egui_rules.rs +++ b/crates/design-lint/tests/egui_rules.rs @@ -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")); +}