[`ruff`] Preserve parentheses around `deque` in fix for `unnecessary-empty-iterable-within-deque-call` (`RUF037`) (#18598)

Closes #18552
This commit is contained in:
Dylan 2025-06-09 15:38:39 -05:00 committed by GitHub
parent 79006dfb52
commit caf885c20a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 2 deletions

View File

@ -56,3 +56,6 @@ def f():
def f():
queue = deque() # Ok
def f():
x = 0 or(deque)([])

View File

@ -1,4 +1,5 @@
use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::parenthesize::parenthesized_range;
use ruff_python_ast::{self as ast, Expr};
use ruff_text_size::Ranged;
@ -107,7 +108,15 @@ fn fix_unnecessary_literal_in_deque(
deque: &ast::ExprCall,
maxlen: Option<&Expr>,
) -> Fix {
let deque_name = checker.locator().slice(deque.func.range());
let deque_name = checker.locator().slice(
parenthesized_range(
deque.func.as_ref().into(),
deque.into(),
checker.comment_ranges(),
checker.source(),
)
.unwrap_or(deque.func.range()),
);
let deque_str = match maxlen {
Some(maxlen) => {
let len_str = checker.locator().slice(maxlen);

View File

@ -1,6 +1,5 @@
---
source: crates/ruff_linter/src/rules/ruff/mod.rs
snapshot_kind: text
---
RUF037.py:6:13: RUF037 [*] Unnecessary empty iterable within a deque call
|
@ -127,3 +126,18 @@ RUF037.py:30:13: RUF037 [*] Unnecessary empty iterable within a deque call
31 31 |
32 32 |
33 33 | def f():
RUF037.py:61:13: RUF037 [*] Unnecessary empty iterable within a deque call
|
60 | def f():
61 | x = 0 or(deque)([])
| ^^^^^^^^^^^ RUF037
|
= help: Replace with `deque()`
Safe fix
58 58 | queue = deque() # Ok
59 59 |
60 60 | def f():
61 |- x = 0 or(deque)([])
61 |+ x = 0 or(deque)()