Skip to content

Commit

Permalink
Avoid false-positive print separator diagnostic with starred argument
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Oct 19, 2023
1 parent 0e58433 commit 61ee4f5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Expand Up @@ -31,3 +31,4 @@
print("foo", "", sep=",")
print("foo", "", "bar", "", sep=",")
print("", "", **kwargs)
print(*args, sep=",")
12 changes: 10 additions & 2 deletions crates/ruff_linter/src/rules/refurb/rules/print_empty_string.rs
Expand Up @@ -7,13 +7,17 @@ use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;

/// ## What it does
/// Checks for `print` calls with an empty string as the only positional
/// argument.
/// Checks for `print` calls with unnecessary empty strings as positional
/// arguments and unnecessary `sep` keyword arguments.
///
/// ## Why is this bad?
/// Prefer calling `print` without any positional arguments, which is
/// equivalent and more concise.
///
/// Similarly, when printing one or fewer items, the `sep` keyword argument,
/// (used to define the string that separates the `print` arguments) can be
/// omitted.
///
/// ## Example
/// ```python
/// print("")
Expand Down Expand Up @@ -95,6 +99,10 @@ pub(crate) fn print_empty_string(checker: &mut Checker, call: &ast::ExprCall) {
checker.diagnostics.push(diagnostic);
}

[arg] if arg.is_starred_expr() => {
// If there's a starred argument, we can't remove the empty string.
}

// Ex) `print(sep="\t")` or `print(obj, sep="\t")`
[] | [_] => {
// If there's a `sep` argument, remove it, regardless of what it is.
Expand Down

0 comments on commit 61ee4f5

Please sign in to comment.