Skip to content

Commit 7caf0d0

Browse files
authored
Simplify formatting of strings by using flags from the AST nodes (#10489)
1 parent fc792d1 commit 7caf0d0

22 files changed

+725
-809
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ruff_linter/src/rules/pylint/rules/bad_string_format_character.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use std::str::FromStr;
22

33
use ruff_diagnostics::{Diagnostic, Violation};
44
use ruff_macros::{derive_message_formats, violation};
5-
use ruff_python_ast::Expr;
5+
use ruff_python_ast::{AnyStringKind, Expr};
66
use ruff_python_literal::{
77
cformat::{CFormatErrorType, CFormatString},
88
format::FormatPart,
99
format::FromTemplate,
1010
format::{FormatSpec, FormatSpecError, FormatString},
1111
};
12-
use ruff_python_parser::{lexer, Mode, StringKind, Tok};
12+
use ruff_python_parser::{lexer, Mode, Tok};
1313
use ruff_text_size::{Ranged, TextRange};
1414

1515
use crate::checkers::ast::Checker;
@@ -92,7 +92,7 @@ pub(crate) fn call(checker: &mut Checker, string: &str, range: TextRange) {
9292
/// Ex) `"%z" % "1"`
9393
pub(crate) fn percent(checker: &mut Checker, expr: &Expr) {
9494
// Grab each string segment (in case there's an implicit concatenation).
95-
let mut strings: Vec<(TextRange, StringKind)> = vec![];
95+
let mut strings: Vec<(TextRange, AnyStringKind)> = vec![];
9696
for (tok, range) in
9797
lexer::lex_starts_at(checker.locator().slice(expr), Mode::Module, expr.start()).flatten()
9898
{

crates/ruff_linter/src/rules/pylint/rules/bad_string_format_type.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::str::FromStr;
22

3-
use ruff_python_ast::{self as ast, Expr};
3+
use ruff_python_ast::{self as ast, AnyStringKind, Expr};
44
use ruff_python_literal::cformat::{CFormatPart, CFormatSpec, CFormatStrOrBytes, CFormatString};
5-
use ruff_python_parser::{lexer, AsMode, StringKind, Tok};
5+
use ruff_python_parser::{lexer, AsMode, Tok};
66
use ruff_text_size::{Ranged, TextRange};
77
use rustc_hash::FxHashMap;
88

@@ -218,7 +218,7 @@ fn is_valid_dict(
218218
pub(crate) fn bad_string_format_type(checker: &mut Checker, expr: &Expr, right: &Expr) {
219219
// Grab each string segment (in case there's an implicit concatenation).
220220
let content = checker.locator().slice(expr);
221-
let mut strings: Vec<(TextRange, StringKind)> = vec![];
221+
let mut strings: Vec<(TextRange, AnyStringKind)> = vec![];
222222
for (tok, range) in
223223
lexer::lex_starts_at(content, checker.source_type.as_mode(), expr.start()).flatten()
224224
{

crates/ruff_linter/src/rules/pyupgrade/rules/printf_string_formatting.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use std::str::FromStr;
44
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
55
use ruff_macros::{derive_message_formats, violation};
66
use ruff_python_ast::whitespace::indentation;
7-
use ruff_python_ast::{self as ast, Expr};
7+
use ruff_python_ast::{self as ast, AnyStringKind, Expr};
88
use ruff_python_codegen::Stylist;
99
use ruff_python_literal::cformat::{
1010
CConversionFlags, CFormatPart, CFormatPrecision, CFormatQuantity, CFormatString,
1111
};
12-
use ruff_python_parser::{lexer, AsMode, StringKind, Tok};
12+
use ruff_python_parser::{lexer, AsMode, Tok};
1313
use ruff_python_stdlib::identifiers::is_identifier;
1414
use ruff_source_file::Locator;
1515
use ruff_text_size::{Ranged, TextRange};
@@ -352,7 +352,7 @@ fn convertible(format_string: &CFormatString, params: &Expr) -> bool {
352352
/// UP031
353353
pub(crate) fn printf_string_formatting(checker: &mut Checker, expr: &Expr, right: &Expr) {
354354
// Grab each string segment (in case there's an implicit concatenation).
355-
let mut strings: Vec<(TextRange, StringKind)> = vec![];
355+
let mut strings: Vec<(TextRange, AnyStringKind)> = vec![];
356356
let mut extension = None;
357357
for (tok, range) in lexer::lex_starts_at(
358358
checker.locator().slice(expr),

0 commit comments

Comments
 (0)