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 16af959
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions crates/ruff_linter/src/rules/refurb/rules/print_empty_string.rs
Original file line number Diff line number Diff line change
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 comments on commit 16af959

Please sign in to comment.