[perflint] Add PERF401 and PERF402 rules (#5298)

## Summary

Adds `PERF401` and `PERF402` mirroring `W8401` and `W8402` from
https://github.com/tonybaloney/perflint

Implementation is not super smart but should be at parity with upstream
implementation judging by:
c07391c176/perflint/comprehension_checker.py (L42-L73)

It essentially checks:

- If the body of a for-loop is just one statement
- If that statement is an `if` and the if-statement contains a call to
`append()` we flag `PERF401` and suggest a list comprehension
- If that statement is a plain call to `append()` or `insert()` we flag
`PERF402` and suggest `list()` or `list.copy()`

I've set the violation to only flag the first append call in a long
`if-else` statement for `PERF401`. Happy to change this to some other
location or make it multiple violations if that makes more sense.

## Test Plan

Fixtures were added with the relevant scenarios for both rules

## Issue Links

Refers: https://github.com/astral-sh/ruff/issues/4789
This commit is contained in:
qdegraaf 2023-07-03 06:03:09 +02:00 committed by GitHub
parent 0bff4ed4d3
commit 93b2bd7184
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 248 additions and 11 deletions

View file

@ -45,9 +45,7 @@ def format_confusables_rs(raw_data: dict[str, list[int]]) -> str:
for i in range(0, len(items), 2):
flattened_items.add((items[i], items[i + 1]))
tuples = []
for left, right in sorted(flattened_items):
tuples.append(f" {left}u32 => {right},\n")
tuples = [f" {left}u32 => {right},\n" for left, right in sorted(flattened_items)]
print(f"{len(tuples)} confusable tuples.")