Skip to content

Commit

Permalink
[pyupgrade] Write empty string in lieu of panic (#11696)
Browse files Browse the repository at this point in the history
## Summary

Closes #11692.
  • Loading branch information
charliermarsh authored Jun 2, 2024
1 parent 9f3e609 commit 6d79ddc
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,9 @@ async def c():

# The string _should_ be converted, since the function call is repeated in the arguments.
"{0} {1}".format(foo(), foo())

# The call should be removed, but the string itself should remain.
''.format(self.project)

# The call should be removed, but the string itself should remain.
"".format(self.project)
18 changes: 13 additions & 5 deletions crates/ruff_linter/src/rules/pyupgrade/rules/f_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ pub(crate) fn f_strings(checker: &mut Checker, call: &ast::ExprCall, summary: &F
return;
};

if !value.is_string_literal_expr() {
let Expr::StringLiteral(literal) = &**value else {
return;
};

Expand Down Expand Up @@ -520,10 +520,18 @@ pub(crate) fn f_strings(checker: &mut Checker, call: &ast::ExprCall, summary: &F
.intersects(call.arguments.range());

if !has_comments {
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
contents,
call.range(),
)));
if contents.is_empty() {
// Ex) `''.format(self.project)`
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
checker.locator().slice(literal).to_string(),
call.range(),
)));
} else {
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
contents,
call.range(),
)));
}
};
checker.diagnostics.push(diagnostic);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,8 @@ UP032_0.py:261:1: UP032 [*] Use f-string instead of `format` call
260 | # The string _should_ be converted, since the function call is repeated in the arguments.
261 | "{0} {1}".format(foo(), foo())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP032
262 |
263 | # The call should be removed, but the string itself should remain.
|
= help: Convert to f-string

Expand All @@ -1330,3 +1332,41 @@ UP032_0.py:261:1: UP032 [*] Use f-string instead of `format` call
260 260 | # The string _should_ be converted, since the function call is repeated in the arguments.
261 |-"{0} {1}".format(foo(), foo())
261 |+f"{foo()} {foo()}"
262 262 |
263 263 | # The call should be removed, but the string itself should remain.
264 264 | ''.format(self.project)

UP032_0.py:264:1: UP032 [*] Use f-string instead of `format` call
|
263 | # The call should be removed, but the string itself should remain.
264 | ''.format(self.project)
| ^^^^^^^^^^^^^^^^^^^^^^^ UP032
265 |
266 | # The call should be removed, but the string itself should remain.
|
= help: Convert to f-string

Safe fix
261 261 | "{0} {1}".format(foo(), foo())
262 262 |
263 263 | # The call should be removed, but the string itself should remain.
264 |-''.format(self.project)
264 |+''
265 265 |
266 266 | # The call should be removed, but the string itself should remain.
267 267 | "".format(self.project)

UP032_0.py:267:1: UP032 [*] Use f-string instead of `format` call
|
266 | # The call should be removed, but the string itself should remain.
267 | "".format(self.project)
| ^^^^^^^^^^^^^^^^^^^^^^^ UP032
|
= help: Convert to f-string

Safe fix
264 264 | ''.format(self.project)
265 265 |
266 266 | # The call should be removed, but the string itself should remain.
267 |-"".format(self.project)
267 |+""

0 comments on commit 6d79ddc

Please sign in to comment.