mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:24 +00:00
Flag FURB105 with starred kwargs (#7630)
This commit is contained in:
parent
15813a65f3
commit
f32b0eef9c
3 changed files with 34 additions and 19 deletions
|
@ -16,6 +16,7 @@ print("foo", "", sep="")
|
||||||
print("foo", "", "bar", sep="")
|
print("foo", "", "bar", sep="")
|
||||||
print("", *args)
|
print("", *args)
|
||||||
print("", *args, sep="")
|
print("", *args, sep="")
|
||||||
|
print("", **kwargs)
|
||||||
|
|
||||||
# OK.
|
# OK.
|
||||||
|
|
||||||
|
@ -29,4 +30,3 @@ print("", "foo", sep=",")
|
||||||
print("foo", "", sep=",")
|
print("foo", "", sep=",")
|
||||||
print("foo", "", "bar", "", sep=",")
|
print("foo", "", "bar", "", sep=",")
|
||||||
print("", "", **kwargs)
|
print("", "", **kwargs)
|
||||||
print("", **kwargs)
|
|
||||||
|
|
|
@ -57,27 +57,22 @@ impl Violation for PrintEmptyString {
|
||||||
|
|
||||||
/// FURB105
|
/// FURB105
|
||||||
pub(crate) fn print_empty_string(checker: &mut Checker, call: &ast::ExprCall) {
|
pub(crate) fn print_empty_string(checker: &mut Checker, call: &ast::ExprCall) {
|
||||||
// Avoid flagging, e.g., `print("", "", **kwargs)`.
|
|
||||||
if call
|
|
||||||
.arguments
|
|
||||||
.keywords
|
|
||||||
.iter()
|
|
||||||
.any(|keyword| keyword.arg.is_none())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if checker
|
if checker
|
||||||
.semantic()
|
.semantic()
|
||||||
.resolve_call_path(&call.func)
|
.resolve_call_path(&call.func)
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.is_some_and(|call_path| matches!(call_path.as_slice(), ["", "print"]))
|
.is_some_and(|call_path| matches!(call_path.as_slice(), ["", "print"]))
|
||||||
{
|
{
|
||||||
// Ex) `print("", sep="")`
|
// Ex) `print("", sep="")` or `print("", "", **kwargs)`
|
||||||
let empty_separator = call
|
let empty_separator = call
|
||||||
.arguments
|
.arguments
|
||||||
.find_keyword("sep")
|
.find_keyword("sep")
|
||||||
.map_or(false, |keyword| is_empty_string(&keyword.value));
|
.map_or(false, |keyword| is_empty_string(&keyword.value))
|
||||||
|
&& !call
|
||||||
|
.arguments
|
||||||
|
.keywords
|
||||||
|
.iter()
|
||||||
|
.any(|keyword| keyword.arg.is_none());
|
||||||
|
|
||||||
// Avoid flagging, e.g., `print("", "", sep="sep")`
|
// Avoid flagging, e.g., `print("", "", sep="sep")`
|
||||||
if !empty_separator && call.arguments.args.len() != 1 {
|
if !empty_separator && call.arguments.args.len() != 1 {
|
||||||
|
|
|
@ -291,7 +291,7 @@ FURB105.py:16:1: FURB105 [*] Unnecessary empty string passed to `print`
|
||||||
16 |+print("foo", "bar", sep="")
|
16 |+print("foo", "bar", sep="")
|
||||||
17 17 | print("", *args)
|
17 17 | print("", *args)
|
||||||
18 18 | print("", *args, sep="")
|
18 18 | print("", *args, sep="")
|
||||||
19 19 |
|
19 19 | print("", **kwargs)
|
||||||
|
|
||||||
FURB105.py:18:1: FURB105 [*] Unnecessary empty string passed to `print`
|
FURB105.py:18:1: FURB105 [*] Unnecessary empty string passed to `print`
|
||||||
|
|
|
|
||||||
|
@ -299,8 +299,7 @@ FURB105.py:18:1: FURB105 [*] Unnecessary empty string passed to `print`
|
||||||
17 | print("", *args)
|
17 | print("", *args)
|
||||||
18 | print("", *args, sep="")
|
18 | print("", *args, sep="")
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ FURB105
|
| ^^^^^^^^^^^^^^^^^^^^^^^^ FURB105
|
||||||
19 |
|
19 | print("", **kwargs)
|
||||||
20 | # OK.
|
|
||||||
|
|
|
|
||||||
= help: Remove empty string
|
= help: Remove empty string
|
||||||
|
|
||||||
|
@ -310,8 +309,29 @@ FURB105.py:18:1: FURB105 [*] Unnecessary empty string passed to `print`
|
||||||
17 17 | print("", *args)
|
17 17 | print("", *args)
|
||||||
18 |-print("", *args, sep="")
|
18 |-print("", *args, sep="")
|
||||||
18 |+print(*args, sep="")
|
18 |+print(*args, sep="")
|
||||||
19 19 |
|
19 19 | print("", **kwargs)
|
||||||
20 20 | # OK.
|
20 20 |
|
||||||
21 21 |
|
21 21 | # OK.
|
||||||
|
|
||||||
|
FURB105.py:19:1: FURB105 [*] Unnecessary empty string passed to `print`
|
||||||
|
|
|
||||||
|
17 | print("", *args)
|
||||||
|
18 | print("", *args, sep="")
|
||||||
|
19 | print("", **kwargs)
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^ FURB105
|
||||||
|
20 |
|
||||||
|
21 | # OK.
|
||||||
|
|
|
||||||
|
= help: Remove empty string
|
||||||
|
|
||||||
|
ℹ Suggested fix
|
||||||
|
16 16 | print("foo", "", "bar", sep="")
|
||||||
|
17 17 | print("", *args)
|
||||||
|
18 18 | print("", *args, sep="")
|
||||||
|
19 |-print("", **kwargs)
|
||||||
|
19 |+print(**kwargs)
|
||||||
|
20 20 |
|
||||||
|
21 21 | # OK.
|
||||||
|
22 22 |
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue