From 1ef5e952f61d0a809755a8fb1ee8f849ddc9efd8 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Thu, 14 Mar 2024 13:12:46 +0530 Subject: [PATCH] Count expression length as well --- .../test/fixtures/flake8_pyi/PYI053.pyi | 2 +- .../rules/string_or_bytes_too_long.rs | 17 +++++------------ ...s__flake8_pyi__tests__PYI053_PYI053.pyi.snap | 6 +++--- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI053.pyi b/crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI053.pyi index 7bb29dc1c468e..a711b7e9156d9 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI053.pyi +++ b/crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI053.pyi @@ -65,4 +65,4 @@ def not_warnings_dot_deprecated( ) def not_a_deprecated_function() -> None: ... -fbaz: str = f"51 character {foo} stringggggggggggggggggggggggggggggggg" # Error: PYI053 +fbaz: str = f"51 character {foo} stringgggggggggggggggggggggggggg" # Error: PYI053 diff --git a/crates/ruff_linter/src/rules/flake8_pyi/rules/string_or_bytes_too_long.rs b/crates/ruff_linter/src/rules/flake8_pyi/rules/string_or_bytes_too_long.rs index a51886046dbc3..145d38f6da6b4 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/rules/string_or_bytes_too_long.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/rules/string_or_bytes_too_long.rs @@ -73,14 +73,8 @@ pub(crate) fn string_or_bytes_too_long(checker: &mut Checker, string: StringLike checker.diagnostics.push(diagnostic); } -/// Count the number of characters in an f-string. This accounts for implicitly concatenated -/// f-strings as well. For example, the following f-string has 12 characters as highlighted -/// by the caret symbols: -/// -/// ```python -/// x = "one" f"one{expr}one" f"one" f"{expr}" -/// # ^^^ ^^^ ^^^ ^^^ -/// ```` +/// Count the number of visible characters in an f-string. This accounts for +/// implicitly concatenated f-strings as well. fn count_f_string_chars(f_string: &ast::ExprFString) -> usize { f_string .value @@ -90,10 +84,9 @@ fn count_f_string_chars(f_string: &ast::ExprFString) -> usize { ast::FStringPart::FString(f_string) => f_string .elements .iter() - .map(|element| { - element - .as_literal() - .map_or(0, |literal| literal.chars().count()) + .map(|element| match element { + ast::FStringElement::Literal(string) => string.chars().count(), + ast::FStringElement::Expression(expr) => expr.range().len().to_usize(), }) .sum(), }) diff --git a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI053_PYI053.pyi.snap b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI053_PYI053.pyi.snap index a6829a787fe0b..19ca04f611b51 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI053_PYI053.pyi.snap +++ b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI053_PYI053.pyi.snap @@ -150,8 +150,8 @@ PYI053.pyi:68:13: PYI053 [*] String and bytes literals longer than 50 characters | 66 | def not_a_deprecated_function() -> None: ... 67 | -68 | fbaz: str = f"51 character {foo} stringggggggggggggggggggggggggggggggg" # Error: PYI053 - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI053 +68 | fbaz: str = f"51 character {foo} stringgggggggggggggggggggggggggg" # Error: PYI053 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI053 | = help: Replace with `...` @@ -159,5 +159,5 @@ PYI053.pyi:68:13: PYI053 [*] String and bytes literals longer than 50 characters 65 65 | ) 66 66 | def not_a_deprecated_function() -> None: ... 67 67 | -68 |-fbaz: str = f"51 character {foo} stringggggggggggggggggggggggggggggggg" # Error: PYI053 +68 |-fbaz: str = f"51 character {foo} stringgggggggggggggggggggggggggg" # Error: PYI053 68 |+fbaz: str = ... # Error: PYI053