From 4bb803c266a9eae34fe645bf4a3739c5ce946412 Mon Sep 17 00:00:00 2001 From: harupy Date: Fri, 4 Aug 2023 00:34:36 +0900 Subject: [PATCH 1/2] More precise invalid expression check for UP032 --- .../test/fixtures/pyupgrade/UP032_0.py | 4 +++ .../src/rules/pyupgrade/rules/f_strings.rs | 25 ++++++++++--------- ...__rules__pyupgrade__tests__UP032_0.py.snap | 21 +++++++++++++++- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/pyupgrade/UP032_0.py b/crates/ruff/resources/test/fixtures/pyupgrade/UP032_0.py index 04fb034bf3ccb..7511a147dd2f6 100644 --- a/crates/ruff/resources/test/fixtures/pyupgrade/UP032_0.py +++ b/crates/ruff/resources/test/fixtures/pyupgrade/UP032_0.py @@ -126,6 +126,8 @@ async def c(): return "{}".format(1 + await 3) +"{}".format(1 * 2) + ### # Non-errors ### @@ -194,3 +196,5 @@ async def c(): "{a}" "{1 + 2}" ).format(a=1) + +"{}".format(**c) diff --git a/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs b/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs index 62bff0dcb9a8d..4bd689805ac1f 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs @@ -74,7 +74,8 @@ impl<'a> FormatSummaryValues<'a> { }) = expr { for arg in args { - if contains_invalids(locator.slice(arg.range())) + if matches!(arg, Expr::Starred(..)) + || contains_quotes(locator.slice(arg.range())) || locator.contains_line_break(arg.range()) { return None; @@ -87,14 +88,15 @@ impl<'a> FormatSummaryValues<'a> { value, range: _, } = keyword; - if let Some(key) = arg { - if contains_invalids(locator.slice(value.range())) - || locator.contains_line_break(value.range()) - { - return None; - } - extracted_kwargs.insert(key, value); + let Some(key) = arg else { + return None; + }; + if contains_quotes(locator.slice(value.range())) + || locator.contains_line_break(value.range()) + { + return None; } + extracted_kwargs.insert(key, value); } } @@ -127,10 +129,9 @@ impl<'a> FormatSummaryValues<'a> { } } -/// Return `true` if the string contains characters that are forbidden by -/// argument identifiers. -fn contains_invalids(string: &str) -> bool { - string.contains('*') || string.contains('\'') || string.contains('"') +/// Return `true` if the string contains quotes. +fn contains_quotes(string: &str) -> bool { + string.contains('\'') || string.contains('"') } enum FormatContext { diff --git a/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP032_0.py.snap b/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP032_0.py.snap index 8b499c9081b5f..a9d2929ffe97f 100644 --- a/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP032_0.py.snap +++ b/crates/ruff/src/rules/pyupgrade/snapshots/ruff__rules__pyupgrade__tests__UP032_0.py.snap @@ -924,6 +924,25 @@ UP032_0.py:126:12: UP032 [*] Use f-string instead of `format` call 126 |+ return f"{1 + await 3}" 127 127 | 128 128 | -129 129 | ### +129 129 | "{}".format(1 * 2) + +UP032_0.py:129:1: UP032 [*] Use f-string instead of `format` call + | +129 | "{}".format(1 * 2) + | ^^^^^^^^^^^^^^^^^^ UP032 +130 | +131 | ### + | + = help: Convert to f-string + +ℹ Suggested fix +126 126 | return "{}".format(1 + await 3) +127 127 | +128 128 | +129 |-"{}".format(1 * 2) + 129 |+f"{1 * 2}" +130 130 | +131 131 | ### +132 132 | # Non-errors From e9ebcf293ea393a56637ab212611c968de565c9f Mon Sep 17 00:00:00 2001 From: harupy Date: Fri, 4 Aug 2023 00:38:16 +0900 Subject: [PATCH 2/2] Use array --- crates/ruff/src/rules/pyupgrade/rules/f_strings.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs b/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs index 4bd689805ac1f..1739207cba95b 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs @@ -131,7 +131,7 @@ impl<'a> FormatSummaryValues<'a> { /// Return `true` if the string contains quotes. fn contains_quotes(string: &str) -> bool { - string.contains('\'') || string.contains('"') + string.contains(['\'', '"']) } enum FormatContext {