Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/flynt/FLY002.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@
# Regression test for: https://github.com/astral-sh/ruff/issues/7197
def create_file_public_url(url, filename):
return''.join([url, filename])

# Regression test for: https://github.com/astral-sh/ruff/issues/19837
nok10 = "".join((foo, '"'))
nok11 = ''.join((foo, "'"))
nok12 = ''.join([foo, "'", '"'])
nok13 = "".join([foo, "'", '"'])
25 changes: 22 additions & 3 deletions crates/ruff_linter/src/rules/flynt/rules/static_join_to_fstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ast::FStringFlags;
use itertools::Itertools;

use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::{self as ast, Arguments, Expr, StringFlags};
use ruff_python_ast::{self as ast, Arguments, Expr, StringFlags, str::Quote};
use ruff_text_size::{Ranged, TextRange};

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -115,6 +115,8 @@ fn build_fstring(joiner: &str, joinees: &[Expr], flags: FStringFlags) -> Option<
}

let mut f_string_elements = Vec::with_capacity(joinees.len() * 2);
let mut has_single_quote = joiner.contains('\'');
let mut has_double_quote = joiner.contains('"');
let mut first = true;

for expr in joinees {
Expand All @@ -126,14 +128,31 @@ fn build_fstring(joiner: &str, joinees: &[Expr], flags: FStringFlags) -> Option<
if !std::mem::take(&mut first) {
f_string_elements.push(helpers::to_interpolated_string_literal_element(joiner));
}
f_string_elements.push(helpers::to_interpolated_string_element(expr)?);
let element = helpers::to_interpolated_string_element(expr)?;
if let ast::InterpolatedStringElement::Literal(ast::InterpolatedStringLiteralElement {
value,
..
}) = &element
{
has_single_quote |= value.contains('\'');
has_double_quote |= value.contains('"');
}
f_string_elements.push(element);
}

let quote = flags.quote_style();
let adjusted_quote = match quote {
Quote::Single if has_single_quote && !has_double_quote => quote.opposite(),
Quote::Double if has_double_quote && !has_single_quote => quote.opposite(),
_ if has_double_quote && has_single_quote => return None,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the content includes both single quote and double quote, ruff skips FLY002 for now.

I will improve this logic in a separate pull request as we discussed here.

#20197 (review)

_ => quote,
};

let node = ast::FString {
elements: f_string_elements.into(),
range: TextRange::default(),
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
flags,
flags: flags.with_quote_style(adjusted_quote),
};
Some(node.into())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,56 @@ FLY002 [*] Consider `f"{url}{filename}"` instead of string join
24 | def create_file_public_url(url, filename):
25 | return''.join([url, filename])
| ^^^^^^^^^^^^^^^^^^^^^^^^
26 |
27 | # Regression test for: https://github.com/astral-sh/ruff/issues/19837
|
help: Replace with `f"{url}{filename}"`
22 |
23 | # Regression test for: https://github.com/astral-sh/ruff/issues/7197
24 | def create_file_public_url(url, filename):
- return''.join([url, filename])
25 + return f"{url}{filename}"
26 |
27 | # Regression test for: https://github.com/astral-sh/ruff/issues/19837
28 | nok10 = "".join((foo, '"'))
note: This is an unsafe fix and may change runtime behavior

FLY002 [*] Consider `f'{foo}"'` instead of string join
--> FLY002.py:28:9
|
27 | # Regression test for: https://github.com/astral-sh/ruff/issues/19837
28 | nok10 = "".join((foo, '"'))
| ^^^^^^^^^^^^^^^^^^^
29 | nok11 = ''.join((foo, "'"))
30 | nok12 = ''.join([foo, "'", '"'])
|
help: Replace with `f'{foo}"'`
25 | return''.join([url, filename])
26 |
27 | # Regression test for: https://github.com/astral-sh/ruff/issues/19837
- nok10 = "".join((foo, '"'))
28 + nok10 = f'{foo}"'
29 | nok11 = ''.join((foo, "'"))
30 | nok12 = ''.join([foo, "'", '"'])
31 | nok13 = "".join([foo, "'", '"'])
note: This is an unsafe fix and may change runtime behavior

FLY002 [*] Consider `f"{foo}'"` instead of string join
--> FLY002.py:29:9
|
27 | # Regression test for: https://github.com/astral-sh/ruff/issues/19837
28 | nok10 = "".join((foo, '"'))
29 | nok11 = ''.join((foo, "'"))
| ^^^^^^^^^^^^^^^^^^^
30 | nok12 = ''.join([foo, "'", '"'])
31 | nok13 = "".join([foo, "'", '"'])
|
help: Replace with `f"{foo}'"`
26 |
27 | # Regression test for: https://github.com/astral-sh/ruff/issues/19837
28 | nok10 = "".join((foo, '"'))
- nok11 = ''.join((foo, "'"))
29 + nok11 = f"{foo}'"
30 | nok12 = ''.join([foo, "'", '"'])
31 | nok13 = "".join([foo, "'", '"'])
note: This is an unsafe fix and may change runtime behavior
Loading