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

Flag FURB105 with starred kwargs #7630

Merged
merged 1 commit into from Sep 24, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,6 +16,7 @@
print("foo", "", "bar", sep="")
print("", *args)
print("", *args, sep="")
print("", **kwargs)

# OK.

Expand All @@ -29,4 +30,3 @@
print("foo", "", sep=",")
print("foo", "", "bar", "", sep=",")
print("", "", **kwargs)
print("", **kwargs)
19 changes: 7 additions & 12 deletions crates/ruff_linter/src/rules/refurb/rules/print_empty_string.rs
Expand Up @@ -57,27 +57,22 @@ impl Violation for PrintEmptyString {

/// FURB105
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
.semantic()
.resolve_call_path(&call.func)
.as_ref()
.is_some_and(|call_path| matches!(call_path.as_slice(), ["", "print"]))
{
// Ex) `print("", sep="")`
// Ex) `print("", sep="")` or `print("", "", **kwargs)`
let empty_separator = call
.arguments
.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")`
if !empty_separator && call.arguments.args.len() != 1 {
Expand Down
Expand Up @@ -291,16 +291,15 @@ FURB105.py:16:1: FURB105 [*] Unnecessary empty string passed to `print`
16 |+print("foo", "bar", sep="")
17 17 | print("", *args)
18 18 | print("", *args, sep="")
19 19 |
19 19 | print("", **kwargs)

FURB105.py:18:1: FURB105 [*] Unnecessary empty string passed to `print`
|
16 | print("foo", "", "bar", sep="")
17 | print("", *args)
18 | print("", *args, sep="")
| ^^^^^^^^^^^^^^^^^^^^^^^^ FURB105
19 |
20 | # OK.
19 | print("", **kwargs)
|
= help: Remove empty string

Expand All @@ -310,8 +309,29 @@ FURB105.py:18:1: FURB105 [*] Unnecessary empty string passed to `print`
17 17 | print("", *args)
18 |-print("", *args, sep="")
18 |+print(*args, sep="")
19 19 |
20 20 | # OK.
21 21 |
19 19 | print("", **kwargs)
20 20 |
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 |