mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:26 +00:00
[ruff
] Trigger RUF037
for empty string and byte strings (#18862)
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-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
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-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
This commit is contained in:
parent
e474f36473
commit
ca7933804e
4 changed files with 145 additions and 1 deletions
|
@ -1,5 +1,5 @@
|
|||
from collections import deque
|
||||
import collections
|
||||
from collections import deque
|
||||
|
||||
|
||||
def f():
|
||||
|
@ -91,3 +91,14 @@ def f():
|
|||
|
||||
def f():
|
||||
deque([], iterable=[])
|
||||
|
||||
# https://github.com/astral-sh/ruff/issues/18854
|
||||
deque("")
|
||||
deque(b"")
|
||||
deque(f"")
|
||||
deque(f"" "")
|
||||
deque(f"" f"")
|
||||
deque("abc") # OK
|
||||
deque(b"abc") # OK
|
||||
deque(f"" "a") # OK
|
||||
deque(f"{x}" "") # OK
|
||||
|
|
|
@ -100,6 +100,9 @@ pub(crate) fn unnecessary_literal_within_deque_call(checker: &Checker, deque: &a
|
|||
})
|
||||
&& call.arguments.is_empty()
|
||||
}
|
||||
Expr::StringLiteral(string) => string.value.is_empty(),
|
||||
Expr::BytesLiteral(bytes) => bytes.value.is_empty(),
|
||||
Expr::FString(fstring) => fstring.value.is_empty_literal(),
|
||||
_ => false,
|
||||
};
|
||||
if !is_empty_literal {
|
||||
|
|
|
@ -246,6 +246,8 @@ RUF037.py:93:5: RUF037 [*] Unnecessary empty iterable within a deque call
|
|||
92 | def f():
|
||||
93 | deque([], iterable=[])
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ RUF037
|
||||
94 |
|
||||
95 | # https://github.com/astral-sh/ruff/issues/18854
|
||||
|
|
||||
= help: Replace with `deque()`
|
||||
|
||||
|
@ -255,3 +257,110 @@ RUF037.py:93:5: RUF037 [*] Unnecessary empty iterable within a deque call
|
|||
92 92 | def f():
|
||||
93 |- deque([], iterable=[])
|
||||
93 |+ deque([])
|
||||
94 94 |
|
||||
95 95 | # https://github.com/astral-sh/ruff/issues/18854
|
||||
96 96 | deque("")
|
||||
|
||||
RUF037.py:96:1: RUF037 [*] Unnecessary empty iterable within a deque call
|
||||
|
|
||||
95 | # https://github.com/astral-sh/ruff/issues/18854
|
||||
96 | deque("")
|
||||
| ^^^^^^^^^ RUF037
|
||||
97 | deque(b"")
|
||||
98 | deque(f"")
|
||||
|
|
||||
= help: Replace with `deque()`
|
||||
|
||||
ℹ Safe fix
|
||||
93 93 | deque([], iterable=[])
|
||||
94 94 |
|
||||
95 95 | # https://github.com/astral-sh/ruff/issues/18854
|
||||
96 |-deque("")
|
||||
96 |+deque()
|
||||
97 97 | deque(b"")
|
||||
98 98 | deque(f"")
|
||||
99 99 | deque(f"" "")
|
||||
|
||||
RUF037.py:97:1: RUF037 [*] Unnecessary empty iterable within a deque call
|
||||
|
|
||||
95 | # https://github.com/astral-sh/ruff/issues/18854
|
||||
96 | deque("")
|
||||
97 | deque(b"")
|
||||
| ^^^^^^^^^^ RUF037
|
||||
98 | deque(f"")
|
||||
99 | deque(f"" "")
|
||||
|
|
||||
= help: Replace with `deque()`
|
||||
|
||||
ℹ Safe fix
|
||||
94 94 |
|
||||
95 95 | # https://github.com/astral-sh/ruff/issues/18854
|
||||
96 96 | deque("")
|
||||
97 |-deque(b"")
|
||||
97 |+deque()
|
||||
98 98 | deque(f"")
|
||||
99 99 | deque(f"" "")
|
||||
100 100 | deque(f"" f"")
|
||||
|
||||
RUF037.py:98:1: RUF037 [*] Unnecessary empty iterable within a deque call
|
||||
|
|
||||
96 | deque("")
|
||||
97 | deque(b"")
|
||||
98 | deque(f"")
|
||||
| ^^^^^^^^^^ RUF037
|
||||
99 | deque(f"" "")
|
||||
100 | deque(f"" f"")
|
||||
|
|
||||
= help: Replace with `deque()`
|
||||
|
||||
ℹ Safe fix
|
||||
95 95 | # https://github.com/astral-sh/ruff/issues/18854
|
||||
96 96 | deque("")
|
||||
97 97 | deque(b"")
|
||||
98 |-deque(f"")
|
||||
98 |+deque()
|
||||
99 99 | deque(f"" "")
|
||||
100 100 | deque(f"" f"")
|
||||
101 101 | deque("abc") # OK
|
||||
|
||||
RUF037.py:99:1: RUF037 [*] Unnecessary empty iterable within a deque call
|
||||
|
|
||||
97 | deque(b"")
|
||||
98 | deque(f"")
|
||||
99 | deque(f"" "")
|
||||
| ^^^^^^^^^^^^^ RUF037
|
||||
100 | deque(f"" f"")
|
||||
101 | deque("abc") # OK
|
||||
|
|
||||
= help: Replace with `deque()`
|
||||
|
||||
ℹ Safe fix
|
||||
96 96 | deque("")
|
||||
97 97 | deque(b"")
|
||||
98 98 | deque(f"")
|
||||
99 |-deque(f"" "")
|
||||
99 |+deque()
|
||||
100 100 | deque(f"" f"")
|
||||
101 101 | deque("abc") # OK
|
||||
102 102 | deque(b"abc") # OK
|
||||
|
||||
RUF037.py:100:1: RUF037 [*] Unnecessary empty iterable within a deque call
|
||||
|
|
||||
98 | deque(f"")
|
||||
99 | deque(f"" "")
|
||||
100 | deque(f"" f"")
|
||||
| ^^^^^^^^^^^^^^ RUF037
|
||||
101 | deque("abc") # OK
|
||||
102 | deque(b"abc") # OK
|
||||
|
|
||||
= help: Replace with `deque()`
|
||||
|
||||
ℹ Safe fix
|
||||
97 97 | deque(b"")
|
||||
98 98 | deque(f"")
|
||||
99 99 | deque(f"" "")
|
||||
100 |-deque(f"" f"")
|
||||
100 |+deque()
|
||||
101 101 | deque("abc") # OK
|
||||
102 102 | deque(b"abc") # OK
|
||||
103 103 | deque(f"" "a") # OK
|
||||
|
|
|
@ -504,6 +504,20 @@ impl FStringValue {
|
|||
pub fn elements(&self) -> impl Iterator<Item = &InterpolatedStringElement> {
|
||||
self.f_strings().flat_map(|fstring| fstring.elements.iter())
|
||||
}
|
||||
|
||||
/// Returns `true` if the node represents an empty f-string literal.
|
||||
///
|
||||
/// Noteh that a [`FStringValue`] node will always have >= 1 [`FStringPart`]s inside it.
|
||||
/// This method checks whether the value of the concatenated parts is equal to the empty
|
||||
/// f-string, not whether the f-string has 0 parts inside it.
|
||||
pub fn is_empty_literal(&self) -> bool {
|
||||
match &self.inner {
|
||||
FStringValueInner::Single(fstring_part) => fstring_part.is_empty_literal(),
|
||||
FStringValueInner::Concatenated(fstring_parts) => {
|
||||
fstring_parts.iter().all(FStringPart::is_empty_literal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoIterator for &'a FStringValue {
|
||||
|
@ -550,6 +564,13 @@ impl FStringPart {
|
|||
Self::FString(f_string) => f_string.flags.quote_style(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_empty_literal(&self) -> bool {
|
||||
match &self {
|
||||
FStringPart::Literal(string_literal) => string_literal.value.is_empty(),
|
||||
FStringPart::FString(f_string) => f_string.elements.is_empty(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Ranged for FStringPart {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue