diff --git a/crates/ruff/resources/test/fixtures/flynt/FLY002.py b/crates/ruff/resources/test/fixtures/flynt/FLY002.py index 2067b12d5d..a73dfcc28a 100644 --- a/crates/ruff/resources/test/fixtures/flynt/FLY002.py +++ b/crates/ruff/resources/test/fixtures/flynt/FLY002.py @@ -16,3 +16,8 @@ nok4 = "a".join([a, a, *a]) # Not OK (not a static length) nok5 = "a".join([choice("flarp")]) # Not OK (not a simple call) nok6 = "a".join(x for x in "feefoofum") # Not OK (generator) nok7 = "a".join([f"foo{8}", "bar"]) # Not OK (contains an f-string) + + +# Regression test for: https://github.com/astral-sh/ruff/issues/7197 +def create_file_public_url(url, filename): + return''.join([url, filename]) diff --git a/crates/ruff/src/rules/flynt/rules/static_join_to_fstring.rs b/crates/ruff/src/rules/flynt/rules/static_join_to_fstring.rs index 6e99df0459..8cb4268c1a 100644 --- a/crates/ruff/src/rules/flynt/rules/static_join_to_fstring.rs +++ b/crates/ruff/src/rules/flynt/rules/static_join_to_fstring.rs @@ -1,5 +1,6 @@ use itertools::Itertools; +use crate::autofix::edits::pad; use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::{self as ast, Arguments, Constant, Expr}; @@ -113,6 +114,7 @@ fn build_fstring(joiner: &str, joinees: &[Expr]) -> Option { Some(node.into()) } +/// FLY002 pub(crate) fn static_join_to_fstring(checker: &mut Checker, expr: &Expr, joiner: &str) { let Expr::Call(ast::ExprCall { arguments: Arguments { args, keywords, .. }, @@ -154,7 +156,7 @@ pub(crate) fn static_join_to_fstring(checker: &mut Checker, expr: &Expr, joiner: ); if checker.patch(diagnostic.kind.rule()) { diagnostic.set_fix(Fix::suggested(Edit::range_replacement( - contents, + pad(contents, expr.range(), checker.locator()), expr.range(), ))); } diff --git a/crates/ruff/src/rules/flynt/snapshots/ruff__rules__flynt__tests__FLY002_FLY002.py.snap b/crates/ruff/src/rules/flynt/snapshots/ruff__rules__flynt__tests__FLY002_FLY002.py.snap index def47016d3..aff724b257 100644 --- a/crates/ruff/src/rules/flynt/snapshots/ruff__rules__flynt__tests__FLY002_FLY002.py.snap +++ b/crates/ruff/src/rules/flynt/snapshots/ruff__rules__flynt__tests__FLY002_FLY002.py.snap @@ -125,4 +125,20 @@ FLY002.py:10:7: FLY002 [*] Consider `f"{secrets.token_urlsafe()}a{secrets.token_ 12 12 | nok1 = "x".join({"4", "5", "yee"}) # Not OK (set) 13 13 | nok2 = a.join(["1", "2", "3"]) # Not OK (not a static joiner) +FLY002.py:23:11: FLY002 [*] Consider `f"{url}{filename}"` instead of string join + | +21 | # Regression test for: https://github.com/astral-sh/ruff/issues/7197 +22 | def create_file_public_url(url, filename): +23 | return''.join([url, filename]) + | ^^^^^^^^^^^^^^^^^^^^^^^^ FLY002 + | + = help: Replace with `f"{url}{filename}"` + +ℹ Suggested fix +20 20 | +21 21 | # Regression test for: https://github.com/astral-sh/ruff/issues/7197 +22 22 | def create_file_public_url(url, filename): +23 |- return''.join([url, filename]) + 23 |+ return f"{url}{filename}" +