-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
318 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from collections import deque | ||
import collections | ||
|
||
|
||
def f(): | ||
queue = collections.deque([]) # RUF025 | ||
|
||
|
||
def f(): | ||
queue = collections.deque([], maxlen=10) # RUF025 | ||
|
||
|
||
def f(): | ||
queue = deque([]) # RUF025 | ||
|
||
|
||
def f(): | ||
queue = deque(()) # RUF025 | ||
|
||
|
||
def f(): | ||
queue = deque({}) # RUF025 | ||
|
||
|
||
def f(): | ||
queue = deque(set()) # RUF025 | ||
|
||
|
||
def f(): | ||
queue = collections.deque([], maxlen=10) # RUF025 | ||
|
||
|
||
def f(): | ||
class FakeDeque: | ||
pass | ||
|
||
deque = FakeDeque | ||
queue = deque([]) # Ok | ||
|
||
|
||
def f(): | ||
class FakeSet: | ||
pass | ||
|
||
set = FakeSet | ||
queue = deque(set()) # Ok | ||
|
||
|
||
def f(): | ||
queue = deque([1, 2]) # Ok | ||
|
||
|
||
def f(): | ||
queue = deque([1, 2], maxlen=10) # Ok | ||
|
||
|
||
def f(): | ||
queue = deque() # Ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
crates/ruff_linter/src/rules/ruff/rules/unnecessary_literal_within_deque_call.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use crate::checkers::ast::Checker; | ||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; | ||
use ruff_macros::{derive_message_formats, ViolationMetadata}; | ||
use ruff_python_ast::{self as ast, Expr}; | ||
use ruff_text_size::Ranged; | ||
|
||
/// ## What it does | ||
/// Checks for usages of `collections.deque` that have an empty iterable as the first argument. | ||
/// | ||
/// ## Why is this bad? | ||
/// It's unnecessary to use an empty literal as a deque's iterable, since this is already the default behavior. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ```python | ||
/// from collections import deque | ||
/// | ||
/// queue = deque(set()) | ||
/// queue = deque([], 10) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// | ||
/// ```python | ||
/// from collections import deque | ||
/// | ||
/// queue = deque() | ||
/// queue = deque(maxlen=10) | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: `collections.deque`](https://docs.python.org/3/library/collections.html#collections.deque) | ||
#[derive(ViolationMetadata)] | ||
pub(crate) struct UnnecessaryEmptyIterableWithinDequeCall { | ||
has_maxlen: bool, | ||
} | ||
|
||
impl AlwaysFixableViolation for UnnecessaryEmptyIterableWithinDequeCall { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
"Unnecessary empty iterable within a deque call".to_string() | ||
} | ||
|
||
fn fix_title(&self) -> String { | ||
let title = if self.has_maxlen { | ||
"Replace with `deque(maxlen=...)`" | ||
} else { | ||
"Replace with `deque()`" | ||
}; | ||
title.to_string() | ||
} | ||
} | ||
|
||
/// RUF025 | ||
pub(crate) fn unnecessary_literal_within_deque_call(checker: &mut Checker, deque: &ast::ExprCall) { | ||
let ast::ExprCall { | ||
func, arguments, .. | ||
} = deque; | ||
|
||
let Some(qualified) = checker.semantic().resolve_qualified_name(func) else { | ||
return; | ||
}; | ||
if !matches!(qualified.segments(), ["collections", "deque"]) || arguments.len() > 2 { | ||
return; | ||
} | ||
|
||
let Some(iterable) = arguments.find_argument_value("iterable", 0) else { | ||
return; | ||
}; | ||
|
||
let maxlen = arguments.find_argument_value("maxlen", 1); | ||
|
||
let is_empty_literal = match iterable { | ||
Expr::Dict(dict) => dict.is_empty(), | ||
Expr::List(list) => list.is_empty(), | ||
Expr::Tuple(tuple) => tuple.is_empty(), | ||
Expr::Call(call) => { | ||
checker | ||
.semantic() | ||
.resolve_builtin_symbol(&call.func) | ||
// other lints should handle empty list/dict/tuple calls, | ||
// but this means that the lint still applies before those are fixed | ||
.is_some_and(|name| { | ||
name == "set" || name == "list" || name == "dict" || name == "tuple" | ||
}) | ||
&& call.arguments.is_empty() | ||
} | ||
_ => false, | ||
}; | ||
if !is_empty_literal { | ||
return; | ||
} | ||
|
||
let mut diagnostic = Diagnostic::new( | ||
UnnecessaryEmptyIterableWithinDequeCall { | ||
has_maxlen: maxlen.is_some(), | ||
}, | ||
deque.range, | ||
); | ||
|
||
diagnostic.set_fix(fix_unnecessary_literal_in_deque(checker, deque, maxlen)); | ||
|
||
checker.diagnostics.push(diagnostic); | ||
} | ||
|
||
fn fix_unnecessary_literal_in_deque( | ||
checker: &Checker, | ||
deque: &ast::ExprCall, | ||
maxlen: Option<&Expr>, | ||
) -> Fix { | ||
let deque_name = checker.locator().slice(deque.func.range()); | ||
let deque_str = match maxlen { | ||
Some(maxlen) => { | ||
let len_str = checker.locator().slice(maxlen); | ||
format!("{deque_name}(maxlen={len_str})") | ||
} | ||
None => format!("{deque_name}()"), | ||
}; | ||
Fix::safe_edit(Edit::range_replacement(deque_str, deque.range)) | ||
} |
129 changes: 129 additions & 0 deletions
129
...ff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF025_RUF025.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/ruff/mod.rs | ||
snapshot_kind: text | ||
--- | ||
RUF025.py:6:13: RUF025 [*] Unnecessary empty iterable within a deque call | ||
| | ||
5 | def f(): | ||
6 | queue = collections.deque([]) # RUF025 | ||
| ^^^^^^^^^^^^^^^^^^^^^ RUF025 | ||
| | ||
= help: Replace with `deque()` | ||
|
||
ℹ Safe fix | ||
3 3 | | ||
4 4 | | ||
5 5 | def f(): | ||
6 |- queue = collections.deque([]) # RUF025 | ||
6 |+ queue = collections.deque() # RUF025 | ||
7 7 | | ||
8 8 | | ||
9 9 | def f(): | ||
|
||
RUF025.py:10:13: RUF025 [*] Unnecessary empty iterable within a deque call | ||
| | ||
9 | def f(): | ||
10 | queue = collections.deque([], maxlen=10) # RUF025 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF025 | ||
| | ||
= help: Replace with `deque(maxlen=...)` | ||
|
||
ℹ Safe fix | ||
7 7 | | ||
8 8 | | ||
9 9 | def f(): | ||
10 |- queue = collections.deque([], maxlen=10) # RUF025 | ||
10 |+ queue = collections.deque(maxlen=10) # RUF025 | ||
11 11 | | ||
12 12 | | ||
13 13 | def f(): | ||
|
||
RUF025.py:14:13: RUF025 [*] Unnecessary empty iterable within a deque call | ||
| | ||
13 | def f(): | ||
14 | queue = deque([]) # RUF025 | ||
| ^^^^^^^^^ RUF025 | ||
| | ||
= help: Replace with `deque()` | ||
|
||
ℹ Safe fix | ||
11 11 | | ||
12 12 | | ||
13 13 | def f(): | ||
14 |- queue = deque([]) # RUF025 | ||
14 |+ queue = deque() # RUF025 | ||
15 15 | | ||
16 16 | | ||
17 17 | def f(): | ||
|
||
RUF025.py:18:13: RUF025 [*] Unnecessary empty iterable within a deque call | ||
| | ||
17 | def f(): | ||
18 | queue = deque(()) # RUF025 | ||
| ^^^^^^^^^ RUF025 | ||
| | ||
= help: Replace with `deque()` | ||
|
||
ℹ Safe fix | ||
15 15 | | ||
16 16 | | ||
17 17 | def f(): | ||
18 |- queue = deque(()) # RUF025 | ||
18 |+ queue = deque() # RUF025 | ||
19 19 | | ||
20 20 | | ||
21 21 | def f(): | ||
|
||
RUF025.py:22:13: RUF025 [*] Unnecessary empty iterable within a deque call | ||
| | ||
21 | def f(): | ||
22 | queue = deque({}) # RUF025 | ||
| ^^^^^^^^^ RUF025 | ||
| | ||
= help: Replace with `deque()` | ||
|
||
ℹ Safe fix | ||
19 19 | | ||
20 20 | | ||
21 21 | def f(): | ||
22 |- queue = deque({}) # RUF025 | ||
22 |+ queue = deque() # RUF025 | ||
23 23 | | ||
24 24 | | ||
25 25 | def f(): | ||
|
||
RUF025.py:26:13: RUF025 [*] Unnecessary empty iterable within a deque call | ||
| | ||
25 | def f(): | ||
26 | queue = deque(set()) # RUF025 | ||
| ^^^^^^^^^^^^ RUF025 | ||
| | ||
= help: Replace with `deque()` | ||
|
||
ℹ Safe fix | ||
23 23 | | ||
24 24 | | ||
25 25 | def f(): | ||
26 |- queue = deque(set()) # RUF025 | ||
26 |+ queue = deque() # RUF025 | ||
27 27 | | ||
28 28 | | ||
29 29 | def f(): | ||
|
||
RUF025.py:30:13: RUF025 [*] Unnecessary empty iterable within a deque call | ||
| | ||
29 | def f(): | ||
30 | queue = collections.deque([], maxlen=10) # RUF025 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF025 | ||
| | ||
= help: Replace with `deque(maxlen=...)` | ||
|
||
ℹ Safe fix | ||
27 27 | | ||
28 28 | | ||
29 29 | def f(): | ||
30 |- queue = collections.deque([], maxlen=10) # RUF025 | ||
30 |+ queue = collections.deque(maxlen=10) # RUF025 | ||
31 31 | | ||
32 32 | | ||
33 33 | def f(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.