Fix for F541 unescape f-string (#2971)

This commit is contained in:
Simon Brugman 2023-02-17 20:27:01 +01:00 committed by GitHub
parent 909a5c3253
commit 0dd590f137
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 128 additions and 26 deletions

View File

@ -33,3 +33,11 @@ f"{f'{v:0.2f}'}"
# Errors # Errors
f"{v:{f'0.2f'}}" f"{v:{f'0.2f'}}"
f"{f''}" f"{f''}"
f"{{test}}"
f'{{ 40 }}'
f"{{a {{x}}"
f"{{{{x}}}}"
# To be fixed
# Error: f-string: single '}' is not allowed at line 41 column 8
# f"\{{x}}"

View File

@ -2,6 +2,7 @@ use ruff_macros::{define_violation, derive_message_formats};
use rustpython_parser::ast::{Expr, ExprKind}; use rustpython_parser::ast::{Expr, ExprKind};
use crate::ast::helpers::find_useless_f_strings; use crate::ast::helpers::find_useless_f_strings;
use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::fix::Fix; use crate::fix::Fix;
use crate::registry::Diagnostic; use crate::registry::Diagnostic;
@ -46,6 +47,26 @@ impl AlwaysAutofixableViolation for FStringMissingPlaceholders {
} }
} }
fn unescape_f_string(content: &str) -> String {
content.replace("{{", "{").replace("}}", "}")
}
fn fix_f_string_missing_placeholders(
prefix_range: &Range,
tok_range: &Range,
checker: &mut Checker,
) -> Fix {
let content = checker.locator.slice(&Range::new(
prefix_range.end_location,
tok_range.end_location,
));
Fix::replacement(
unescape_f_string(content),
prefix_range.location,
tok_range.end_location,
)
}
/// F541 /// F541
pub fn f_string_missing_placeholders(expr: &Expr, values: &[Expr], checker: &mut Checker) { pub fn f_string_missing_placeholders(expr: &Expr, values: &[Expr], checker: &mut Checker) {
if !values if !values
@ -55,9 +76,10 @@ pub fn f_string_missing_placeholders(expr: &Expr, values: &[Expr], checker: &mut
for (prefix_range, tok_range) in find_useless_f_strings(expr, checker.locator) { for (prefix_range, tok_range) in find_useless_f_strings(expr, checker.locator) {
let mut diagnostic = Diagnostic::new(FStringMissingPlaceholders, tok_range); let mut diagnostic = Diagnostic::new(FStringMissingPlaceholders, tok_range);
if checker.patch(diagnostic.kind.rule()) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::deletion( diagnostic.amend(fix_f_string_missing_placeholders(
prefix_range.location, &prefix_range,
prefix_range.end_location, &tok_range,
checker,
)); ));
} }
checker.diagnostics.push(diagnostic); checker.diagnostics.push(diagnostic);

View File

@ -1,5 +1,5 @@
--- ---
source: src/rules/pyflakes/mod.rs source: crates/ruff/src/rules/pyflakes/mod.rs
expression: diagnostics expression: diagnostics
--- ---
- kind: - kind:
@ -12,13 +12,13 @@ expression: diagnostics
column: 10 column: 10
fix: fix:
content: content:
- "" - "\"def\""
location: location:
row: 6 row: 6
column: 4 column: 4
end_location: end_location:
row: 6 row: 6
column: 5 column: 10
parent: ~ parent: ~
- kind: - kind:
FStringMissingPlaceholders: ~ FStringMissingPlaceholders: ~
@ -30,13 +30,13 @@ expression: diagnostics
column: 10 column: 10
fix: fix:
content: content:
- "" - "\"def\""
location: location:
row: 7 row: 7
column: 4 column: 4
end_location: end_location:
row: 7 row: 7
column: 5 column: 10
parent: ~ parent: ~
- kind: - kind:
FStringMissingPlaceholders: ~ FStringMissingPlaceholders: ~
@ -48,13 +48,13 @@ expression: diagnostics
column: 10 column: 10
fix: fix:
content: content:
- "" - "\"def\""
location: location:
row: 9 row: 9
column: 4 column: 4
end_location: end_location:
row: 9 row: 9
column: 5 column: 10
parent: ~ parent: ~
- kind: - kind:
FStringMissingPlaceholders: ~ FStringMissingPlaceholders: ~
@ -66,13 +66,13 @@ expression: diagnostics
column: 8 column: 8
fix: fix:
content: content:
- "" - "\"a\""
location: location:
row: 13 row: 13
column: 4 column: 4
end_location: end_location:
row: 13 row: 13
column: 5 column: 8
parent: ~ parent: ~
- kind: - kind:
FStringMissingPlaceholders: ~ FStringMissingPlaceholders: ~
@ -84,13 +84,13 @@ expression: diagnostics
column: 8 column: 8
fix: fix:
content: content:
- "" - "\"b\""
location: location:
row: 14 row: 14
column: 4 column: 4
end_location: end_location:
row: 14 row: 14
column: 5 column: 8
parent: ~ parent: ~
- kind: - kind:
FStringMissingPlaceholders: ~ FStringMissingPlaceholders: ~
@ -102,13 +102,13 @@ expression: diagnostics
column: 9 column: 9
fix: fix:
content: content:
- "" - "\"d\""
location: location:
row: 16 row: 16
column: 5 column: 5
end_location: end_location:
row: 16 row: 16
column: 6 column: 9
parent: ~ parent: ~
- kind: - kind:
FStringMissingPlaceholders: ~ FStringMissingPlaceholders: ~
@ -120,13 +120,13 @@ expression: diagnostics
column: 9 column: 9
fix: fix:
content: content:
- "" - "r\"e\""
location: location:
row: 17 row: 17
column: 4 column: 4
end_location: end_location:
row: 17 row: 17
column: 5 column: 9
parent: ~ parent: ~
- kind: - kind:
FStringMissingPlaceholders: ~ FStringMissingPlaceholders: ~
@ -138,13 +138,13 @@ expression: diagnostics
column: 7 column: 7
fix: fix:
content: content:
- "" - "\"\""
location: location:
row: 19 row: 19
column: 4 column: 4
end_location: end_location:
row: 19 row: 19
column: 5 column: 7
parent: ~ parent: ~
- kind: - kind:
FStringMissingPlaceholders: ~ FStringMissingPlaceholders: ~
@ -156,13 +156,13 @@ expression: diagnostics
column: 16 column: 16
fix: fix:
content: content:
- "" - "\"z\""
location: location:
row: 25 row: 25
column: 12 column: 12
end_location: end_location:
row: 25 row: 25
column: 13 column: 16
parent: ~ parent: ~
- kind: - kind:
FStringMissingPlaceholders: ~ FStringMissingPlaceholders: ~
@ -174,13 +174,13 @@ expression: diagnostics
column: 13 column: 13
fix: fix:
content: content:
- "" - "'0.2f'"
location: location:
row: 34 row: 34
column: 6 column: 6
end_location: end_location:
row: 34 row: 34
column: 7 column: 13
parent: ~ parent: ~
- kind: - kind:
FStringMissingPlaceholders: ~ FStringMissingPlaceholders: ~
@ -192,12 +192,84 @@ expression: diagnostics
column: 6 column: 6
fix: fix:
content: content:
- "" - "''"
location: location:
row: 35 row: 35
column: 3 column: 3
end_location: end_location:
row: 35 row: 35
column: 4 column: 6
parent: ~
- kind:
FStringMissingPlaceholders: ~
location:
row: 36
column: 0
end_location:
row: 36
column: 11
fix:
content:
- "\"{test}\""
location:
row: 36
column: 0
end_location:
row: 36
column: 11
parent: ~
- kind:
FStringMissingPlaceholders: ~
location:
row: 37
column: 0
end_location:
row: 37
column: 11
fix:
content:
- "'{ 40 }'"
location:
row: 37
column: 0
end_location:
row: 37
column: 11
parent: ~
- kind:
FStringMissingPlaceholders: ~
location:
row: 38
column: 0
end_location:
row: 38
column: 12
fix:
content:
- "\"{a {x}\""
location:
row: 38
column: 0
end_location:
row: 38
column: 12
parent: ~
- kind:
FStringMissingPlaceholders: ~
location:
row: 39
column: 0
end_location:
row: 39
column: 12
fix:
content:
- "\"{{x}}\""
location:
row: 39
column: 0
end_location:
row: 39
column: 12
parent: ~ parent: ~