From 539e33747cda9af00fd84837d42c3658e0463fb7 Mon Sep 17 00:00:00 2001 From: konstin Date: Sun, 1 Oct 2023 17:30:31 +0200 Subject: [PATCH] Fix PLE251 rules with f-string escaping **Summary** The `value` of the `FStringMiddle` for `f"""}}ab"""` is `}ab`, i.e. the curly brace escaping is decoded. If we iterate over string this gives us false indices causing exploding fixes for PLE251 rules (PLE2510, PLE2512, PLE2513, PLE2514, PLE2515). Instead, we now use the source range. Handles https://github.com/astral-sh/ruff/issues/7455#issuecomment-1741998106 Handles https://github.com/astral-sh/ruff/issues/7455#issuecomment-1741998256 **Test Plan** Minimized fuzzing cases as fixtures. --- .../fixtures/pylint/invalid_characters.py | Bin 1461 -> 1639 bytes .../pylint/rules/invalid_string_characters.rs | 4 +++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/invalid_characters.py b/crates/ruff_linter/resources/test/fixtures/pylint/invalid_characters.py index e1864fa38175502c3dcdc92b2b38f0d924a1cb34..79c9307695a9153b9945e2976c45335259630bfa 100644 GIT binary patch delta 187 zcmdnW{hVjRR@QnhWrd8Al7eC@ef{*zl8n+Mz2y8{{lwyuqQo5C;tc(w(zG=F%;MtG z)M9;e6H`-VFdL{QH#M(B*U;R=(9+Vv(7=qVLcvxcO-V_qwl+~J2}l!XurUFXjZ9HY ImIj#&00n(J$p8QV delta 7 OcmaFPvz2?pR#pHF)B_#> diff --git a/crates/ruff_linter/src/rules/pylint/rules/invalid_string_characters.rs b/crates/ruff_linter/src/rules/pylint/rules/invalid_string_characters.rs index 5c00ef89a1b6c4..ca4681d7867d3a 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/invalid_string_characters.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/invalid_string_characters.rs @@ -180,7 +180,9 @@ pub(crate) fn invalid_string_characters( ) { let text = match tok { Tok::String { .. } => locator.slice(range), - Tok::FStringMiddle { value, .. } => value.as_str(), + // The string value has already decoded escaped curly braces, which would gives us wrong + // column information + Tok::FStringMiddle { .. } => &locator.contents()[range], _ => return, };