mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
[flake8-comprehensions
] autofix C414 and C417 + bugfix (#2693)
Closes https://github.com/charliermarsh/ruff/issues/2262 and closes https://github.com/charliermarsh/ruff/issues/2423 Fixes bug where some cases generated duplicated violations (see https://github.com/charliermarsh/ruff/pull/2732#issuecomment-1426397842)
This commit is contained in:
parent
c53f91d943
commit
0123425be1
11 changed files with 780 additions and 101 deletions
40
docs/rules/unnecessary-double-cast-or-process.md
Normal file
40
docs/rules/unnecessary-double-cast-or-process.md
Normal file
|
@ -0,0 +1,40 @@
|
|||
# unnecessary-double-cast-or-process (C414)
|
||||
|
||||
Derived from the **flake8-comprehensions** linter.
|
||||
|
||||
Autofix is always available.
|
||||
|
||||
## What it does
|
||||
Checks for unnecessary `list`, `reversed`, `set`, `sorted`, and `tuple`
|
||||
call within `list`, `set`, `sorted`, and `tuple` calls.
|
||||
|
||||
## Why is this bad?
|
||||
It's unnecessary to double-cast or double-process iterables by wrapping
|
||||
the listed functions within an additional `list`, `set`, `sorted`, or
|
||||
`tuple` call. Doing so is redundant and can be confusing for readers.
|
||||
|
||||
## Examples
|
||||
```python
|
||||
list(tuple(iterable))
|
||||
```
|
||||
|
||||
Use instead:
|
||||
```python
|
||||
list(iterable)
|
||||
```
|
||||
|
||||
This rule applies to a variety of functions, including `list`, `reversed`,
|
||||
`set`, `sorted`, and `tuple`. For example:
|
||||
* Instead of `list(list(iterable))`, use `list(iterable)`.
|
||||
* Instead of `list(tuple(iterable))`, use `list(iterable)`.
|
||||
* Instead of `tuple(list(iterable))`, use `tuple(iterable)`.
|
||||
* Instead of `tuple(tuple(iterable))`, use `tuple(iterable)`.
|
||||
* Instead of `set(set(iterable))`, use `set(iterable)`.
|
||||
* Instead of `set(list(iterable))`, use `set(iterable)`.
|
||||
* Instead of `set(tuple(iterable))`, use `set(iterable)`.
|
||||
* Instead of `set(sorted(iterable))`, use `set(iterable)`.
|
||||
* Instead of `set(reversed(iterable))`, use `set(iterable)`.
|
||||
* Instead of `sorted(list(iterable))`, use `sorted(iterable)`.
|
||||
* Instead of `sorted(tuple(iterable))`, use `sorted(iterable)`.
|
||||
* Instead of `sorted(sorted(iterable))`, use `sorted(iterable)`.
|
||||
* Instead of `sorted(reversed(iterable))`, use `sorted(iterable)`.
|
32
docs/rules/unnecessary-map.md
Normal file
32
docs/rules/unnecessary-map.md
Normal file
|
@ -0,0 +1,32 @@
|
|||
# unnecessary-map (C417)
|
||||
|
||||
Derived from the **flake8-comprehensions** linter.
|
||||
|
||||
Autofix is sometimes available.
|
||||
|
||||
## What it does
|
||||
Checks for unnecessary `map` calls with `lambda` functions.
|
||||
|
||||
## Why is this bad?
|
||||
Using `map(func, iterable)` when `func` is a `lambda` is slower than
|
||||
using a generator expression or a comprehension, as the latter approach
|
||||
avoids the function call overhead, in addition to being more readable.
|
||||
|
||||
## Examples
|
||||
```python
|
||||
map(lambda x: x + 1, iterable)
|
||||
```
|
||||
|
||||
Use instead:
|
||||
```python
|
||||
(x + 1 for x in iterable)
|
||||
```
|
||||
|
||||
This rule also applies to `map` calls within `list`, `set`, and `dict`
|
||||
calls. For example:
|
||||
* Instead of `list(map(lambda num: num * 2, nums))`, use
|
||||
`[num * 2 for num in nums]`.
|
||||
* Instead of `set(map(lambda num: num % 2 == 0, nums))`, use
|
||||
`{num % 2 == 0 for num in nums}`.
|
||||
* Instead of `dict(map(lambda v: (v, v ** 2), values))`, use
|
||||
`{v: v ** 2 for v in values}`.
|
Loading…
Add table
Add a link
Reference in a new issue