mirror of https://github.com/astral-sh/ruff
Avoid flagging nested f-strings (#1516)
This commit is contained in:
parent
bfdf972a5d
commit
9ba17fbf92
|
|
@ -1,6 +1,8 @@
|
|||
# OK
|
||||
a = "abc"
|
||||
b = f"ghi{'jkl'}"
|
||||
|
||||
# Errors
|
||||
c = f"def"
|
||||
d = f"def" + "ghi"
|
||||
e = (
|
||||
|
|
@ -8,5 +10,17 @@ e = (
|
|||
"ghi"
|
||||
)
|
||||
|
||||
# OK
|
||||
g = f"ghi{123:{45}}"
|
||||
|
||||
# Error
|
||||
h = "x" "y" f"z"
|
||||
|
||||
v = 23.234234
|
||||
|
||||
# OK
|
||||
f"{v:0.2f}"
|
||||
|
||||
# OK (false negatives)
|
||||
f"{v:{f'0.2f'}}"
|
||||
f"{f''}"
|
||||
|
|
|
|||
|
|
@ -91,6 +91,7 @@ pub struct Checker<'a> {
|
|||
in_type_definition: bool,
|
||||
in_deferred_string_type_definition: bool,
|
||||
in_deferred_type_definition: bool,
|
||||
in_f_string: bool,
|
||||
in_literal: bool,
|
||||
in_subscript: bool,
|
||||
seen_import_boundary: bool,
|
||||
|
|
@ -147,6 +148,7 @@ impl<'a> Checker<'a> {
|
|||
in_type_definition: false,
|
||||
in_deferred_string_type_definition: false,
|
||||
in_deferred_type_definition: false,
|
||||
in_f_string: false,
|
||||
in_literal: false,
|
||||
in_subscript: false,
|
||||
seen_import_boundary: false,
|
||||
|
|
@ -1466,6 +1468,7 @@ where
|
|||
|
||||
self.push_expr(expr);
|
||||
|
||||
let prev_in_f_string = self.in_f_string;
|
||||
let prev_in_literal = self.in_literal;
|
||||
let prev_in_type_definition = self.in_type_definition;
|
||||
|
||||
|
|
@ -2151,7 +2154,9 @@ where
|
|||
}
|
||||
}
|
||||
ExprKind::JoinedStr { values } => {
|
||||
if self.settings.enabled.contains(&CheckCode::F541) {
|
||||
// Conversion flags are parsed as f-strings without placeholders, so skip
|
||||
// nested f-strings, which would lead to false positives.
|
||||
if !self.in_f_string && self.settings.enabled.contains(&CheckCode::F541) {
|
||||
if !values
|
||||
.iter()
|
||||
.any(|value| matches!(value.node, ExprKind::FormattedValue { .. }))
|
||||
|
|
@ -2654,6 +2659,11 @@ where
|
|||
}
|
||||
self.in_subscript = prev_in_subscript;
|
||||
}
|
||||
ExprKind::JoinedStr { .. } => {
|
||||
self.in_f_string = true;
|
||||
visitor::walk_expr(self, expr);
|
||||
self.in_f_string = prev_in_f_string;
|
||||
}
|
||||
_ => visitor::walk_expr(self, expr),
|
||||
}
|
||||
|
||||
|
|
@ -2671,6 +2681,7 @@ where
|
|||
|
||||
self.in_type_definition = prev_in_type_definition;
|
||||
self.in_literal = prev_in_literal;
|
||||
self.in_f_string = prev_in_f_string;
|
||||
|
||||
self.pop_expr();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,19 +4,10 @@ expression: checks
|
|||
---
|
||||
- kind: FStringMissingPlaceholders
|
||||
location:
|
||||
row: 4
|
||||
row: 6
|
||||
column: 4
|
||||
end_location:
|
||||
row: 4
|
||||
column: 10
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind: FStringMissingPlaceholders
|
||||
location:
|
||||
row: 5
|
||||
column: 4
|
||||
end_location:
|
||||
row: 5
|
||||
row: 6
|
||||
column: 10
|
||||
fix: ~
|
||||
parent: ~
|
||||
|
|
@ -31,10 +22,19 @@ expression: checks
|
|||
parent: ~
|
||||
- kind: FStringMissingPlaceholders
|
||||
location:
|
||||
row: 12
|
||||
row: 9
|
||||
column: 4
|
||||
end_location:
|
||||
row: 12
|
||||
row: 9
|
||||
column: 10
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind: FStringMissingPlaceholders
|
||||
location:
|
||||
row: 17
|
||||
column: 4
|
||||
end_location:
|
||||
row: 17
|
||||
column: 16
|
||||
fix: ~
|
||||
parent: ~
|
||||
|
|
|
|||
Loading…
Reference in New Issue