Skip to content

Commit

Permalink
Avoid early-exit in explicit-f-string-type-conversion (#4886)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored and konstin committed Jun 13, 2023
1 parent ef90d5a commit 253e05e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
2 changes: 2 additions & 0 deletions crates/ruff/resources/test/fixtures/ruff/RUF010.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def foo(one_arg):

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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 |


0 comments on commit 253e05e

Please sign in to comment.