Avoid early-exit in explicit-f-string-type-conversion (#4886)

This commit is contained in:
Charlie Marsh 2023-06-05 20:52:11 -04:00 committed by GitHub
parent 805b2eb0b7
commit c2a3e97b7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 12 deletions

View file

@ -12,6 +12,8 @@ f"{str(d['a'])}, {repr(d['b'])}, {ascii(d['c'])}" # RUF010
f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
f"{foo(bla)}" # OK
f"{str(bla, 'ascii')}, {str(bla, encoding='cp1255')}" # OK

View file

@ -97,9 +97,10 @@ pub(crate) fn explicit_f_string_type_conversion(
}) = &formatted_value else {
continue;
};
// Skip if there's already a conversion flag.
if !conversion.is_none() {
return;
continue;
}
let Expr::Call(ast::ExprCall {
@ -108,24 +109,24 @@ pub(crate) fn explicit_f_string_type_conversion(
keywords,
..
}) = value.as_ref() else {
return;
continue;
};
// Can't be a conversion otherwise.
if args.len() != 1 || !keywords.is_empty() {
return;
continue;
}
let Expr::Name(ast::ExprName { id, .. }) = func.as_ref() else {
return;
continue;
};
if !matches!(id.as_str(), "str" | "repr" | "ascii") {
return;
continue;
};
if !checker.semantic_model().is_builtin(id) {
return;
continue;
}
let mut diagnostic = Diagnostic::new(ExplicitFStringTypeConversion, value.range());

View file

@ -128,7 +128,7 @@ RUF010.py:13:5: RUF010 [*] Use conversion in f-string
15 | f"{(str(bla))}, {(repr(bla))}, {(ascii(bla))}" # RUF010
| ^^^^^^^^ RUF010
16 |
17 | f"{foo(bla)}" # OK
17 | f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
= 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"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
14 14 |
15 15 | f"{foo(bla)}" # OK
15 15 | f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
16 16 |
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
| ^^^^^^^^^ RUF010
16 |
17 | f"{foo(bla)}" # OK
17 | f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
= 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))}, {bla!r}, {(ascii(bla))}" # RUF010
14 14 |
15 15 | f"{foo(bla)}" # OK
15 15 | f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
16 16 |
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
| ^^^^^^^^^^ RUF010
16 |
17 | f"{foo(bla)}" # OK
17 | f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
|
= 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))}, {bla!a}" # RUF010
14 14 |
15 15 | f"{foo(bla)}" # OK
15 15 | f"{bla!s}, {(repr(bla))}, {(ascii(bla))}" # RUF010
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 |