mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 22:55:08 +00:00
[pyupgrade
] Write empty string in lieu of panic (#11696)
## Summary Closes https://github.com/astral-sh/ruff/issues/11692.
This commit is contained in:
parent
9f3e609278
commit
6d79ddc0aa
3 changed files with 59 additions and 5 deletions
|
@ -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;
|
||||
};
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
@ -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 |+""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue