Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |