mirror of https://github.com/astral-sh/ruff
Avoid early-exit in explicit-f-string-type-conversion (#4886)
This commit is contained in:
parent
805b2eb0b7
commit
c2a3e97b7f
|
|
@ -12,6 +12,8 @@ f"{str(d['a'])}, {repr(d['b'])}, {ascii(d['c'])}" # RUF010
|
||||||
|
|
||||||
f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
|
|
||||||
|
f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
|
|
||||||
f"{foo(bla)}" # OK
|
f"{foo(bla)}" # OK
|
||||||
|
|
||||||
f"{str(bla, 'ascii')}, {str(bla, encoding='cp1255')}" # OK
|
f"{str(bla, 'ascii')}, {str(bla, encoding='cp1255')}" # OK
|
||||||
|
|
|
||||||
|
|
@ -97,9 +97,10 @@ pub(crate) fn explicit_f_string_type_conversion(
|
||||||
}) = &formatted_value else {
|
}) = &formatted_value else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Skip if there's already a conversion flag.
|
// Skip if there's already a conversion flag.
|
||||||
if !conversion.is_none() {
|
if !conversion.is_none() {
|
||||||
return;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let Expr::Call(ast::ExprCall {
|
let Expr::Call(ast::ExprCall {
|
||||||
|
|
@ -108,24 +109,24 @@ pub(crate) fn explicit_f_string_type_conversion(
|
||||||
keywords,
|
keywords,
|
||||||
..
|
..
|
||||||
}) = value.as_ref() else {
|
}) = value.as_ref() else {
|
||||||
return;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Can't be a conversion otherwise.
|
// Can't be a conversion otherwise.
|
||||||
if args.len() != 1 || !keywords.is_empty() {
|
if args.len() != 1 || !keywords.is_empty() {
|
||||||
return;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let Expr::Name(ast::ExprName { id, .. }) = func.as_ref() else {
|
let Expr::Name(ast::ExprName { id, .. }) = func.as_ref() else {
|
||||||
return;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
if !matches!(id.as_str(), "str" | "repr" | "ascii") {
|
if !matches!(id.as_str(), "str" | "repr" | "ascii") {
|
||||||
return;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
if !checker.semantic_model().is_builtin(id) {
|
if !checker.semantic_model().is_builtin(id) {
|
||||||
return;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut diagnostic = Diagnostic::new(ExplicitFStringTypeConversion, value.range());
|
let mut diagnostic = Diagnostic::new(ExplicitFStringTypeConversion, value.range());
|
||||||
|
|
|
||||||
|
|
@ -128,7 +128,7 @@ RUF010.py:13:5: RUF010 [*] Use conversion in f-string
|
||||||
15 | f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
15 | f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
| ^^^^^^^^ RUF010
|
| ^^^^^^^^ RUF010
|
||||||
16 |
|
16 |
|
||||||
17 | f"{foo(bla)}" # OK
|
17 | f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
|
|
|
|
||||||
= help: Replace f-string function call with conversion
|
= help: Replace f-string function call with conversion
|
||||||
|
|
||||||
|
|
@ -139,7 +139,7 @@ RUF010.py:13:5: RUF010 [*] Use conversion in f-string
|
||||||
13 |-f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
13 |-f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
13 |+f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
13 |+f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
14 14 |
|
14 14 |
|
||||||
15 15 | f"{foo(bla)}" # OK
|
15 15 | f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
16 16 |
|
16 16 |
|
||||||
|
|
||||||
RUF010.py:13:19: RUF010 [*] Use conversion in f-string
|
RUF010.py:13:19: RUF010 [*] Use conversion in f-string
|
||||||
|
|
@ -149,7 +149,7 @@ RUF010.py:13:19: RUF010 [*] Use conversion in f-string
|
||||||
15 | f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
15 | f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
| ^^^^^^^^^ RUF010
|
| ^^^^^^^^^ RUF010
|
||||||
16 |
|
16 |
|
||||||
17 | f"{foo(bla)}" # OK
|
17 | f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
|
|
|
|
||||||
= help: Replace f-string function call with conversion
|
= help: Replace f-string function call with conversion
|
||||||
|
|
||||||
|
|
@ -160,7 +160,7 @@ RUF010.py:13:19: RUF010 [*] Use conversion in f-string
|
||||||
13 |-f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
13 |-f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
13 |+f"{(str(bla))}, {bla!r}, {(ascii(bla))}" # RUF010
|
13 |+f"{(str(bla))}, {bla!r}, {(ascii(bla))}" # RUF010
|
||||||
14 14 |
|
14 14 |
|
||||||
15 15 | f"{foo(bla)}" # OK
|
15 15 | f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
16 16 |
|
16 16 |
|
||||||
|
|
||||||
RUF010.py:13:34: RUF010 [*] Use conversion in f-string
|
RUF010.py:13:34: RUF010 [*] Use conversion in f-string
|
||||||
|
|
@ -170,7 +170,7 @@ RUF010.py:13:34: RUF010 [*] Use conversion in f-string
|
||||||
15 | f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
15 | f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
| ^^^^^^^^^^ RUF010
|
| ^^^^^^^^^^ RUF010
|
||||||
16 |
|
16 |
|
||||||
17 | f"{foo(bla)}" # OK
|
17 | f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
|
|
|
|
||||||
= help: Replace f-string function call with conversion
|
= help: Replace f-string function call with conversion
|
||||||
|
|
||||||
|
|
@ -181,7 +181,49 @@ RUF010.py:13:34: RUF010 [*] Use conversion in f-string
|
||||||
13 |-f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
13 |-f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
13 |+f"{(str(bla))}, {(repr(bla))}, {bla!a}" # RUF010
|
13 |+f"{(str(bla))}, {(repr(bla))}, {bla!a}" # RUF010
|
||||||
14 14 |
|
14 14 |
|
||||||
15 15 | f"{foo(bla)}" # OK
|
15 15 | f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
16 16 |
|
16 16 |
|
||||||
|
|
||||||
|
RUF010.py:15:14: RUF010 [*] Use conversion in f-string
|
||||||
|
|
|
||||||
|
15 | f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
|
16 |
|
||||||
|
17 | f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
|
| ^^^^^^^^^ RUF010
|
||||||
|
18 |
|
||||||
|
19 | f"{foo(bla)}" # OK
|
||||||
|
|
|
||||||
|
= help: Replace f-string function call with conversion
|
||||||
|
|
||||||
|
ℹ Fix
|
||||||
|
12 12 |
|
||||||
|
13 13 | f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
|
14 14 |
|
||||||
|
15 |-f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
|
15 |+f"{bla!s}, {bla!r}, {(ascii(bla))}" # RUF010
|
||||||
|
16 16 |
|
||||||
|
17 17 | f"{foo(bla)}" # OK
|
||||||
|
18 18 |
|
||||||
|
|
||||||
|
RUF010.py:15:29: RUF010 [*] Use conversion in f-string
|
||||||
|
|
|
||||||
|
15 | f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
|
16 |
|
||||||
|
17 | f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
|
| ^^^^^^^^^^ RUF010
|
||||||
|
18 |
|
||||||
|
19 | f"{foo(bla)}" # OK
|
||||||
|
|
|
||||||
|
= help: Replace f-string function call with conversion
|
||||||
|
|
||||||
|
ℹ Fix
|
||||||
|
12 12 |
|
||||||
|
13 13 | f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
|
14 14 |
|
||||||
|
15 |-f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
||||||
|
15 |+f"{bla!s}, {(repr(bla))}, {bla!a}" # RUF010
|
||||||
|
16 16 |
|
||||||
|
17 17 | f"{foo(bla)}" # OK
|
||||||
|
18 18 |
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue