Add required space for FLY002 fixes (#7222)

Closes https://github.com/astral-sh/ruff/issues/7197.
This commit is contained in:
Charlie Marsh 2023-09-07 16:32:43 +02:00 committed by GitHub
parent 7971e0b0ee
commit 5cea43731e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 1 deletions

View file

@ -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])

View file

@ -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<Expr> {
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(),
)));
}

View file

@ -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}"