[ruff] Preserve parentheses around deque in fix for unnecessary-empty-iterable-within-deque-call (RUF037) (#18598)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

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)()