Skip to content

Commit

Permalink
perf: defer checking sep kwarg
Browse files Browse the repository at this point in the history
  • Loading branch information
tjkuson committed Sep 23, 2023
1 parent 7bf84c8 commit 81fa094
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 10 deletions.
25 changes: 15 additions & 10 deletions crates/ruff_linter/src/rules/refurb/rules/print_empty_string.rs
Expand Up @@ -70,16 +70,21 @@ pub(crate) fn print_empty_string(checker: &mut Checker, call: &ast::ExprCall) {
.as_ref()
.is_some_and(|call_path| matches!(call_path.as_slice(), ["", "print"]))
{
// Check if the `sep` keyword argument is an empty string.
let sep_value_is_empty_string = call
.arguments
.find_keyword("sep")
.map_or(false, |keyword| is_const_empty_string(&keyword.value));

// If the print call does not have precisely one positional argument,
// do not trigger unless the `sep` keyword argument is an empty string.
if call.arguments.args.len() != 1 && !sep_value_is_empty_string {
return;
// For performance reasons, defer assignment to until we know that we
// need to check if the separator is an empty string.
let mut sep_value_is_empty_string = false;

// If the call does not have only one positional argument, check if the
// `sep` keyword argument is an empty string; if it is not an empty
// string, don't trigger.
if call.arguments.args.len() != 1 {
sep_value_is_empty_string = call
.arguments
.find_keyword("sep")
.map_or(false, |keyword| is_const_empty_string(&keyword.value));
if !sep_value_is_empty_string {
return;
}
}

// Check if the positional arguments is are all empty strings, or if
Expand Down
@@ -0,0 +1,233 @@
---
source: crates/ruff_linter/src/rules/refurb/mod.rs
assertion_line: 30
---
FURB105.py:3:1: FURB105 [*] Called `print` with an empty string, use `print()` instead
|
1 | # Errors.
2 |
3 | print("")
| ^^^^^^^^^ FURB105
4 | print("", sep=",")
5 | print("", end="bar")
|
= help: Remove empty string positional argument

ℹ Suggested fix
1 1 | # Errors.
2 2 |
3 |-print("")
3 |+print()
4 4 | print("", sep=",")
5 5 | print("", end="bar")
6 6 | print("", sep=",", end="bar")

FURB105.py:4:1: FURB105 [*] Called `print` with an empty string and a redundant separator, use `print()` instead
|
3 | print("")
4 | print("", sep=",")
| ^^^^^^^^^^^^^^^^^^ FURB105
5 | print("", end="bar")
6 | print("", sep=",", end="bar")
|
= help: Remove empty string positional argument and redundant separator

ℹ Suggested fix
1 1 | # Errors.
2 2 |
3 3 | print("")
4 |-print("", sep=",")
4 |+print()
5 5 | print("", end="bar")
6 6 | print("", sep=",", end="bar")
7 7 | print(sep="")

FURB105.py:5:1: FURB105 [*] Called `print` with an empty string, use `print(end="bar")` instead
|
3 | print("")
4 | print("", sep=",")
5 | print("", end="bar")
| ^^^^^^^^^^^^^^^^^^^^ FURB105
6 | print("", sep=",", end="bar")
7 | print(sep="")
|
= help: Remove empty string positional argument

ℹ Suggested fix
2 2 |
3 3 | print("")
4 4 | print("", sep=",")
5 |-print("", end="bar")
5 |+print(end="bar")
6 6 | print("", sep=",", end="bar")
7 7 | print(sep="")
8 8 | print("", sep="")

FURB105.py:6:1: FURB105 [*] Called `print` with an empty string and a redundant separator, use `print(end="bar")` instead
|
4 | print("", sep=",")
5 | print("", end="bar")
6 | print("", sep=",", end="bar")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FURB105
7 | print(sep="")
8 | print("", sep="")
|
= help: Remove empty string positional argument and redundant separator

ℹ Suggested fix
3 3 | print("")
4 4 | print("", sep=",")
5 5 | print("", end="bar")
6 |-print("", sep=",", end="bar")
6 |+print(end="bar")
7 7 | print(sep="")
8 8 | print("", sep="")
9 9 | print("", "", sep="")

FURB105.py:8:1: FURB105 [*] Called `print` with an empty string and a redundant separator, use `print()` instead
|
6 | print("", sep=",", end="bar")
7 | print(sep="")
8 | print("", sep="")
| ^^^^^^^^^^^^^^^^^ FURB105
9 | print("", "", sep="")
10 | print("", "", sep="", end="")
|
= help: Remove empty string positional argument and redundant separator

ℹ Suggested fix
5 5 | print("", end="bar")
6 6 | print("", sep=",", end="bar")
7 7 | print(sep="")
8 |-print("", sep="")
8 |+print()
9 9 | print("", "", sep="")
10 10 | print("", "", sep="", end="")
11 11 | print("", "", sep="", end="bar")

FURB105.py:9:1: FURB105 [*] Called `print` with an empty string and a redundant separator, use `print()` instead
|
7 | print(sep="")
8 | print("", sep="")
9 | print("", "", sep="")
| ^^^^^^^^^^^^^^^^^^^^^ FURB105
10 | print("", "", sep="", end="")
11 | print("", "", sep="", end="bar")
|
= help: Remove empty string positional argument and redundant separator

ℹ Suggested fix
6 6 | print("", sep=",", end="bar")
7 7 | print(sep="")
8 8 | print("", sep="")
9 |-print("", "", sep="")
9 |+print()
10 10 | print("", "", sep="", end="")
11 11 | print("", "", sep="", end="bar")
12 12 | print("", sep="", end="bar")

FURB105.py:10:1: FURB105 [*] Called `print` with an empty string and a redundant separator, use `print(end="")` instead
|
8 | print("", sep="")
9 | print("", "", sep="")
10 | print("", "", sep="", end="")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FURB105
11 | print("", "", sep="", end="bar")
12 | print("", sep="", end="bar")
|
= help: Remove empty string positional argument and redundant separator

ℹ Suggested fix
7 7 | print(sep="")
8 8 | print("", sep="")
9 9 | print("", "", sep="")
10 |-print("", "", sep="", end="")
10 |+print(end="")
11 11 | print("", "", sep="", end="bar")
12 12 | print("", sep="", end="bar")
13 13 | print(sep="", end="bar")

FURB105.py:11:1: FURB105 [*] Called `print` with an empty string and a redundant separator, use `print(end="bar")` instead
|
9 | print("", "", sep="")
10 | print("", "", sep="", end="")
11 | print("", "", sep="", end="bar")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FURB105
12 | print("", sep="", end="bar")
13 | print(sep="", end="bar")
|
= help: Remove empty string positional argument and redundant separator

ℹ Suggested fix
8 8 | print("", sep="")
9 9 | print("", "", sep="")
10 10 | print("", "", sep="", end="")
11 |-print("", "", sep="", end="bar")
11 |+print(end="bar")
12 12 | print("", sep="", end="bar")
13 13 | print(sep="", end="bar")
14 14 | print("", "foo", sep="")

FURB105.py:12:1: FURB105 [*] Called `print` with an empty string and a redundant separator, use `print(end="bar")` instead
|
10 | print("", "", sep="", end="")
11 | print("", "", sep="", end="bar")
12 | print("", sep="", end="bar")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FURB105
13 | print(sep="", end="bar")
14 | print("", "foo", sep="")
|
= help: Remove empty string positional argument and redundant separator

ℹ Suggested fix
9 9 | print("", "", sep="")
10 10 | print("", "", sep="", end="")
11 11 | print("", "", sep="", end="bar")
12 |-print("", sep="", end="bar")
12 |+print(end="bar")
13 13 | print(sep="", end="bar")
14 14 | print("", "foo", sep="")
15 15 | print("foo", "", sep="")

FURB105.py:14:1: FURB105 [*] Called `print` with an empty string and a redundant separator, use `print("foo")` instead
|
12 | print("", sep="", end="bar")
13 | print(sep="", end="bar")
14 | print("", "foo", sep="")
| ^^^^^^^^^^^^^^^^^^^^^^^^ FURB105
15 | print("foo", "", sep="")
|
= help: Remove empty string positional argument and redundant separator

ℹ Suggested fix
11 11 | print("", "", sep="", end="bar")
12 12 | print("", sep="", end="bar")
13 13 | print(sep="", end="bar")
14 |-print("", "foo", sep="")
14 |+print("foo")
15 15 | print("foo", "", sep="")
16 16 |
17 17 | # OK.

FURB105.py:15:1: FURB105 [*] Called `print` with an empty string and a redundant separator, use `print("foo")` instead
|
13 | print(sep="", end="bar")
14 | print("", "foo", sep="")
15 | print("foo", "", sep="")
| ^^^^^^^^^^^^^^^^^^^^^^^^ FURB105
16 |
17 | # OK.
|
= help: Remove empty string positional argument and redundant separator

ℹ Suggested fix
12 12 | print("", sep="", end="bar")
13 13 | print(sep="", end="bar")
14 14 | print("", "foo", sep="")
15 |-print("foo", "", sep="")
15 |+print("foo")
16 16 |
17 17 | # OK.
18 18 |


0 comments on commit 81fa094

Please sign in to comment.