[pyupgrade] Write empty string in lieu of panic (#11696)

## Summary

Closes https://github.com/astral-sh/ruff/issues/11692.
This commit is contained in:
Charlie Marsh 2024-06-02 13:51:03 -04:00 committed by GitHub
parent 9f3e609278
commit 6d79ddc0aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 59 additions and 5 deletions

View file

@ -259,3 +259,9 @@ raise ValueError(
# The string _should_ be converted, since the function call is repeated in the arguments. # The string _should_ be converted, since the function call is repeated in the arguments.
"{0} {1}".format(foo(), foo()) "{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)

View file

@ -400,7 +400,7 @@ pub(crate) fn f_strings(checker: &mut Checker, call: &ast::ExprCall, summary: &F
return; return;
}; };
if !value.is_string_literal_expr() { let Expr::StringLiteral(literal) = &**value else {
return; return;
}; };
@ -520,10 +520,18 @@ pub(crate) fn f_strings(checker: &mut Checker, call: &ast::ExprCall, summary: &F
.intersects(call.arguments.range()); .intersects(call.arguments.range());
if !has_comments { if !has_comments {
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( if contents.is_empty() {
contents, // Ex) `''.format(self.project)`
call.range(), 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); checker.diagnostics.push(diagnostic);
} }

View file

@ -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. 260 | # The string _should_ be converted, since the function call is repeated in the arguments.
261 | "{0} {1}".format(foo(), foo()) 261 | "{0} {1}".format(foo(), foo())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP032 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP032
262 |
263 | # The call should be removed, but the string itself should remain.
| |
= help: Convert to f-string = help: Convert to f-string
@ -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. 260 260 | # The string _should_ be converted, since the function call is repeated in the arguments.
261 |-"{0} {1}".format(foo(), foo()) 261 |-"{0} {1}".format(foo(), foo())
261 |+f"{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 |+""