From 81fa094964946d082c44953c22443502536e69cc Mon Sep 17 00:00:00 2001 From: Tom Kuson Date: Sat, 23 Sep 2023 15:25:30 +0100 Subject: [PATCH] perf: defer checking sep kwarg --- .../rules/refurb/rules/print_empty_string.rs | 25 +- ...refurb__tests__FURB105_FURB105.py.snap.new | 233 ++++++++++++++++++ 2 files changed, 248 insertions(+), 10 deletions(-) create mode 100644 crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB105_FURB105.py.snap.new diff --git a/crates/ruff_linter/src/rules/refurb/rules/print_empty_string.rs b/crates/ruff_linter/src/rules/refurb/rules/print_empty_string.rs index 8a4c8a29e8e068..a1b56a565e5739 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/print_empty_string.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/print_empty_string.rs @@ -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 diff --git a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB105_FURB105.py.snap.new b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB105_FURB105.py.snap.new new file mode 100644 index 00000000000000..7e9f04e3b4cf28 --- /dev/null +++ b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB105_FURB105.py.snap.new @@ -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 | + +