Compare commits

...

29 Commits

Author SHA1 Message Date
Charlie Marsh
06e426f509 Bump version to 0.0.250 (#3095) 2023-02-21 15:20:46 -05:00
Carlos Gonçalves
6eb014b3b2 feat(B032): add b032 flake8_bugbear (#3085) 2023-02-21 19:53:29 +00:00
Charlie Marsh
d9fd78d907 Ignore setters in flake8-boolean-trap (#3092) 2023-02-21 19:31:00 +00:00
Charlie Marsh
37df07d2e0 Re-add compatibility to README (#3091) 2023-02-21 18:57:47 +00:00
Charlie Marsh
d5c65b5f1b Add support for structural pattern matching (#3047) 2023-02-21 18:52:10 +00:00
Charlie Marsh
cdc4e86158 Add support for TryStar (#3089) 2023-02-21 13:42:20 -05:00
Charlie Marsh
50ec6d3b0f Use LibCST to fix chained assertions (#3087) 2023-02-21 13:10:31 -05:00
Charlie Marsh
a6eb60cdd5 Enable function2 test (#3083) 2023-02-21 04:37:50 +00:00
Charlie Marsh
90c04b9cff Enable tupleassign test (#3080) 2023-02-21 00:42:23 +00:00
Charlie Marsh
b701cca779 Enable some already-passing Black tests (#3079) 2023-02-21 00:10:35 +00:00
Charlie Marsh
ce8953442d Add support for trailing colons in slice expressions (#3077) 2023-02-20 23:24:32 +00:00
Charlie Marsh
7d4e513a82 Omit while-True loops from implicit return enforcement (#3076) 2023-02-20 18:22:28 -05:00
Charlie Marsh
35f7f7b66d Avoid boolean-trap rules for positional-only builtin calls (#3075) 2023-02-20 23:08:18 +00:00
Charlie Marsh
6e02405bd6 Add StmtKind::Try; fix trailing newlines (#3074) 2023-02-20 22:55:32 +00:00
Carlos Gonçalves
b657468346 feat(B029): Add B029 from flake8-bugbear (#3068) 2023-02-20 15:57:13 -05:00
Micha Reiser
f72ed255e5 chore: Use LF on all platforms (#3005)
I worked on #2993 and ran into issues that the formatter tests are failing on Windows because `writeln!` emits `\n` as line terminator on all platforms, but `git` on Windows converted the line endings in the snapshots to `\r\n`.

I then tried to replicate the issue on my Windows machine and was surprised that all linter snapshot tests are failing on my machine. I figured out after some time that it is due to my global git config keeping the input line endings rather than converting to `\r\n`. 

Luckily, I've been made aware of #2033 which introduced an "override" for the `assert_yaml_snapshot` macro that normalizes new lines, by splitting the formatted string using the platform-specific newline character. This is a clever approach and gives nice diffs for multiline fixes but makes assumptions about the setup contributors use and requires special care whenever we use line endings inside of tests. 

I recommend that we remove the special new line handling and use `.gitattributes` to enforce the use of `LF` on all platforms [guide](https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings). This gives us platform agnostic tests without having to worry about line endings in our tests or different git configurations.

## Note

It may be necessary for Windows contributors to run the following command to update the line endings of their files

```bash
git rm --cached -r .
git reset --hard
```
2023-02-20 20:13:37 +00:00
Charlie Marsh
7e9dea0027 Change contributing suggestion (#3067) 2023-02-20 20:05:38 +00:00
Colin Delahunty
9545958ad8 [flake8-simplify]: Implement manual-dict-lookup (#2767) 2023-02-20 20:00:59 +00:00
Colin Delahunty
41faa335d1 [tryceratops]: Verbose Log Messages (#3036) 2023-02-20 18:21:04 +00:00
Charlie Marsh
4cfa350112 Bump version to 0.0.249 (#3063) 2023-02-20 13:11:29 -05:00
Charlie Marsh
41f163fc8d Avoid assert() to assert statement conversion in expressions (#3062) 2023-02-20 17:49:22 +00:00
Charlie Marsh
d21dd994e6 Increase expected size of FormatElement (#3049) 2023-02-20 12:47:35 -05:00
Josh Karpel
6f5a6b8c8b Do not autofix E731 in class bodies (#3050) 2023-02-20 12:38:42 -05:00
Jeong YunWon
35606d7b05 clean up to fix nightly clippy warnings and dedents (#3057) 2023-02-20 09:33:47 -05:00
Matthew Lloyd
3ad257cfea Add PDM to "Who's Using Ruff?" (#3048) 2023-02-20 03:58:22 +00:00
Charlie Marsh
b39f960cd1 Relax constraints on pep8-naming module validation (#3043) 2023-02-19 17:34:23 -05:00
Charlie Marsh
c297d46899 Remove unused AsFormat trait for Option<T> (#3041)
We should re-add this, but it's currently unused and doesn't compile under 1.66.0.

See: #3039.
2023-02-19 20:19:35 +00:00
Jonathan Plasse
d6a100028c Update docs and pre-commit after #3006 (#3038) 2023-02-19 14:23:01 -05:00
Jonathan Plasse
35d4e03f2a Fix ruff_dev regex workspace dependency (#3037) 2023-02-19 18:02:08 +00:00
431 changed files with 4739 additions and 4660 deletions

6
.gitattributes vendored Normal file
View File

@@ -0,0 +1,6 @@
* text=auto eol=lf
crates/ruff/resources/test/fixtures/isort/line_ending_crlf.py text eol=crlf
crates/ruff/resources/test/fixtures/pycodestyle/W605_1.py text eol=crlf
ruff.schema.json linguist-generated=true text=auto eol=lf

View File

@@ -30,7 +30,7 @@ repos:
pass_filenames: false
- id: ruff
name: ruff
entry: cargo run -- --no-cache --fix
entry: cargo run -p ruff_cli -- check --no-cache --force-exclude --fix --exit-non-zero-on-fix
language: rust
types_or: [python, pyi]
require_serial: true

View File

@@ -29,13 +29,10 @@ If you're looking for a place to start, we recommend implementing a new lint rul
pattern-match against the examples in the existing codebase. Many lint rules are inspired by
existing Python plugins, which can be used as a reference implementation.
As a concrete example: consider taking on one of the rules from the [`tryceratops`](https://github.com/charliermarsh/ruff/issues/2056)
plugin, and looking to the originating [Python source](https://github.com/guilatrova/tryceratops)
As a concrete example: consider taking on one of the rules from the [`flake8-pyi`](https://github.com/charliermarsh/ruff/issues/848)
plugin, and looking to the originating [Python source](https://github.com/PyCQA/flake8-pyi)
for guidance.
Alternatively, we've started work on the [`flake8-pyi`](https://github.com/charliermarsh/ruff/issues/848)
plugin (see the [Python source](https://github.com/PyCQA/flake8-pyi)) -- another good place to start.
### Prerequisites
Ruff is written in Rust. You'll need to install the
@@ -52,7 +49,7 @@ cargo install cargo-insta
After cloning the repository, run Ruff locally with:
```shell
cargo run check /path/to/file.py --no-cache
cargo run -p ruff_cli -- check /path/to/file.py --no-cache
```
Prior to opening a pull request, ensure that your code has been auto-formatted,
@@ -135,7 +132,7 @@ contain a variety of violations and non-violations designed to evaluate and demo
of your lint rule.
Run `cargo dev generate-all` to generate the code for your new fixture. Then run Ruff
locally with (e.g.) `cargo run check crates/ruff/resources/test/fixtures/pycodestyle/E402.py --no-cache --select E402`.
locally with (e.g.) `cargo run -p ruff_cli -- check crates/ruff/resources/test/fixtures/pycodestyle/E402.py --no-cache --select E402`.
Once you're satisfied with the output, codify the behavior as a snapshot test by adding a new
`test_case` macro in the relevant `crates/ruff/src/[linter]/mod.rs` file. Then, run `cargo test --all`.

14
Cargo.lock generated
View File

@@ -753,7 +753,7 @@ checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "flake8-to-ruff"
version = "0.0.248"
version = "0.0.250"
dependencies = [
"anyhow",
"clap 4.1.6",
@@ -1927,7 +1927,7 @@ dependencies = [
[[package]]
name = "ruff"
version = "0.0.248"
version = "0.0.250"
dependencies = [
"anyhow",
"bisection",
@@ -1983,7 +1983,7 @@ dependencies = [
[[package]]
name = "ruff_cli"
version = "0.0.248"
version = "0.0.250"
dependencies = [
"annotate-snippets 0.9.1",
"anyhow",
@@ -2146,7 +2146,7 @@ dependencies = [
[[package]]
name = "rustpython-ast"
version = "0.2.0"
source = "git+https://github.com/RustPython/RustPython.git?rev=61b48f108982d865524f86624a9d5bc2ae3bccef#61b48f108982d865524f86624a9d5bc2ae3bccef"
source = "git+https://github.com/RustPython/RustPython.git?rev=ddf497623ae56d21aa4166ff1c0725a7db67e955#ddf497623ae56d21aa4166ff1c0725a7db67e955"
dependencies = [
"num-bigint",
"rustpython-compiler-core",
@@ -2155,7 +2155,7 @@ dependencies = [
[[package]]
name = "rustpython-common"
version = "0.2.0"
source = "git+https://github.com/RustPython/RustPython.git?rev=61b48f108982d865524f86624a9d5bc2ae3bccef#61b48f108982d865524f86624a9d5bc2ae3bccef"
source = "git+https://github.com/RustPython/RustPython.git?rev=ddf497623ae56d21aa4166ff1c0725a7db67e955#ddf497623ae56d21aa4166ff1c0725a7db67e955"
dependencies = [
"ascii",
"bitflags",
@@ -2180,7 +2180,7 @@ dependencies = [
[[package]]
name = "rustpython-compiler-core"
version = "0.2.0"
source = "git+https://github.com/RustPython/RustPython.git?rev=61b48f108982d865524f86624a9d5bc2ae3bccef#61b48f108982d865524f86624a9d5bc2ae3bccef"
source = "git+https://github.com/RustPython/RustPython.git?rev=ddf497623ae56d21aa4166ff1c0725a7db67e955#ddf497623ae56d21aa4166ff1c0725a7db67e955"
dependencies = [
"bincode",
"bitflags",
@@ -2197,7 +2197,7 @@ dependencies = [
[[package]]
name = "rustpython-parser"
version = "0.2.0"
source = "git+https://github.com/RustPython/RustPython.git?rev=61b48f108982d865524f86624a9d5bc2ae3bccef#61b48f108982d865524f86624a9d5bc2ae3bccef"
source = "git+https://github.com/RustPython/RustPython.git?rev=ddf497623ae56d21aa4166ff1c0725a7db67e955#ddf497623ae56d21aa4166ff1c0725a7db67e955"
dependencies = [
"ahash",
"anyhow",

View File

@@ -13,8 +13,8 @@ libcst = { git = "https://github.com/charliermarsh/LibCST", rev = "f2f0b7a487a87
once_cell = { version = "1.16.0" }
regex = { version = "1.6.0" }
rustc-hash = { version = "1.1.0" }
rustpython-common = { git = "https://github.com/RustPython/RustPython.git", rev = "61b48f108982d865524f86624a9d5bc2ae3bccef" }
rustpython-parser = { features = ["lalrpop"], git = "https://github.com/RustPython/RustPython.git", rev = "61b48f108982d865524f86624a9d5bc2ae3bccef" }
rustpython-common = { git = "https://github.com/RustPython/RustPython.git", rev = "ddf497623ae56d21aa4166ff1c0725a7db67e955" }
rustpython-parser = { features = ["lalrpop"], git = "https://github.com/RustPython/RustPython.git", rev = "ddf497623ae56d21aa4166ff1c0725a7db67e955" }
schemars = { version = "0.8.11" }
serde = { version = "1.0.147", features = ["derive"] }
serde_json = { version = "1.0.87" }

View File

@@ -27,6 +27,7 @@ An extremely fast Python linter, written in Rust.
* ⚡️ 10-100x faster than existing linters
* 🐍 Installable via `pip`
* 🛠️ `pyproject.toml` support
* 🤝 Python 3.11 compatibility
* 📦 Built-in caching, to avoid re-analyzing unchanged files
* 🔧 Autofix support, for automatic error correction (e.g., automatically remove unused imports)
* 📏 Over [400 built-in rules](https://beta.ruff.rs/docs/rules/) (and growing)
@@ -167,7 +168,7 @@ Ruff also works with [pre-commit](https://pre-commit.com):
```yaml
- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: 'v0.0.248'
rev: 'v0.0.250'
hooks:
- id: ruff
```
@@ -177,7 +178,7 @@ Or, to enable autofix:
```yaml
- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: 'v0.0.248'
rev: 'v0.0.250'
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
@@ -720,6 +721,7 @@ Ruff is used in a number of major open-source projects, including:
* [Dagger](https://github.com/dagger/dagger)
* [Sphinx](https://github.com/sphinx-doc/sphinx)
* [Hatch](https://github.com/pypa/hatch)
* [PDM](https://github.com/pdm-project/pdm)
* [Jupyter](https://github.com/jupyter-server/jupyter_server)
* [Great Expectations](https://github.com/great-expectations/great_expectations)
* [ONNX](https://github.com/onnx/onnx)

View File

@@ -1,6 +1,6 @@
[package]
name = "flake8-to-ruff"
version = "0.0.248"
version = "0.0.250"
edition = { workspace = true }
rust-version = { workspace = true }

View File

@@ -1,6 +1,6 @@
[package]
name = "ruff"
version = "0.0.248"
version = "0.0.250"
authors = ["Charlie Marsh <charlie.r.marsh@gmail.com>"]
edition = { workspace = true }
rust-version = { workspace = true }

View File

@@ -57,6 +57,8 @@ dict.fromkeys(("world",), True)
{}.deploy(True, False)
getattr(someobj, attrname, False)
mylist.index(True)
int(True)
str(int(False))
class Registry:
@@ -66,3 +68,11 @@ class Registry:
# FBT001: Boolean positional arg in function definition
def __setitem__(self, switch: Switch, value: bool) -> None:
self._switches[switch.value] = value
@foo.setter
def foo(self, value: bool) -> None:
pass
# FBT001: Boolean positional arg in function definition
def foo(self, value: bool) -> None:
pass

View File

@@ -105,3 +105,25 @@ while True:
pass
finally:
break # warning
while True:
try:
pass
finally:
match *0, 1, *2:
case 0,:
y = 0
case 0, *x:
break # warning
while True:
try:
pass
finally:
match *0, 1, *2:
case 0,:
y = 0
case 0, *x:
pass # no warning

View File

@@ -0,0 +1,14 @@
"""
Should emit:
B029 - on lines 8 and 13
"""
try:
pass
except ():
pass
try:
pass
except () as e:
pass

View File

@@ -0,0 +1,29 @@
"""
Should emit:
B032 - on lines 9, 10, 12, 13, 16-19
"""
# Flag these
dct = {"a": 1}
dct["b"]: 2
dct.b: 2
dct["b"]: "test"
dct.b: "test"
test = "test"
dct["b"]: test
dct["b"]: test.lower()
dct.b: test
dct.b: test.lower()
# Do not flag below
typed_dct: dict[str, int] = {"a": 1}
typed_dct["b"] = 2
typed_dct.b = 2
class TestClass:
def test_self(self):
self.test: int

View File

@@ -62,3 +62,11 @@ except Exception as e:
raise RuntimeError("boom!")
else:
raise RuntimeError("bang!")
try:
...
except Exception as e:
match 0:
case 0:
raise RuntimeError("boom!")

View File

@@ -17,6 +17,12 @@ class Test(unittest.TestCase):
self.assertTrue(**{"expr": expr, "msg": msg}) # Error, unfixable
self.assertTrue(msg=msg, expr=expr, unexpected_arg=False) # Error, unfixable
self.assertTrue(msg=msg) # Error, unfixable
(
self.assertIsNotNone(value) # Error, unfixable
if expect_condition
else self.assertIsNone(value) # Error, unfixable
)
return self.assertEqual(True, False) # Error, unfixable
def test_assert_false(self):
self.assertFalse(True) # Error

View File

@@ -9,6 +9,7 @@ def test_ok():
assert something, "something message"
assert something or something_else and something_third, "another message"
def test_error():
assert something and something_else
assert something and something_else and something_third
@@ -17,13 +18,24 @@ def test_error():
assert not something and something_else
assert not (something or something_else)
assert not (something or something_else or something_third)
assert something and something_else == """error
message
"""
# recursive case
assert not (a or not (b or c))
assert not (a or not (b and c)) # note that we only reduce once here
assert not (a or not (b or c)) # note that we only reduce once here
assert not (a or not (b and c))
# detected, but no autofix for messages
assert something and something_else, "error message"
assert not (something or something_else and something_third), "with message"
# detected, but no autofix for mixed conditions (e.g. `a or b and c`)
assert not (something or something_else and something_third)
# detected, but no autofix for parenthesized conditions
assert (
something
and something_else
== """error
message
"""
)

View File

@@ -77,7 +77,7 @@ def x(y):
# last line in while loop
def x(y):
while True:
while i > 0:
if y > 0:
return 1
y += 1
@@ -259,3 +259,19 @@ def nested(values):
for value in values:
print(value)
def while_true():
while True:
if y > 0:
return 1
y += 1
# match
def x(y):
match y:
case 0:
return 1
case 1:
print() # error

View File

@@ -0,0 +1,76 @@
# Errors
a = "hello"
# SIM116
if a == "foo":
return "bar"
elif a == "bar":
return "baz"
elif a == "boo":
return "ooh"
else:
return 42
# SIM116
if a == 1:
return (1, 2, 3)
elif a == 2:
return (4, 5, 6)
elif a == 3:
return (7, 8, 9)
else:
return (10, 11, 12)
# SIM116
if a == 1:
return (1, 2, 3)
elif a == 2:
return (4, 5, 6)
elif a == 3:
return (7, 8, 9)
# SIM116
if a == "hello 'sir'":
return (1, 2, 3)
elif a == 'goodbye "mam"':
return (4, 5, 6)
elif a == """Fairwell 'mister'""":
return (7, 8, 9)
else:
return (10, 11, 12)
# SIM116
if a == b"one":
return 1
elif a == b"two":
return 2
elif a == b"three":
return 3
# SIM116
if a == "hello 'sir'":
return ("hello'", 'hi"', 3)
elif a == 'goodbye "mam"':
return (4, 5, 6)
elif a == """Fairwell 'mister'""":
return (7, 8, 9)
else:
return (10, 11, 12)
# OK
if a == "foo":
return "bar"
elif a == "bar":
return baz()
elif a == "boo":
return "ooh"
else:
return 42
# OK
if a == b"one":
return 1
elif b == b"two":
return 2
elif a == b"three":
return 3

View File

@@ -1,2 +1,2 @@
from long_module_name import member_one, member_two, member_three, member_four, member_five
from long_module_name import member_one, member_two, member_three, member_four, member_five

View File

@@ -54,3 +54,6 @@ def f(): ...
class C: ...; x = 1
#: E701:1:8 E702:1:13
class C: ...; ...
#: E701:2:12
match *0, 1, *2:
case 0,: y = 0

View File

@@ -9,6 +9,9 @@ while False:
f = lambda: (yield 1)
#: E731
f = lambda: (yield from g())
#: E731
class F:
f = lambda x: 2 * x
f = object()
f.method = lambda: "Method"

View File

@@ -1,35 +1,35 @@
#: W605:1:10
regex = '\.png$'
#: W605:2:1
regex = '''
\.png$
'''
#: W605:2:6
f(
'\_'
)
#: W605:4:6
"""
multi-line
literal
with \_ somewhere
in the middle
"""
#: Okay
regex = r'\.png$'
regex = '\\.png$'
regex = r'''
\.png$
'''
regex = r'''
\\.png$
'''
s = '\\'
regex = '\w' # noqa
regex = '''
\w
''' # noqa
#: W605:1:10
regex = '\.png$'
#: W605:2:1
regex = '''
\.png$
'''
#: W605:2:6
f(
'\_'
)
#: W605:4:6
"""
multi-line
literal
with \_ somewhere
in the middle
"""
#: Okay
regex = r'\.png$'
regex = '\\.png$'
regex = r'''
\.png$
'''
regex = r'''
\\.png$
'''
s = '\\'
regex = '\w' # noqa
regex = '''
\w
''' # noqa

View File

@@ -85,3 +85,10 @@ else:
CustomInt: TypeAlias = "np.int8 | np.int16"
# Test: match statements.
match *0, 1, *2:
case 0,:
import x
import y

View File

@@ -1,7 +1,7 @@
"""
Test that shadowing a global with a class attribute does not produce a
warning.
"""
Test that shadowing a global with a class attribute does not produce a
warning.
"""
import fu

View File

@@ -71,7 +71,7 @@ def f():
def connect():
return None, None
with (connect() as (connection, cursor)):
with connect() as (connection, cursor):
cursor.execute("SELECT * FROM users")
@@ -94,3 +94,28 @@ def f():
(exponential := (exponential * base_multiplier) % 3): i + 1 for i in range(2)
}
return hash_map
def f(x: int):
msg1 = "Hello, world!"
msg2 = "Hello, world!"
msg3 = "Hello, world!"
match x:
case 1:
print(msg1)
case 2:
print(msg2)
def f(x: int):
import enum
Foo = enum.Enum("Foo", "A B")
Bar = enum.Enum("Bar", "A B")
Baz = enum.Enum("Baz", "A B")
match x:
case (Foo.A):
print("A")
case [Bar.A, *_]:
print("A")

View File

@@ -20,6 +20,24 @@ def bad():
logger.exception("something failed")
def bad():
try:
a = process()
if not a:
raise MyException(a)
raise MyException(a)
try:
b = process()
if not b:
raise MyException(b)
except* Exception:
logger.exception("something failed")
except* Exception:
logger.exception("something failed")
def good():
try:
a = process() # This throws the exception now

View File

@@ -0,0 +1,64 @@
# Errors
def main_function():
try:
process()
handle()
finish()
except Exception as ex:
logger.exception(f"Found an error: {ex}") # TRY401
def main_function():
try:
process()
handle()
finish()
except ValueError as bad:
if True is False:
for i in range(10):
logger.exception(f"Found an error: {bad} {good}") # TRY401
except IndexError as bad:
logger.exception(f"Found an error: {bad} {bad}") # TRY401
except Exception as bad:
logger.exception(f"Found an error: {bad}") # TRY401
logger.exception(f"Found an error: {bad}") # TRY401
if True:
logger.exception(f"Found an error: {bad}") # TRY401
import logging
logger = logging.getLogger(__name__)
def func_fstr():
try:
...
except Exception as ex:
logger.exception(f"Logging an error: {ex}") # TRY401
def func_concat():
try:
...
except Exception as ex:
logger.exception("Logging an error: " + str(ex)) # TRY401
def func_comma():
try:
...
except Exception as ex:
logger.exception("Logging an error:", ex) # TRY401
# OK
def main_function():
try:
process()
handle()
finish()
except Exception as ex:
logger.exception(f"Found an error: {er}")
logger.exception(f"Found an error: {ex.field}")

View File

@@ -8,14 +8,14 @@ behaviors.
Running from the repo root should pick up and enforce the appropriate settings for each package:
```console
∴ cargo run resources/test/project/
resources/test/project/examples/.dotfiles/script.py:1:1: I001 Import block is un-sorted or un-formatted
resources/test/project/examples/.dotfiles/script.py:1:8: F401 `numpy` imported but unused
resources/test/project/examples/.dotfiles/script.py:2:17: F401 `app.app_file` imported but unused
resources/test/project/examples/docs/docs/file.py:1:1: I001 Import block is un-sorted or un-formatted
resources/test/project/examples/docs/docs/file.py:8:5: F841 Local variable `x` is assigned to but never used
resources/test/project/project/file.py:1:8: F401 `os` imported but unused
resources/test/project/project/import_file.py:1:1: I001 Import block is un-sorted or un-formatted
∴ cargo run -p ruff_cli -- check crates/ruff/resources/test/project/
crates/ruff/resources/test/project/examples/.dotfiles/script.py:1:1: I001 Import block is un-sorted or un-formatted
crates/ruff/resources/test/project/examples/.dotfiles/script.py:1:8: F401 `numpy` imported but unused
crates/ruff/resources/test/project/examples/.dotfiles/script.py:2:17: F401 `app.app_file` imported but unused
crates/ruff/resources/test/project/examples/docs/docs/file.py:1:1: I001 Import block is un-sorted or un-formatted
crates/ruff/resources/test/project/examples/docs/docs/file.py:8:5: F841 Local variable `x` is assigned to but never used
crates/ruff/resources/test/project/project/file.py:1:8: F401 `os` imported but unused
crates/ruff/resources/test/project/project/import_file.py:1:1: I001 Import block is un-sorted or un-formatted
Found 7 errors.
7 potentially fixable with the --fix option.
```
@@ -23,7 +23,7 @@ Found 7 errors.
Running from the project directory itself should exhibit the same behavior:
```console
∴ (cd resources/test/project/ && cargo run .)
∴ (cd crates/ruff/resources/test/project/ && cargo run -p ruff_cli -- check .)
examples/.dotfiles/script.py:1:1: I001 Import block is un-sorted or un-formatted
examples/.dotfiles/script.py:1:8: F401 `numpy` imported but unused
examples/.dotfiles/script.py:2:17: F401 `app.app_file` imported but unused
@@ -39,7 +39,7 @@ Running from the sub-package directory should exhibit the same behavior, but omi
files:
```console
∴ (cd resources/test/project/examples/docs && cargo run .)
∴ (cd crates/ruff/resources/test/project/examples/docs && cargo run -p ruff_cli -- check .)
docs/file.py:1:1: I001 Import block is un-sorted or un-formatted
docs/file.py:8:5: F841 Local variable `x` is assigned to but never used
Found 2 errors.
@@ -50,16 +50,16 @@ Found 2 errors.
file paths from the current working directory:
```console
∴ (cargo run -- --config=resources/test/project/pyproject.toml resources/test/project/)
resources/test/project/examples/.dotfiles/script.py:1:8: F401 `numpy` imported but unused
resources/test/project/examples/.dotfiles/script.py:2:17: F401 `app.app_file` imported but unused
resources/test/project/examples/docs/docs/concepts/file.py:1:8: F401 `os` imported but unused
resources/test/project/examples/docs/docs/file.py:1:1: I001 Import block is un-sorted or un-formatted
resources/test/project/examples/docs/docs/file.py:1:8: F401 `os` imported but unused
resources/test/project/examples/docs/docs/file.py:3:8: F401 `numpy` imported but unused
resources/test/project/examples/docs/docs/file.py:4:27: F401 `docs.concepts.file` imported but unused
resources/test/project/examples/excluded/script.py:1:8: F401 `os` imported but unused
resources/test/project/project/file.py:1:8: F401 `os` imported but unused
∴ (cargo run -p ruff_cli -- check --config=crates/ruff/resources/test/project/pyproject.toml crates/ruff/resources/test/project/)
crates/ruff/resources/test/project/examples/.dotfiles/script.py:1:8: F401 `numpy` imported but unused
crates/ruff/resources/test/project/examples/.dotfiles/script.py:2:17: F401 `app.app_file` imported but unused
crates/ruff/resources/test/project/examples/docs/docs/concepts/file.py:1:8: F401 `os` imported but unused
crates/ruff/resources/test/project/examples/docs/docs/file.py:1:1: I001 Import block is un-sorted or un-formatted
crates/ruff/resources/test/project/examples/docs/docs/file.py:1:8: F401 `os` imported but unused
crates/ruff/resources/test/project/examples/docs/docs/file.py:3:8: F401 `numpy` imported but unused
crates/ruff/resources/test/project/examples/docs/docs/file.py:4:27: F401 `docs.concepts.file` imported but unused
crates/ruff/resources/test/project/examples/excluded/script.py:1:8: F401 `os` imported but unused
crates/ruff/resources/test/project/project/file.py:1:8: F401 `os` imported but unused
Found 9 errors.
9 potentially fixable with the --fix option.
```
@@ -68,7 +68,7 @@ Running from a parent directory should "ignore" the `exclude` (hence, `concepts/
included in the output):
```console
∴ (cd resources/test/project/examples && cargo run -- --config=docs/ruff.toml .)
∴ (cd crates/ruff/resources/test/project/examples && cargo run -p ruff_cli -- check --config=docs/ruff.toml .)
docs/docs/concepts/file.py:5:5: F841 Local variable `x` is assigned to but never used
docs/docs/file.py:1:1: I001 Import block is un-sorted or un-formatted
docs/docs/file.py:8:5: F841 Local variable `x` is assigned to but never used
@@ -80,8 +80,8 @@ Found 4 errors.
Passing an excluded directory directly should report errors in the contained files:
```console
∴ cargo run resources/test/project/examples/excluded/
resources/test/project/examples/excluded/script.py:1:8: F401 `os` imported but unused
∴ cargo run -p ruff_cli -- check crates/ruff/resources/test/project/examples/excluded/
crates/ruff/resources/test/project/examples/excluded/script.py:1:8: F401 `os` imported but unused
Found 1 error.
1 potentially fixable with the --fix option.
```
@@ -89,8 +89,8 @@ Found 1 error.
Unless we `--force-exclude`:
```console
∴ cargo run resources/test/project/examples/excluded/ --force-exclude
∴ cargo run -p ruff_cli -- check crates/ruff/resources/test/project/examples/excluded/ --force-exclude
warning: No Python files found under the given path(s)
∴ cargo run resources/test/project/examples/excluded/script.py --force-exclude
∴ cargo run -p ruff_cli -- check crates/ruff/resources/test/project/examples/excluded/script.py --force-exclude
warning: No Python files found under the given path(s)
```

View File

@@ -1,18 +0,0 @@
/// Platform-independent snapshot assertion
#[macro_export]
macro_rules! assert_yaml_snapshot {
( $($args: expr),+) => {
let line_sep = if cfg!(windows) { "\r\n" } else { "\n" };
// adjust snapshot file for platform
let mut settings = insta::Settings::clone_current();
settings.add_redaction("[].fix.content", insta::dynamic_redaction(move |value, _path| {
insta::internals::Content::Seq(
value.as_str().unwrap().split(line_sep).map(|line| line.into()).collect()
)
}));
settings.bind(|| {
insta::assert_yaml_snapshot!($($args),+);
});
};
}

View File

@@ -59,6 +59,12 @@ fn alternatives<'a>(stmt: &'a RefEquality<'a, Stmt>) -> Vec<Vec<RefEquality<'a,
handlers,
orelse,
..
}
| StmtKind::TryStar {
body,
handlers,
orelse,
..
} => vec![body.iter().chain(orelse.iter()).map(RefEquality).collect()]
.into_iter()
.chain(handlers.iter().map(|handler| {

View File

@@ -4,8 +4,8 @@
use num_bigint::BigInt;
use rustpython_parser::ast::{
Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler,
ExcepthandlerKind, Expr, ExprContext, ExprKind, Keyword, Operator, Stmt, StmtKind, Unaryop,
Withitem,
ExcepthandlerKind, Expr, ExprContext, ExprKind, Keyword, MatchCase, Operator, Pattern,
PatternKind, Stmt, StmtKind, Unaryop, Withitem,
};
#[derive(Debug, PartialEq, Eq, Hash)]
@@ -157,6 +157,110 @@ impl<'a> From<&'a Withitem> for ComparableWithitem<'a> {
}
}
#[allow(clippy::enum_variant_names)]
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum ComparablePattern<'a> {
MatchValue {
value: ComparableExpr<'a>,
},
MatchSingleton {
value: ComparableConstant<'a>,
},
MatchSequence {
patterns: Vec<ComparablePattern<'a>>,
},
MatchMapping {
keys: Vec<ComparableExpr<'a>>,
patterns: Vec<ComparablePattern<'a>>,
rest: Option<&'a str>,
},
MatchClass {
cls: ComparableExpr<'a>,
patterns: Vec<ComparablePattern<'a>>,
kwd_attrs: Vec<&'a str>,
kwd_patterns: Vec<ComparablePattern<'a>>,
},
MatchStar {
name: Option<&'a str>,
},
MatchAs {
pattern: Option<Box<ComparablePattern<'a>>>,
name: Option<&'a str>,
},
MatchOr {
patterns: Vec<ComparablePattern<'a>>,
},
}
impl<'a> From<&'a Pattern> for ComparablePattern<'a> {
fn from(pattern: &'a Pattern) -> Self {
match &pattern.node {
PatternKind::MatchValue { value } => Self::MatchValue {
value: value.into(),
},
PatternKind::MatchSingleton { value } => Self::MatchSingleton {
value: value.into(),
},
PatternKind::MatchSequence { patterns } => Self::MatchSequence {
patterns: patterns.iter().map(Into::into).collect(),
},
PatternKind::MatchMapping {
keys,
patterns,
rest,
} => Self::MatchMapping {
keys: keys.iter().map(Into::into).collect(),
patterns: patterns.iter().map(Into::into).collect(),
rest: rest.as_deref(),
},
PatternKind::MatchClass {
cls,
patterns,
kwd_attrs,
kwd_patterns,
} => Self::MatchClass {
cls: cls.into(),
patterns: patterns.iter().map(Into::into).collect(),
kwd_attrs: kwd_attrs.iter().map(String::as_str).collect(),
kwd_patterns: kwd_patterns.iter().map(Into::into).collect(),
},
PatternKind::MatchStar { name } => Self::MatchStar {
name: name.as_deref(),
},
PatternKind::MatchAs { pattern, name } => Self::MatchAs {
pattern: pattern.as_ref().map(Into::into),
name: name.as_deref(),
},
PatternKind::MatchOr { patterns } => Self::MatchOr {
patterns: patterns.iter().map(Into::into).collect(),
},
}
}
}
impl<'a> From<&'a Box<Pattern>> for Box<ComparablePattern<'a>> {
fn from(pattern: &'a Box<Pattern>) -> Self {
Box::new((&**pattern).into())
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ComparableMatchCase<'a> {
pub pattern: ComparablePattern<'a>,
pub guard: Option<ComparableExpr<'a>>,
pub body: Vec<ComparableStmt<'a>>,
}
impl<'a> From<&'a MatchCase> for ComparableMatchCase<'a> {
fn from(match_case: &'a MatchCase) -> Self {
Self {
pattern: (&match_case.pattern).into(),
guard: match_case.guard.as_ref().map(Into::into),
body: match_case.body.iter().map(Into::into).collect(),
}
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum ComparableConstant<'a> {
None,
@@ -644,6 +748,10 @@ pub enum ComparableStmt<'a> {
body: Vec<ComparableStmt<'a>>,
type_comment: Option<&'a str>,
},
Match {
subject: ComparableExpr<'a>,
cases: Vec<ComparableMatchCase<'a>>,
},
Raise {
exc: Option<ComparableExpr<'a>>,
cause: Option<ComparableExpr<'a>>,
@@ -654,6 +762,12 @@ pub enum ComparableStmt<'a> {
orelse: Vec<ComparableStmt<'a>>,
finalbody: Vec<ComparableStmt<'a>>,
},
TryStar {
body: Vec<ComparableStmt<'a>>,
handlers: Vec<ComparableExcepthandler<'a>>,
orelse: Vec<ComparableStmt<'a>>,
finalbody: Vec<ComparableStmt<'a>>,
},
Assert {
test: ComparableExpr<'a>,
msg: Option<ComparableExpr<'a>>,
@@ -811,7 +925,10 @@ impl<'a> From<&'a Stmt> for ComparableStmt<'a> {
body: body.iter().map(Into::into).collect(),
type_comment: type_comment.as_ref().map(String::as_str),
},
StmtKind::Match { .. } => unreachable!("StmtKind::Match is not supported"),
StmtKind::Match { subject, cases } => Self::Match {
subject: subject.into(),
cases: cases.iter().map(Into::into).collect(),
},
StmtKind::Raise { exc, cause } => Self::Raise {
exc: exc.as_ref().map(Into::into),
cause: cause.as_ref().map(Into::into),
@@ -827,6 +944,17 @@ impl<'a> From<&'a Stmt> for ComparableStmt<'a> {
orelse: orelse.iter().map(Into::into).collect(),
finalbody: finalbody.iter().map(Into::into).collect(),
},
StmtKind::TryStar {
body,
handlers,
orelse,
finalbody,
} => Self::TryStar {
body: body.iter().map(Into::into).collect(),
handlers: handlers.iter().map(Into::into).collect(),
orelse: orelse.iter().map(Into::into).collect(),
finalbody: finalbody.iter().map(Into::into).collect(),
},
StmtKind::Assert { test, msg } => Self::Assert {
test: test.into(),
msg: msg.as_ref().map(Into::into),

View File

@@ -7,7 +7,7 @@ use regex::Regex;
use rustc_hash::{FxHashMap, FxHashSet};
use rustpython_parser::ast::{
Arguments, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprKind, Keyword, KeywordData,
Located, Location, Stmt, StmtKind,
Located, Location, MatchCase, Pattern, PatternKind, Stmt, StmtKind,
};
use rustpython_parser::lexer;
use rustpython_parser::lexer::Tok;
@@ -249,6 +249,46 @@ where
}
}
pub fn any_over_pattern<F>(pattern: &Pattern, func: &F) -> bool
where
F: Fn(&Expr) -> bool,
{
match &pattern.node {
PatternKind::MatchValue { value } => any_over_expr(value, func),
PatternKind::MatchSingleton { .. } => false,
PatternKind::MatchSequence { patterns } => patterns
.iter()
.any(|pattern| any_over_pattern(pattern, func)),
PatternKind::MatchMapping { keys, patterns, .. } => {
keys.iter().any(|key| any_over_expr(key, func))
|| patterns
.iter()
.any(|pattern| any_over_pattern(pattern, func))
}
PatternKind::MatchClass {
cls,
patterns,
kwd_patterns,
..
} => {
any_over_expr(cls, func)
|| patterns
.iter()
.any(|pattern| any_over_pattern(pattern, func))
|| kwd_patterns
.iter()
.any(|pattern| any_over_pattern(pattern, func))
}
PatternKind::MatchStar { .. } => false,
PatternKind::MatchAs { pattern, .. } => pattern
.as_ref()
.map_or(false, |pattern| any_over_pattern(pattern, func)),
PatternKind::MatchOr { patterns } => patterns
.iter()
.any(|pattern| any_over_pattern(pattern, func)),
}
}
pub fn any_over_stmt<F>(stmt: &Stmt, func: &F) -> bool
where
F: Fn(&Expr) -> bool,
@@ -391,6 +431,12 @@ where
handlers,
orelse,
finalbody,
}
| StmtKind::TryStar {
body,
handlers,
orelse,
finalbody,
} => {
any_over_body(body, func)
|| handlers.iter().any(|handler| {
@@ -409,8 +455,21 @@ where
.as_ref()
.map_or(false, |value| any_over_expr(value, func))
}
// TODO(charlie): Handle match statements.
StmtKind::Match { .. } => false,
StmtKind::Match { subject, cases } => {
any_over_expr(subject, func)
|| cases.iter().any(|case| {
let MatchCase {
pattern,
guard,
body,
} = case;
any_over_pattern(pattern, func)
|| guard
.as_ref()
.map_or(false, |expr| any_over_expr(expr, func))
|| any_over_body(body, func)
})
}
StmtKind::Import { .. } => false,
StmtKind::ImportFrom { .. } => false,
StmtKind::Global { .. } => false,

View File

@@ -181,7 +181,10 @@ pub fn extract_globals(body: &[Stmt]) -> FxHashMap<&str, &Stmt> {
/// Check if a node is parent of a conditional branch.
pub fn on_conditional_branch<'a>(parents: &mut impl Iterator<Item = &'a Stmt>) -> bool {
parents.any(|parent| {
if matches!(parent.node, StmtKind::If { .. } | StmtKind::While { .. }) {
if matches!(
parent.node,
StmtKind::If { .. } | StmtKind::While { .. } | StmtKind::Match { .. }
) {
return true;
}
if let StmtKind::Expr { value } = &parent.node {
@@ -198,7 +201,10 @@ pub fn in_nested_block<'a>(mut parents: impl Iterator<Item = &'a Stmt>) -> bool
parents.any(|parent| {
matches!(
parent.node,
StmtKind::Try { .. } | StmtKind::If { .. } | StmtKind::With { .. }
StmtKind::Try { .. }
| StmtKind::TryStar { .. }
| StmtKind::If { .. }
| StmtKind::With { .. }
)
})
}
@@ -304,13 +310,14 @@ pub fn locate_cmpops(contents: &str) -> Vec<LocatedCmpop> {
ops.push(LocatedCmpop::new(start, end, Cmpop::In));
}
Tok::Is => {
if let Some((_, _, end)) =
let op = if let Some((_, _, end)) =
tok_iter.next_if(|(_, tok, _)| matches!(tok, Tok::Not))
{
ops.push(LocatedCmpop::new(start, end, Cmpop::IsNot));
LocatedCmpop::new(start, end, Cmpop::IsNot)
} else {
ops.push(LocatedCmpop::new(start, end, Cmpop::Is));
}
LocatedCmpop::new(start, end, Cmpop::Is)
};
ops.push(op);
}
Tok::NotEqual => {
ops.push(LocatedCmpop::new(start, end, Cmpop::NotEq));

View File

@@ -205,7 +205,6 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
visitor.visit_body(body);
}
StmtKind::Match { subject, cases } => {
// TODO(charlie): Handle `cases`.
visitor.visit_expr(subject);
for match_case in cases {
visitor.visit_match_case(match_case);
@@ -232,6 +231,19 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
visitor.visit_body(orelse);
visitor.visit_body(finalbody);
}
StmtKind::TryStar {
body,
handlers,
orelse,
finalbody,
} => {
visitor.visit_body(body);
for excepthandler in handlers {
visitor.visit_excepthandler(excepthandler);
}
visitor.visit_body(orelse);
visitor.visit_body(finalbody);
}
StmtKind::Assert { test, msg } => {
visitor.visit_expr(test);
if let Some(expr) = msg {

View File

@@ -53,6 +53,12 @@ fn is_lone_child(child: &Stmt, parent: &Stmt, deleted: &[&Stmt]) -> Result<bool>
handlers,
orelse,
finalbody,
}
| StmtKind::TryStar {
body,
handlers,
orelse,
finalbody,
} => {
if body.iter().contains(child) {
Ok(has_single_child(body, deleted))
@@ -74,6 +80,19 @@ fn is_lone_child(child: &Stmt, parent: &Stmt, deleted: &[&Stmt]) -> Result<bool>
bail!("Unable to find child in parent body")
}
}
StmtKind::Match { cases, .. } => {
if let Some(body) = cases.iter().find_map(|case| {
if case.body.iter().contains(child) {
Some(&case.body)
} else {
None
}
}) {
Ok(has_single_child(body, deleted))
} else {
bail!("Unable to find child in parent body")
}
}
_ => bail!("Unable to find child in parent body"),
}
}

View File

@@ -239,51 +239,51 @@ impl<'a> Checker<'a> {
'b: 'a,
{
let call_path = collect_call_path(value);
if let Some(head) = call_path.first() {
if let Some(binding) = self.find_binding(head) {
match &binding.kind {
BindingKind::Importation(.., name)
| BindingKind::SubmoduleImportation(name, ..) => {
return if name.starts_with('.') {
if let Some(module) = &self.module_path {
let mut source_path = from_relative_import(module, name);
source_path.extend(call_path.into_iter().skip(1));
Some(source_path)
} else {
None
}
} else {
let mut source_path: CallPath = name.split('.').collect();
source_path.extend(call_path.into_iter().skip(1));
Some(source_path)
};
let Some(head) = call_path.first() else {
return None;
};
let Some(binding) = self.find_binding(head) else {
return None;
};
match &binding.kind {
BindingKind::Importation(.., name) | BindingKind::SubmoduleImportation(name, ..) => {
if name.starts_with('.') {
if let Some(module) = &self.module_path {
let mut source_path = from_relative_import(module, name);
source_path.extend(call_path.into_iter().skip(1));
Some(source_path)
} else {
None
}
BindingKind::FromImportation(.., name) => {
return if name.starts_with('.') {
if let Some(module) = &self.module_path {
let mut source_path = from_relative_import(module, name);
source_path.extend(call_path.into_iter().skip(1));
Some(source_path)
} else {
None
}
} else {
let mut source_path: CallPath = name.split('.').collect();
source_path.extend(call_path.into_iter().skip(1));
Some(source_path)
};
}
BindingKind::Builtin => {
let mut source_path: CallPath = smallvec![];
source_path.push("");
source_path.extend(call_path);
return Some(source_path);
}
_ => {}
} else {
let mut source_path: CallPath = name.split('.').collect();
source_path.extend(call_path.into_iter().skip(1));
Some(source_path)
}
}
BindingKind::FromImportation(.., name) => {
if name.starts_with('.') {
if let Some(module) = &self.module_path {
let mut source_path = from_relative_import(module, name);
source_path.extend(call_path.into_iter().skip(1));
Some(source_path)
} else {
None
}
} else {
let mut source_path: CallPath = name.split('.').collect();
source_path.extend(call_path.into_iter().skip(1));
Some(source_path)
}
}
BindingKind::Builtin => {
let mut source_path: CallPath = smallvec![];
source_path.push("");
source_path.extend(call_path);
Some(source_path)
}
_ => None,
}
None
}
/// Return `true` if a `Rule` is disabled by a `noqa` directive.
@@ -706,7 +706,12 @@ where
.rules
.enabled(&Rule::BooleanPositionalArgInFunctionDefinition)
{
flake8_boolean_trap::rules::check_positional_boolean_in_def(self, name, args);
flake8_boolean_trap::rules::check_positional_boolean_in_def(
self,
name,
decorator_list,
args,
);
}
if self
@@ -715,7 +720,10 @@ where
.enabled(&Rule::BooleanDefaultValueInFunctionDefinition)
{
flake8_boolean_trap::rules::check_boolean_default_value_in_function_definition(
self, name, args,
self,
name,
decorator_list,
args,
);
}
@@ -1565,6 +1573,9 @@ where
if self.settings.rules.enabled(&Rule::NeedlessBool) {
flake8_simplify::rules::return_bool_condition_directly(self, stmt);
}
if self.settings.rules.enabled(&Rule::ManualDictLookup) {
flake8_simplify::rules::manual_dict_lookup(self, stmt, test, body, orelse);
}
if self.settings.rules.enabled(&Rule::UseTernaryOperator) {
flake8_simplify::rules::use_ternary_operator(
self,
@@ -1702,6 +1713,13 @@ where
orelse,
finalbody,
..
}
| StmtKind::TryStar {
body,
handlers,
orelse,
finalbody,
..
} => {
if self.settings.rules.enabled(&Rule::DefaultExceptNotLast) {
if let Some(diagnostic) =
@@ -1752,6 +1770,9 @@ where
if self.settings.rules.enabled(&Rule::VerboseRaise) {
tryceratops::rules::verbose_raise(self, handlers);
}
if self.settings.rules.enabled(&Rule::VerboseLogMessage) {
tryceratops::rules::verbose_log_message(self, handlers);
}
if self.settings.rules.enabled(&Rule::RaiseWithinTry) {
tryceratops::rules::raise_within_try(self, body);
}
@@ -1821,6 +1842,18 @@ where
pycodestyle::rules::lambda_assignment(self, target, value, stmt);
}
}
if self
.settings
.rules
.enabled(&Rule::UnintentionalTypeAnnotation)
{
flake8_bugbear::rules::unintentional_type_annotation(
self,
target,
value.as_deref(),
stmt,
);
}
}
StmtKind::Delete { .. } => {}
StmtKind::Expr { value, .. } => {
@@ -1986,6 +2019,12 @@ where
handlers,
orelse,
finalbody,
}
| StmtKind::TryStar {
body,
handlers,
orelse,
finalbody,
} => {
// TODO(charlie): The use of `smallvec` here leads to a lifetime issue.
let handler_names = extract_handler_names(handlers)
@@ -3702,6 +3741,9 @@ where
self.settings.flake8_bandit.check_typed_exception,
);
}
if self.settings.rules.enabled(&Rule::ExceptWithEmptyTuple) {
flake8_bugbear::rules::except_with_empty_tuple(self, excepthandler);
}
if self.settings.rules.enabled(&Rule::ReraiseNoCause) {
tryceratops::rules::reraise_no_cause(self, body);
}
@@ -4169,146 +4211,147 @@ impl<'a> Checker<'a> {
}
fn handle_node_load(&mut self, expr: &Expr) {
if let ExprKind::Name { id, .. } = &expr.node {
let scope_id = self.current_scope().id;
let ExprKind::Name { id, .. } = &expr.node else {
return;
};
let scope_id = self.current_scope().id;
let mut first_iter = true;
let mut in_generator = false;
let mut import_starred = false;
let mut first_iter = true;
let mut in_generator = false;
let mut import_starred = false;
for scope_index in self.scope_stack.iter().rev() {
let scope = &self.scopes[*scope_index];
if matches!(scope.kind, ScopeKind::Class(_)) {
if id == "__class__" {
return;
} else if !first_iter && !in_generator {
continue;
}
}
if let Some(index) = scope.bindings.get(&id.as_str()) {
// Mark the binding as used.
let context = self.execution_context();
self.bindings[*index].mark_used(scope_id, Range::from_located(expr), context);
if matches!(self.bindings[*index].kind, BindingKind::Annotation)
&& !self.in_deferred_string_type_definition
&& !self.in_deferred_type_definition
{
continue;
}
// If the name of the sub-importation is the same as an alias of another
// importation and the alias is used, that sub-importation should be
// marked as used too.
//
// This handles code like:
// import pyarrow as pa
// import pyarrow.csv
// print(pa.csv.read_csv("test.csv"))
match &self.bindings[*index].kind {
BindingKind::Importation(name, full_name)
| BindingKind::SubmoduleImportation(name, full_name) => {
let has_alias = full_name
.split('.')
.last()
.map(|segment| &segment != name)
.unwrap_or_default();
if has_alias {
// Mark the sub-importation as used.
if let Some(index) = scope.bindings.get(full_name) {
self.bindings[*index].mark_used(
scope_id,
Range::from_located(expr),
context,
);
}
}
}
BindingKind::FromImportation(name, full_name) => {
let has_alias = full_name
.split('.')
.last()
.map(|segment| &segment != name)
.unwrap_or_default();
if has_alias {
// Mark the sub-importation as used.
if let Some(index) = scope.bindings.get(full_name.as_str()) {
self.bindings[*index].mark_used(
scope_id,
Range::from_located(expr),
context,
);
}
}
}
_ => {}
}
for scope_index in self.scope_stack.iter().rev() {
let scope = &self.scopes[*scope_index];
if matches!(scope.kind, ScopeKind::Class(_)) {
if id == "__class__" {
return;
} else if !first_iter && !in_generator {
continue;
}
first_iter = false;
in_generator = matches!(scope.kind, ScopeKind::Generator);
import_starred = import_starred || scope.import_starred;
}
if import_starred {
if self.settings.rules.enabled(&Rule::ImportStarUsage) {
let mut from_list = vec![];
for scope_index in self.scope_stack.iter().rev() {
let scope = &self.scopes[*scope_index];
for binding in scope.bindings.values().map(|index| &self.bindings[*index]) {
if let BindingKind::StarImportation(level, module) = &binding.kind {
from_list.push(helpers::format_import_from(
level.as_ref(),
module.as_deref(),
));
if let Some(index) = scope.bindings.get(&id.as_str()) {
// Mark the binding as used.
let context = self.execution_context();
self.bindings[*index].mark_used(scope_id, Range::from_located(expr), context);
if matches!(self.bindings[*index].kind, BindingKind::Annotation)
&& !self.in_deferred_string_type_definition
&& !self.in_deferred_type_definition
{
continue;
}
// If the name of the sub-importation is the same as an alias of another
// importation and the alias is used, that sub-importation should be
// marked as used too.
//
// This handles code like:
// import pyarrow as pa
// import pyarrow.csv
// print(pa.csv.read_csv("test.csv"))
match &self.bindings[*index].kind {
BindingKind::Importation(name, full_name)
| BindingKind::SubmoduleImportation(name, full_name) => {
let has_alias = full_name
.split('.')
.last()
.map(|segment| &segment != name)
.unwrap_or_default();
if has_alias {
// Mark the sub-importation as used.
if let Some(index) = scope.bindings.get(full_name) {
self.bindings[*index].mark_used(
scope_id,
Range::from_located(expr),
context,
);
}
}
}
from_list.sort();
self.diagnostics.push(Diagnostic::new(
pyflakes::rules::ImportStarUsage {
name: id.to_string(),
sources: from_list,
},
Range::from_located(expr),
));
BindingKind::FromImportation(name, full_name) => {
let has_alias = full_name
.split('.')
.last()
.map(|segment| &segment != name)
.unwrap_or_default();
if has_alias {
// Mark the sub-importation as used.
if let Some(index) = scope.bindings.get(full_name.as_str()) {
self.bindings[*index].mark_used(
scope_id,
Range::from_located(expr),
context,
);
}
}
}
_ => {}
}
return;
}
if self.settings.rules.enabled(&Rule::UndefinedName) {
// Allow __path__.
if self.path.ends_with("__init__.py") && id == "__path__" {
return;
}
first_iter = false;
in_generator = matches!(scope.kind, ScopeKind::Generator);
import_starred = import_starred || scope.import_starred;
}
// Allow "__module__" and "__qualname__" in class scopes.
if (id == "__module__" || id == "__qualname__")
&& matches!(self.current_scope().kind, ScopeKind::Class(..))
{
return;
}
// Avoid flagging if NameError is handled.
if let Some(handler_names) = self.except_handlers.last() {
if handler_names
.iter()
.any(|call_path| call_path.as_slice() == ["NameError"])
{
return;
if import_starred {
if self.settings.rules.enabled(&Rule::ImportStarUsage) {
let mut from_list = vec![];
for scope_index in self.scope_stack.iter().rev() {
let scope = &self.scopes[*scope_index];
for binding in scope.bindings.values().map(|index| &self.bindings[*index]) {
if let BindingKind::StarImportation(level, module) = &binding.kind {
from_list.push(helpers::format_import_from(
level.as_ref(),
module.as_deref(),
));
}
}
}
from_list.sort();
self.diagnostics.push(Diagnostic::new(
pyflakes::rules::UndefinedName { name: id.clone() },
pyflakes::rules::ImportStarUsage {
name: id.to_string(),
sources: from_list,
},
Range::from_located(expr),
));
}
return;
}
if self.settings.rules.enabled(&Rule::UndefinedName) {
// Allow __path__.
if self.path.ends_with("__init__.py") && id == "__path__" {
return;
}
// Allow "__module__" and "__qualname__" in class scopes.
if (id == "__module__" || id == "__qualname__")
&& matches!(self.current_scope().kind, ScopeKind::Class(..))
{
return;
}
// Avoid flagging if NameError is handled.
if let Some(handler_names) = self.except_handlers.last() {
if handler_names
.iter()
.any(|call_path| call_path.as_slice() == ["NameError"])
{
return;
}
}
self.diagnostics.push(Diagnostic::new(
pyflakes::rules::UndefinedName { name: id.clone() },
Range::from_located(expr),
));
}
}
@@ -4506,26 +4549,29 @@ impl<'a> Checker<'a> {
where
'b: 'a,
{
if let ExprKind::Name { id, .. } = &expr.node {
if operations::on_conditional_branch(
&mut self.parents.iter().rev().map(std::convert::Into::into),
) {
return;
}
let scope =
&mut self.scopes[*(self.scope_stack.last().expect("No current scope found"))];
if scope.bindings.remove(&id.as_str()).is_none()
&& self.settings.rules.enabled(&Rule::UndefinedName)
{
self.diagnostics.push(Diagnostic::new(
pyflakes::rules::UndefinedName {
name: id.to_string(),
},
Range::from_located(expr),
));
}
let ExprKind::Name { id, .. } = &expr.node else {
return;
};
if operations::on_conditional_branch(
&mut self.parents.iter().rev().map(std::convert::Into::into),
) {
return;
}
let scope = &mut self.scopes[*(self.scope_stack.last().expect("No current scope found"))];
if scope.bindings.remove(&id.as_str()).is_some() {
return;
}
if !self.settings.rules.enabled(&Rule::UndefinedName) {
return;
}
self.diagnostics.push(Diagnostic::new(
pyflakes::rules::UndefinedName {
name: id.to_string(),
},
Range::from_located(expr),
));
}
fn visit_docstring<'b>(&mut self, python_ast: &'b Suite) -> bool

View File

@@ -179,6 +179,8 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<Rule> {
(Flake8Bugbear, "025") => Rule::DuplicateTryBlockException,
(Flake8Bugbear, "026") => Rule::StarArgUnpackingAfterKeywordArg,
(Flake8Bugbear, "027") => Rule::EmptyMethodWithoutAbstractDecorator,
(Flake8Bugbear, "029") => Rule::ExceptWithEmptyTuple,
(Flake8Bugbear, "032") => Rule::UnintentionalTypeAnnotation,
(Flake8Bugbear, "904") => Rule::RaiseWithoutFromInsideExcept,
(Flake8Bugbear, "905") => Rule::ZipWithoutExplicitStrict,
@@ -276,6 +278,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<Rule> {
(Flake8Simplify, "112") => Rule::UseCapitalEnvironmentVariables,
(Flake8Simplify, "114") => Rule::IfWithSameArms,
(Flake8Simplify, "115") => Rule::OpenFileWithContextHandler,
(Flake8Simplify, "116") => Rule::ManualDictLookup,
(Flake8Simplify, "117") => Rule::MultipleWithStatements,
(Flake8Simplify, "118") => Rule::KeyInDict,
(Flake8Simplify, "201") => Rule::NegateEqualOp,
@@ -545,6 +548,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<Rule> {
(Tryceratops, "300") => Rule::TryConsiderElse,
(Tryceratops, "301") => Rule::RaiseWithinTry,
(Tryceratops, "400") => Rule::ErrorInsteadOfException,
(Tryceratops, "401") => Rule::VerboseLogMessage,
// flake8-use-pathlib
(Flake8UsePathlib, "100") => Rule::PathlibAbspath,

View File

@@ -11,7 +11,6 @@ pub use rule_selector::RuleSelector;
pub use rules::pycodestyle::rules::IOError;
pub use violation::{AutofixKind, Availability as AutofixAvailability};
mod assert_yaml_snapshot;
mod ast;
mod autofix;
pub mod cache;

View File

@@ -322,7 +322,7 @@ fn push_codes<I: Display>(str: &mut String, codes: impl Iterator<Item = I>) {
if !first {
str.push_str(", ");
}
let _ = write!(str, "{}", code);
let _ = write!(str, "{code}");
first = false;
}
}

View File

@@ -182,6 +182,8 @@ ruff_macros::register_rules!(
rules::flake8_bugbear::rules::EmptyMethodWithoutAbstractDecorator,
rules::flake8_bugbear::rules::RaiseWithoutFromInsideExcept,
rules::flake8_bugbear::rules::ZipWithoutExplicitStrict,
rules::flake8_bugbear::rules::ExceptWithEmptyTuple,
rules::flake8_bugbear::rules::UnintentionalTypeAnnotation,
// flake8-blind-except
rules::flake8_blind_except::rules::BlindExcept,
// flake8-comprehensions
@@ -253,6 +255,7 @@ ruff_macros::register_rules!(
rules::flake8_2020::rules::SysVersionCmpStr10,
rules::flake8_2020::rules::SysVersionSlice1Referenced,
// flake8-simplify
rules::flake8_simplify::rules::ManualDictLookup,
rules::flake8_simplify::rules::DuplicateIsinstanceCall,
rules::flake8_simplify::rules::CollapsibleIf,
rules::flake8_simplify::rules::NeedlessBool,
@@ -512,6 +515,7 @@ ruff_macros::register_rules!(
rules::tryceratops::rules::TryConsiderElse,
rules::tryceratops::rules::RaiseWithinTry,
rules::tryceratops::rules::ErrorInsteadOfException,
rules::tryceratops::rules::VerboseLogMessage,
// flake8-use-pathlib
rules::flake8_use_pathlib::violations::PathlibAbspath,
rules::flake8_use_pathlib::violations::PathlibChmod,

View File

@@ -7,11 +7,12 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::registry::Rule;
use crate::settings;
use crate::test::test_path;
use crate::{assert_yaml_snapshot, settings};
#[test_case(Rule::CommentedOutCode, Path::new("ERA001.py"); "ERA001")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {

View File

@@ -1,5 +1,5 @@
---
source: src/rules/eradicate/mod.rs
source: crates/ruff/src/rules/eradicate/mod.rs
expression: diagnostics
---
- kind:
@@ -11,8 +11,7 @@ expression: diagnostics
row: 1
column: 10
fix:
content:
- ""
content: ""
location:
row: 1
column: 0
@@ -29,8 +28,7 @@ expression: diagnostics
row: 2
column: 22
fix:
content:
- ""
content: ""
location:
row: 2
column: 0
@@ -47,8 +45,7 @@ expression: diagnostics
row: 3
column: 6
fix:
content:
- ""
content: ""
location:
row: 3
column: 0
@@ -65,8 +62,7 @@ expression: diagnostics
row: 5
column: 13
fix:
content:
- ""
content: ""
location:
row: 5
column: 0
@@ -83,8 +79,7 @@ expression: diagnostics
row: 12
column: 16
fix:
content:
- ""
content: ""
location:
row: 12
column: 0

View File

@@ -6,11 +6,12 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::registry::Rule;
use crate::settings;
use crate::test::test_path;
use crate::{assert_yaml_snapshot, settings};
#[test_case(Rule::SysVersionSlice3Referenced, Path::new("YTT101.py"); "YTT101")]
#[test_case(Rule::SysVersion2Referenced, Path::new("YTT102.py"); "YTT102")]

View File

@@ -9,8 +9,8 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use crate::assert_yaml_snapshot;
use crate::registry::Rule;
use crate::settings::Settings;
use crate::test::test_path;

View File

@@ -8,9 +8,9 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::assert_yaml_snapshot;
use crate::registry::Rule;
use crate::settings::Settings;
use crate::test::test_path;

View File

@@ -6,11 +6,12 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::registry::Rule;
use crate::settings;
use crate::test::test_path;
use crate::{assert_yaml_snapshot, settings};
#[test_case(Rule::BlindExcept, Path::new("BLE.py"); "BLE001")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {

View File

@@ -6,11 +6,12 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::registry::Rule;
use crate::settings;
use crate::test::test_path;
use crate::{assert_yaml_snapshot, settings};
#[test_case(Rule::BooleanPositionalArgInFunctionDefinition, Path::new("FBT.py"); "FBT001")]
#[test_case(Rule::BooleanDefaultValueInFunctionDefinition, Path::new("FBT.py"); "FBT002")]

View File

@@ -1,6 +1,8 @@
use ruff_macros::{define_violation, derive_message_formats};
use rustpython_parser::ast::{Arguments, Constant, Expr, ExprKind};
use ruff_macros::{define_violation, derive_message_formats};
use crate::ast::helpers::collect_call_path;
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::registry::{Diagnostic, DiagnosticKind};
@@ -50,6 +52,10 @@ const FUNC_CALL_NAME_ALLOWLIST: &[&str] = &[
"pop",
"setattr",
"setdefault",
"str",
"bytes",
"int",
"float",
];
const FUNC_DEF_NAME_ALLOWLIST: &[&str] = &["__setitem__"];
@@ -87,10 +93,23 @@ fn add_if_boolean(checker: &mut Checker, arg: &Expr, kind: DiagnosticKind) {
}
}
pub fn check_positional_boolean_in_def(checker: &mut Checker, name: &str, arguments: &Arguments) {
pub fn check_positional_boolean_in_def(
checker: &mut Checker,
name: &str,
decorator_list: &[Expr],
arguments: &Arguments,
) {
if FUNC_DEF_NAME_ALLOWLIST.contains(&name) {
return;
}
if decorator_list.iter().any(|expr| {
let call_path = collect_call_path(expr);
call_path.as_slice() == [name, "setter"]
}) {
return;
}
for arg in arguments.posonlyargs.iter().chain(arguments.args.iter()) {
if arg.node.annotation.is_none() {
continue;
@@ -121,11 +140,20 @@ pub fn check_positional_boolean_in_def(checker: &mut Checker, name: &str, argume
pub fn check_boolean_default_value_in_function_definition(
checker: &mut Checker,
name: &str,
decorator_list: &[Expr],
arguments: &Arguments,
) {
if FUNC_DEF_NAME_ALLOWLIST.contains(&name) {
return;
}
if decorator_list.iter().any(|expr| {
let call_path = collect_call_path(expr);
call_path.as_slice() == [name, "setter"]
}) {
return;
}
for arg in &arguments.defaults {
add_if_boolean(checker, arg, BooleanDefaultValueInFunctionDefinition.into());
}

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_boolean_trap/mod.rs
source: crates/ruff/src/rules/flake8_boolean_trap/mod.rs
expression: diagnostics
---
- kind:
@@ -82,4 +82,14 @@ expression: diagnostics
column: 45
fix: ~
parent: ~
- kind:
BooleanPositionalArgInFunctionDefinition: ~
location:
row: 77
column: 18
end_location:
row: 77
column: 29
fix: ~
parent: ~

View File

@@ -7,9 +7,9 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::assert_yaml_snapshot;
use crate::registry::Rule;
use crate::settings::Settings;
use crate::test::test_path;
@@ -41,6 +41,8 @@ mod tests {
#[test_case(Rule::StarArgUnpackingAfterKeywordArg, Path::new("B026.py"); "B026")]
#[test_case(Rule::EmptyMethodWithoutAbstractDecorator, Path::new("B027.py"); "B027")]
#[test_case(Rule::EmptyMethodWithoutAbstractDecorator, Path::new("B027.pyi"); "B027_pyi")]
#[test_case(Rule::ExceptWithEmptyTuple, Path::new("B029.py"); "B029")]
#[test_case(Rule::UnintentionalTypeAnnotation, Path::new("B032.py"); "B032")]
#[test_case(Rule::RaiseWithoutFromInsideExcept, Path::new("B904.py"); "B904")]
#[test_case(Rule::ZipWithoutExplicitStrict, Path::new("B905.py"); "B905")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {

View File

@@ -0,0 +1,36 @@
use ruff_macros::{define_violation, derive_message_formats};
use rustpython_parser::ast::Excepthandler;
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::registry::Diagnostic;
use crate::violation::Violation;
use rustpython_parser::ast::{ExcepthandlerKind, ExprKind};
define_violation!(
pub struct ExceptWithEmptyTuple;
);
impl Violation for ExceptWithEmptyTuple {
#[derive_message_formats]
fn message(&self) -> String {
format!("Using except (): with an empty tuple does not handle/catch anything. Add exceptions to handle.")
}
}
/// B029
pub fn except_with_empty_tuple(checker: &mut Checker, excepthandler: &Excepthandler) {
let ExcepthandlerKind::ExceptHandler { type_, .. } = &excepthandler.node;
if type_.is_none() {
return;
}
let ExprKind::Tuple { elts, .. } = &type_.as_ref().unwrap().node else {
return;
};
if elts.is_empty() {
checker.diagnostics.push(Diagnostic::new(
ExceptWithEmptyTuple,
Range::from_located(excepthandler),
));
}
}

View File

@@ -46,10 +46,16 @@ fn walk_stmt(checker: &mut Checker, body: &[Stmt], f: fn(&Stmt) -> bool) {
}
StmtKind::If { body, .. }
| StmtKind::Try { body, .. }
| StmtKind::TryStar { body, .. }
| StmtKind::With { body, .. }
| StmtKind::AsyncWith { body, .. } => {
walk_stmt(checker, body, f);
}
StmtKind::Match { cases, .. } => {
for case in cases {
walk_stmt(checker, &case.body, f);
}
}
_ => {}
}
}

View File

@@ -10,6 +10,7 @@ pub use cannot_raise_literal::{cannot_raise_literal, CannotRaiseLiteral};
pub use duplicate_exceptions::{
duplicate_exceptions, DuplicateHandlerException, DuplicateTryBlockException,
};
pub use except_with_empty_tuple::{except_with_empty_tuple, ExceptWithEmptyTuple};
pub use f_string_docstring::{f_string_docstring, FStringDocstring};
pub use function_call_argument_default::{
function_call_argument_default, FunctionCallArgumentDefault,
@@ -40,6 +41,10 @@ pub use useless_contextlib_suppress::{useless_contextlib_suppress, UselessContex
pub use useless_expression::{useless_expression, UselessExpression};
pub use zip_without_explicit_strict::{zip_without_explicit_strict, ZipWithoutExplicitStrict};
pub use unintentional_type_annotation::{
unintentional_type_annotation, UnintentionalTypeAnnotation,
};
mod abstract_base_class;
mod assert_false;
mod assert_raises_exception;
@@ -47,6 +52,7 @@ mod assignment_to_os_environ;
mod cached_instance_method;
mod cannot_raise_literal;
mod duplicate_exceptions;
mod except_with_empty_tuple;
mod f_string_docstring;
mod function_call_argument_default;
mod function_uses_loop_variable;
@@ -60,6 +66,7 @@ mod setattr_with_constant;
mod star_arg_unpacking_after_keyword_arg;
mod strip_with_multi_characters;
mod unary_prefix_increment;
mod unintentional_type_annotation;
mod unreliable_callable_check;
mod unused_loop_control_variable;
mod useless_comparison;

View File

@@ -44,7 +44,8 @@ impl<'a> Visitor<'a> for RaiseVisitor {
StmtKind::ClassDef { .. }
| StmtKind::FunctionDef { .. }
| StmtKind::AsyncFunctionDef { .. }
| StmtKind::Try { .. } => {}
| StmtKind::Try { .. }
| StmtKind::TryStar { .. } => {}
StmtKind::If { body, orelse, .. } => {
visitor::walk_body(self, body);
visitor::walk_body(self, orelse);
@@ -56,11 +57,17 @@ impl<'a> Visitor<'a> for RaiseVisitor {
| StmtKind::AsyncFor { body, .. } => {
visitor::walk_body(self, body);
}
StmtKind::Match { cases, .. } => {
for case in cases {
visitor::walk_body(self, &case.body);
}
}
_ => {}
}
}
}
/// B904
pub fn raise_without_from_inside_except(checker: &mut Checker, body: &[Stmt]) {
let mut visitor = RaiseVisitor {
diagnostics: vec![],

View File

@@ -0,0 +1,69 @@
use rustpython_parser::ast::{Expr, ExprKind, Stmt};
use ruff_macros::{define_violation, derive_message_formats};
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::registry::Diagnostic;
use crate::violation::Violation;
define_violation!(
/// ## What it does
/// Checks for the unintentional use of type annotations.
///
/// ## Why is this bad?
/// The use of a colon (`:`) in lieu of an assignment (`=`) can be syntactically valid, but
/// is almost certainly a mistake when used in a subscript or attribute assignment.
///
/// ## Example
/// ```python
/// a["b"]: 1
/// ```
///
/// Use instead:
/// ```python
/// a["b"] = 1
/// ```
pub struct UnintentionalTypeAnnotation;
);
impl Violation for UnintentionalTypeAnnotation {
#[derive_message_formats]
fn message(&self) -> String {
format!(
"Possible unintentional type annotation (using `:`). Did you mean to assign (using `=`)?"
)
}
}
/// B032
pub fn unintentional_type_annotation(
checker: &mut Checker,
target: &Expr,
value: Option<&Expr>,
stmt: &Stmt,
) {
if value.is_some() {
return;
}
match &target.node {
ExprKind::Subscript { value, .. } => {
if matches!(&value.node, ExprKind::Name { .. }) {
checker.diagnostics.push(Diagnostic::new(
UnintentionalTypeAnnotation,
Range::from_located(stmt),
));
}
}
ExprKind::Attribute { value, .. } => {
if let ExprKind::Name { id, .. } = &value.node {
if id != "self" {
checker.diagnostics.push(Diagnostic::new(
UnintentionalTypeAnnotation,
Range::from_located(stmt),
));
}
}
}
_ => {}
};
}

View File

@@ -18,8 +18,6 @@
//! method()
//! ```
use std::iter;
use ruff_macros::{define_violation, derive_message_formats};
use rustc_hash::FxHashMap;
use rustpython_parser::ast::{Expr, ExprKind, Stmt};
@@ -174,27 +172,19 @@ pub fn unused_loop_control_variable(
if matches!(certainty, Certainty::Certain) && checker.patch(diagnostic.kind.rule()) {
// Find the `BindingKind::LoopVar` corresponding to the name.
let scope = checker.current_scope();
if let Some(binding) = iter::once(scope.bindings.get(name))
.flatten()
.chain(
iter::once(scope.rebounds.get(name))
.flatten()
.into_iter()
.flatten(),
)
let binding = scope
.bindings
.get(name)
.into_iter()
.chain(scope.rebounds.get(name).into_iter().flatten())
.find_map(|index| {
let binding = &checker.bindings[*index];
if let Some(source) = &binding.source {
if source == &RefEquality(stmt) {
Some(binding)
} else {
None
}
} else {
None
}
})
{
binding
.source
.as_ref()
.and_then(|source| (source == &RefEquality(stmt)).then_some(binding))
});
if let Some(binding) = binding {
if matches!(binding.kind, BindingKind::LoopVar) {
if !binding.used() {
diagnostic.amend(Fix::replacement(

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_bugbear/mod.rs
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
expression: diagnostics
---
- kind:
@@ -27,8 +27,7 @@ expression: diagnostics
row: 18
column: 13
fix:
content:
- _k
content: _k
location:
row: 18
column: 12
@@ -61,8 +60,7 @@ expression: diagnostics
row: 30
column: 13
fix:
content:
- _k
content: _k
location:
row: 30
column: 12
@@ -134,8 +132,7 @@ expression: diagnostics
row: 52
column: 16
fix:
content:
- _bar
content: _bar
location:
row: 52
column: 13
@@ -168,8 +165,7 @@ expression: diagnostics
row: 68
column: 16
fix:
content:
- _bar
content: _bar
location:
row: 68
column: 13
@@ -189,8 +185,7 @@ expression: diagnostics
row: 77
column: 16
fix:
content:
- _bar
content: _bar
location:
row: 77
column: 13

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_bugbear/mod.rs
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
expression: diagnostics
---
- kind:
@@ -11,8 +11,7 @@ expression: diagnostics
row: 19
column: 19
fix:
content:
- foo.bar
content: foo.bar
location:
row: 19
column: 0
@@ -29,8 +28,7 @@ expression: diagnostics
row: 20
column: 23
fix:
content:
- foo._123abc
content: foo._123abc
location:
row: 20
column: 0
@@ -47,8 +45,7 @@ expression: diagnostics
row: 21
column: 26
fix:
content:
- foo.__123abc__
content: foo.__123abc__
location:
row: 21
column: 0
@@ -65,8 +62,7 @@ expression: diagnostics
row: 22
column: 22
fix:
content:
- foo.abc123
content: foo.abc123
location:
row: 22
column: 0
@@ -83,8 +79,7 @@ expression: diagnostics
row: 23
column: 23
fix:
content:
- foo.abc123
content: foo.abc123
location:
row: 23
column: 0
@@ -101,8 +96,7 @@ expression: diagnostics
row: 24
column: 31
fix:
content:
- x.bar
content: x.bar
location:
row: 24
column: 14
@@ -119,8 +113,7 @@ expression: diagnostics
row: 25
column: 20
fix:
content:
- x.bar
content: x.bar
location:
row: 25
column: 3

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_bugbear/mod.rs
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
expression: diagnostics
---
- kind:
@@ -11,8 +11,7 @@ expression: diagnostics
row: 40
column: 25
fix:
content:
- foo.bar = None
content: foo.bar = None
location:
row: 40
column: 0
@@ -29,8 +28,7 @@ expression: diagnostics
row: 41
column: 29
fix:
content:
- foo._123abc = None
content: foo._123abc = None
location:
row: 41
column: 0
@@ -47,8 +45,7 @@ expression: diagnostics
row: 42
column: 32
fix:
content:
- foo.__123abc__ = None
content: foo.__123abc__ = None
location:
row: 42
column: 0
@@ -65,8 +62,7 @@ expression: diagnostics
row: 43
column: 28
fix:
content:
- foo.abc123 = None
content: foo.abc123 = None
location:
row: 43
column: 0
@@ -83,8 +79,7 @@ expression: diagnostics
row: 44
column: 29
fix:
content:
- foo.abc123 = None
content: foo.abc123 = None
location:
row: 44
column: 0
@@ -101,8 +96,7 @@ expression: diagnostics
row: 45
column: 30
fix:
content:
- foo.bar.baz = None
content: foo.bar.baz = None
location:
row: 45
column: 0

View File

@@ -11,8 +11,7 @@ expression: diagnostics
row: 8
column: 12
fix:
content:
- raise AssertionError()
content: raise AssertionError()
location:
row: 8
column: 0
@@ -29,8 +28,7 @@ expression: diagnostics
row: 10
column: 12
fix:
content:
- "raise AssertionError(\"message\")"
content: "raise AssertionError(\"message\")"
location:
row: 10
column: 0

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_bugbear/mod.rs
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
expression: diagnostics
---
- kind:
@@ -112,4 +112,15 @@ expression: diagnostics
column: 13
fix: ~
parent: ~
- kind:
JumpStatementInFinally:
name: break
location:
row: 118
column: 16
end_location:
row: 118
column: 21
fix: ~
parent: ~

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_bugbear/mod.rs
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
expression: diagnostics
---
- kind:
@@ -12,8 +12,7 @@ expression: diagnostics
row: 3
column: 20
fix:
content:
- ValueError
content: ValueError
location:
row: 3
column: 7

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_bugbear/mod.rs
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
expression: diagnostics
---
- kind:
@@ -13,8 +13,7 @@ expression: diagnostics
row: 17
column: 25
fix:
content:
- OSError
content: OSError
location:
row: 17
column: 7
@@ -33,8 +32,7 @@ expression: diagnostics
row: 28
column: 25
fix:
content:
- MyError
content: MyError
location:
row: 28
column: 7
@@ -53,8 +51,7 @@ expression: diagnostics
row: 49
column: 27
fix:
content:
- re.error
content: re.error
location:
row: 49
column: 7

View File

@@ -0,0 +1,25 @@
---
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
expression: diagnostics
---
- kind:
ExceptWithEmptyTuple: ~
location:
row: 8
column: 0
end_location:
row: 9
column: 8
fix: ~
parent: ~
- kind:
ExceptWithEmptyTuple: ~
location:
row: 13
column: 0
end_location:
row: 14
column: 8
fix: ~
parent: ~

View File

@@ -0,0 +1,85 @@
---
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
expression: diagnostics
---
- kind:
UnintentionalTypeAnnotation: ~
location:
row: 9
column: 0
end_location:
row: 9
column: 11
fix: ~
parent: ~
- kind:
UnintentionalTypeAnnotation: ~
location:
row: 10
column: 0
end_location:
row: 10
column: 8
fix: ~
parent: ~
- kind:
UnintentionalTypeAnnotation: ~
location:
row: 12
column: 0
end_location:
row: 12
column: 16
fix: ~
parent: ~
- kind:
UnintentionalTypeAnnotation: ~
location:
row: 13
column: 0
end_location:
row: 13
column: 13
fix: ~
parent: ~
- kind:
UnintentionalTypeAnnotation: ~
location:
row: 16
column: 0
end_location:
row: 16
column: 14
fix: ~
parent: ~
- kind:
UnintentionalTypeAnnotation: ~
location:
row: 17
column: 0
end_location:
row: 17
column: 22
fix: ~
parent: ~
- kind:
UnintentionalTypeAnnotation: ~
location:
row: 18
column: 0
end_location:
row: 18
column: 11
fix: ~
parent: ~
- kind:
UnintentionalTypeAnnotation: ~
location:
row: 19
column: 0
end_location:
row: 19
column: 19
fix: ~
parent: ~

View File

@@ -52,4 +52,14 @@ expression: diagnostics
column: 35
fix: ~
parent: ~
- kind:
RaiseWithoutFromInsideExcept: ~
location:
row: 72
column: 12
end_location:
row: 72
column: 39
fix: ~
parent: ~

View File

@@ -8,9 +8,9 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::assert_yaml_snapshot;
use crate::registry::Rule;
use crate::settings::Settings;
use crate::test::test_path;

View File

@@ -6,11 +6,12 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::registry::Rule;
use crate::settings;
use crate::test::test_path;
use crate::{assert_yaml_snapshot, settings};
#[test_case(Path::new("COM81.py"); "COM81")]
fn rules(path: &Path) -> Result<()> {

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_commas/mod.rs
source: crates/ruff/src/rules/flake8_commas/mod.rs
expression: diagnostics
---
- kind:
@@ -11,8 +11,7 @@ expression: diagnostics
row: 4
column: 17
fix:
content:
- ","
content: ","
location:
row: 4
column: 17
@@ -29,8 +28,7 @@ expression: diagnostics
row: 10
column: 5
fix:
content:
- ","
content: ","
location:
row: 10
column: 5
@@ -47,8 +45,7 @@ expression: diagnostics
row: 16
column: 5
fix:
content:
- ","
content: ","
location:
row: 16
column: 5
@@ -65,8 +62,7 @@ expression: diagnostics
row: 23
column: 5
fix:
content:
- ","
content: ","
location:
row: 23
column: 5
@@ -153,8 +149,7 @@ expression: diagnostics
row: 70
column: 7
fix:
content:
- ","
content: ","
location:
row: 70
column: 7
@@ -171,8 +166,7 @@ expression: diagnostics
row: 78
column: 7
fix:
content:
- ","
content: ","
location:
row: 78
column: 7
@@ -189,8 +183,7 @@ expression: diagnostics
row: 86
column: 7
fix:
content:
- ","
content: ","
location:
row: 86
column: 7
@@ -207,8 +200,7 @@ expression: diagnostics
row: 152
column: 5
fix:
content:
- ","
content: ","
location:
row: 152
column: 5
@@ -225,8 +217,7 @@ expression: diagnostics
row: 158
column: 10
fix:
content:
- ","
content: ","
location:
row: 158
column: 10
@@ -243,8 +234,7 @@ expression: diagnostics
row: 293
column: 14
fix:
content:
- ","
content: ","
location:
row: 293
column: 14
@@ -261,8 +251,7 @@ expression: diagnostics
row: 304
column: 13
fix:
content:
- ","
content: ","
location:
row: 304
column: 13
@@ -279,8 +268,7 @@ expression: diagnostics
row: 310
column: 13
fix:
content:
- ","
content: ","
location:
row: 310
column: 13
@@ -297,8 +285,7 @@ expression: diagnostics
row: 316
column: 9
fix:
content:
- ","
content: ","
location:
row: 316
column: 9
@@ -315,8 +302,7 @@ expression: diagnostics
row: 322
column: 14
fix:
content:
- ","
content: ","
location:
row: 322
column: 14
@@ -333,8 +319,7 @@ expression: diagnostics
row: 368
column: 14
fix:
content:
- ","
content: ","
location:
row: 368
column: 14
@@ -351,8 +336,7 @@ expression: diagnostics
row: 375
column: 14
fix:
content:
- ","
content: ","
location:
row: 375
column: 14
@@ -369,8 +353,7 @@ expression: diagnostics
row: 404
column: 14
fix:
content:
- ","
content: ","
location:
row: 404
column: 14
@@ -387,8 +370,7 @@ expression: diagnostics
row: 432
column: 14
fix:
content:
- ","
content: ","
location:
row: 432
column: 14
@@ -405,8 +387,7 @@ expression: diagnostics
row: 485
column: 21
fix:
content:
- ""
content: ""
location:
row: 485
column: 20
@@ -423,8 +404,7 @@ expression: diagnostics
row: 487
column: 13
fix:
content:
- ""
content: ""
location:
row: 487
column: 12
@@ -441,8 +421,7 @@ expression: diagnostics
row: 489
column: 18
fix:
content:
- ""
content: ""
location:
row: 489
column: 17
@@ -459,8 +438,7 @@ expression: diagnostics
row: 494
column: 6
fix:
content:
- ""
content: ""
location:
row: 494
column: 5
@@ -477,8 +455,7 @@ expression: diagnostics
row: 496
column: 21
fix:
content:
- ""
content: ""
location:
row: 496
column: 20
@@ -495,8 +472,7 @@ expression: diagnostics
row: 498
column: 13
fix:
content:
- ""
content: ""
location:
row: 498
column: 12
@@ -513,8 +489,7 @@ expression: diagnostics
row: 500
column: 18
fix:
content:
- ""
content: ""
location:
row: 500
column: 17
@@ -531,8 +506,7 @@ expression: diagnostics
row: 505
column: 6
fix:
content:
- ""
content: ""
location:
row: 505
column: 5
@@ -549,8 +523,7 @@ expression: diagnostics
row: 511
column: 10
fix:
content:
- ""
content: ""
location:
row: 511
column: 9
@@ -567,8 +540,7 @@ expression: diagnostics
row: 513
column: 9
fix:
content:
- ""
content: ""
location:
row: 513
column: 8
@@ -585,8 +557,7 @@ expression: diagnostics
row: 519
column: 12
fix:
content:
- ","
content: ","
location:
row: 519
column: 12
@@ -603,8 +574,7 @@ expression: diagnostics
row: 526
column: 9
fix:
content:
- ","
content: ","
location:
row: 526
column: 9
@@ -621,8 +591,7 @@ expression: diagnostics
row: 534
column: 15
fix:
content:
- ","
content: ","
location:
row: 534
column: 15
@@ -639,8 +608,7 @@ expression: diagnostics
row: 541
column: 12
fix:
content:
- ","
content: ","
location:
row: 541
column: 12
@@ -657,8 +625,7 @@ expression: diagnostics
row: 547
column: 23
fix:
content:
- ","
content: ","
location:
row: 547
column: 23
@@ -675,8 +642,7 @@ expression: diagnostics
row: 554
column: 14
fix:
content:
- ","
content: ","
location:
row: 554
column: 14
@@ -693,8 +659,7 @@ expression: diagnostics
row: 561
column: 12
fix:
content:
- ","
content: ","
location:
row: 561
column: 12
@@ -711,8 +676,7 @@ expression: diagnostics
row: 565
column: 12
fix:
content:
- ","
content: ","
location:
row: 565
column: 12
@@ -729,8 +693,7 @@ expression: diagnostics
row: 573
column: 9
fix:
content:
- ","
content: ","
location:
row: 573
column: 9
@@ -747,8 +710,7 @@ expression: diagnostics
row: 577
column: 9
fix:
content:
- ","
content: ","
location:
row: 577
column: 9
@@ -765,8 +727,7 @@ expression: diagnostics
row: 583
column: 9
fix:
content:
- ","
content: ","
location:
row: 583
column: 9
@@ -783,8 +744,7 @@ expression: diagnostics
row: 590
column: 12
fix:
content:
- ","
content: ","
location:
row: 590
column: 12
@@ -801,8 +761,7 @@ expression: diagnostics
row: 598
column: 14
fix:
content:
- ","
content: ","
location:
row: 598
column: 14
@@ -819,8 +778,7 @@ expression: diagnostics
row: 627
column: 19
fix:
content:
- ","
content: ","
location:
row: 627
column: 19

View File

@@ -8,9 +8,9 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::assert_yaml_snapshot;
use crate::registry::Rule;
use crate::settings::Settings;
use crate::test::test_path;
@@ -31,7 +31,6 @@ mod tests {
#[test_case(Rule::UnnecessarySubscriptReversal, Path::new("C415.py"); "C415")]
#[test_case(Rule::UnnecessaryComprehension, Path::new("C416.py"); "C416")]
#[test_case(Rule::UnnecessaryMap, Path::new("C417.py"); "C417")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path(

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_comprehensions/mod.rs
source: crates/ruff/src/rules/flake8_comprehensions/mod.rs
expression: diagnostics
---
- kind:
@@ -11,8 +11,7 @@ expression: diagnostics
row: 1
column: 29
fix:
content:
- "[x for x in range(3)]"
content: "[x for x in range(3)]"
location:
row: 1
column: 4
@@ -29,10 +28,7 @@ expression: diagnostics
row: 4
column: 1
fix:
content:
- "["
- " x for x in range(3)"
- "]"
content: "[\n x for x in range(3)\n]"
location:
row: 2
column: 4

View File

@@ -11,8 +11,7 @@ expression: diagnostics
row: 1
column: 28
fix:
content:
- "{x for x in range(3)}"
content: "{x for x in range(3)}"
location:
row: 1
column: 4
@@ -29,10 +28,7 @@ expression: diagnostics
row: 4
column: 1
fix:
content:
- "{"
- " x for x in range(3)"
- "}"
content: "{\n x for x in range(3)\n}"
location:
row: 2
column: 4
@@ -49,8 +45,7 @@ expression: diagnostics
row: 5
column: 48
fix:
content:
- " {a if a < 6 else 0 for a in range(3)} "
content: " {a if a < 6 else 0 for a in range(3)} "
location:
row: 5
column: 7
@@ -67,8 +62,7 @@ expression: diagnostics
row: 6
column: 57
fix:
content:
- "{a if a < 6 else 0 for a in range(3)}"
content: "{a if a < 6 else 0 for a in range(3)}"
location:
row: 6
column: 16
@@ -85,8 +79,7 @@ expression: diagnostics
row: 7
column: 39
fix:
content:
- " {a for a in range(3)} "
content: " {a for a in range(3)} "
location:
row: 7
column: 15

View File

@@ -11,8 +11,7 @@ expression: diagnostics
row: 1
column: 30
fix:
content:
- "{x: x for x in range(3)}"
content: "{x: x for x in range(3)}"
location:
row: 1
column: 0
@@ -29,10 +28,7 @@ expression: diagnostics
row: 4
column: 1
fix:
content:
- "{"
- " x: x for x in range(3)"
- "}"
content: "{\n x: x for x in range(3)\n}"
location:
row: 2
column: 0
@@ -49,8 +45,7 @@ expression: diagnostics
row: 6
column: 37
fix:
content:
- " {x: x for x in range(3)} "
content: " {x: x for x in range(3)} "
location:
row: 6
column: 7
@@ -67,8 +62,7 @@ expression: diagnostics
row: 7
column: 45
fix:
content:
- " {x: x for x in range(3)} "
content: " {x: x for x in range(3)} "
location:
row: 7
column: 15

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_comprehensions/mod.rs
source: crates/ruff/src/rules/flake8_comprehensions/mod.rs
expression: diagnostics
---
- kind:
@@ -11,8 +11,7 @@ expression: diagnostics
row: 1
column: 30
fix:
content:
- "{x for x in range(3)}"
content: "{x for x in range(3)}"
location:
row: 1
column: 4
@@ -29,10 +28,7 @@ expression: diagnostics
row: 4
column: 1
fix:
content:
- "{"
- " x for x in range(3)"
- "}"
content: "{\n x for x in range(3)\n}"
location:
row: 2
column: 4

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_comprehensions/mod.rs
source: crates/ruff/src/rules/flake8_comprehensions/mod.rs
expression: diagnostics
---
- kind:
@@ -11,8 +11,7 @@ expression: diagnostics
row: 1
column: 32
fix:
content:
- "{i: i for i in range(3)}"
content: "{i: i for i in range(3)}"
location:
row: 1
column: 0

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_comprehensions/mod.rs
source: crates/ruff/src/rules/flake8_comprehensions/mod.rs
expression: diagnostics
---
- kind:
@@ -12,8 +12,7 @@ expression: diagnostics
row: 1
column: 11
fix:
content:
- "{1, 2}"
content: "{1, 2}"
location:
row: 1
column: 0
@@ -31,8 +30,7 @@ expression: diagnostics
row: 2
column: 11
fix:
content:
- "{1, 2}"
content: "{1, 2}"
location:
row: 2
column: 0
@@ -50,8 +48,7 @@ expression: diagnostics
row: 3
column: 7
fix:
content:
- set()
content: set()
location:
row: 3
column: 0
@@ -69,8 +66,7 @@ expression: diagnostics
row: 4
column: 7
fix:
content:
- set()
content: set()
location:
row: 4
column: 0
@@ -88,8 +84,7 @@ expression: diagnostics
row: 6
column: 9
fix:
content:
- "{1}"
content: "{1}"
location:
row: 6
column: 0
@@ -107,10 +102,7 @@ expression: diagnostics
row: 9
column: 2
fix:
content:
- "{"
- " 1,"
- "}"
content: "{\n 1,\n}"
location:
row: 7
column: 0
@@ -128,10 +120,7 @@ expression: diagnostics
row: 12
column: 2
fix:
content:
- "{"
- " 1,"
- "}"
content: "{\n 1,\n}"
location:
row: 10
column: 0
@@ -149,8 +138,7 @@ expression: diagnostics
row: 15
column: 1
fix:
content:
- "{1}"
content: "{1}"
location:
row: 13
column: 0
@@ -168,8 +156,7 @@ expression: diagnostics
row: 18
column: 1
fix:
content:
- "{1,}"
content: "{1,}"
location:
row: 16
column: 0

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_comprehensions/mod.rs
source: crates/ruff/src/rules/flake8_comprehensions/mod.rs
expression: diagnostics
---
- kind:
@@ -12,8 +12,7 @@ expression: diagnostics
row: 1
column: 19
fix:
content:
- "{1: 2}"
content: "{1: 2}"
location:
row: 1
column: 5
@@ -31,8 +30,7 @@ expression: diagnostics
row: 2
column: 20
fix:
content:
- "{1: 2,}"
content: "{1: 2,}"
location:
row: 2
column: 5
@@ -50,8 +48,7 @@ expression: diagnostics
row: 3
column: 13
fix:
content:
- "{}"
content: "{}"
location:
row: 3
column: 5
@@ -69,8 +66,7 @@ expression: diagnostics
row: 4
column: 13
fix:
content:
- "{}"
content: "{}"
location:
row: 4
column: 5

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_comprehensions/mod.rs
source: crates/ruff/src/rules/flake8_comprehensions/mod.rs
expression: diagnostics
---
- kind:
@@ -12,8 +12,7 @@ expression: diagnostics
row: 1
column: 11
fix:
content:
- ()
content: ()
location:
row: 1
column: 4
@@ -31,8 +30,7 @@ expression: diagnostics
row: 2
column: 10
fix:
content:
- "[]"
content: "[]"
location:
row: 2
column: 4
@@ -50,8 +48,7 @@ expression: diagnostics
row: 3
column: 11
fix:
content:
- "{}"
content: "{}"
location:
row: 3
column: 5
@@ -69,8 +66,7 @@ expression: diagnostics
row: 4
column: 14
fix:
content:
- "{\"a\": 1}"
content: "{\"a\": 1}"
location:
row: 4
column: 5

View File

@@ -12,8 +12,7 @@ expression: diagnostics
row: 1
column: 11
fix:
content:
- ()
content: ()
location:
row: 1
column: 4
@@ -31,8 +30,7 @@ expression: diagnostics
row: 2
column: 10
fix:
content:
- "[]"
content: "[]"
location:
row: 2
column: 4
@@ -50,8 +48,7 @@ expression: diagnostics
row: 3
column: 11
fix:
content:
- "{}"
content: "{}"
location:
row: 3
column: 5

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_comprehensions/mod.rs
source: crates/ruff/src/rules/flake8_comprehensions/mod.rs
expression: diagnostics
---
- kind:
@@ -12,8 +12,7 @@ expression: diagnostics
row: 1
column: 14
fix:
content:
- ()
content: ()
location:
row: 1
column: 5
@@ -31,8 +30,7 @@ expression: diagnostics
row: 2
column: 18
fix:
content:
- "(1, 2)"
content: "(1, 2)"
location:
row: 2
column: 5
@@ -50,8 +48,7 @@ expression: diagnostics
row: 3
column: 18
fix:
content:
- "(1, 2)"
content: "(1, 2)"
location:
row: 3
column: 5
@@ -69,11 +66,7 @@ expression: diagnostics
row: 7
column: 2
fix:
content:
- (
- " 1,"
- " 2"
- )
content: "(\n 1,\n 2\n)"
location:
row: 4
column: 5
@@ -91,8 +84,7 @@ expression: diagnostics
row: 10
column: 1
fix:
content:
- "(1, 2)"
content: "(1, 2)"
location:
row: 8
column: 5

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_comprehensions/mod.rs
source: crates/ruff/src/rules/flake8_comprehensions/mod.rs
expression: diagnostics
---
- kind:
@@ -12,8 +12,7 @@ expression: diagnostics
row: 1
column: 17
fix:
content:
- "[1, 2]"
content: "[1, 2]"
location:
row: 1
column: 5
@@ -31,8 +30,7 @@ expression: diagnostics
row: 2
column: 17
fix:
content:
- "[1, 2]"
content: "[1, 2]"
location:
row: 2
column: 5
@@ -50,8 +48,7 @@ expression: diagnostics
row: 3
column: 13
fix:
content:
- "[]"
content: "[]"
location:
row: 3
column: 5
@@ -69,8 +66,7 @@ expression: diagnostics
row: 4
column: 13
fix:
content:
- "[]"
content: "[]"
location:
row: 4
column: 5

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_comprehensions/mod.rs
source: crates/ruff/src/rules/flake8_comprehensions/mod.rs
expression: diagnostics
---
- kind:
@@ -11,8 +11,7 @@ expression: diagnostics
row: 2
column: 20
fix:
content:
- "[i for i in x]"
content: "[i for i in x]"
location:
row: 2
column: 0

View File

@@ -12,8 +12,7 @@ expression: diagnostics
row: 3
column: 15
fix:
content:
- sorted(x)
content: sorted(x)
location:
row: 3
column: 0
@@ -31,8 +30,7 @@ expression: diagnostics
row: 4
column: 19
fix:
content:
- "sorted(x, reverse=True)"
content: "sorted(x, reverse=True)"
location:
row: 4
column: 0
@@ -50,8 +48,7 @@ expression: diagnostics
row: 5
column: 36
fix:
content:
- "sorted(x, key=lambda e: e, reverse=True)"
content: "sorted(x, key=lambda e: e, reverse=True)"
location:
row: 5
column: 0
@@ -69,8 +66,7 @@ expression: diagnostics
row: 6
column: 33
fix:
content:
- "sorted(x, reverse=False)"
content: "sorted(x, reverse=False)"
location:
row: 6
column: 0
@@ -88,8 +84,7 @@ expression: diagnostics
row: 7
column: 50
fix:
content:
- "sorted(x, key=lambda e: e, reverse=False)"
content: "sorted(x, key=lambda e: e, reverse=False)"
location:
row: 7
column: 0
@@ -107,8 +102,7 @@ expression: diagnostics
row: 8
column: 50
fix:
content:
- "sorted(x, reverse=False, key=lambda e: e)"
content: "sorted(x, reverse=False, key=lambda e: e)"
location:
row: 8
column: 0
@@ -126,8 +120,7 @@ expression: diagnostics
row: 9
column: 34
fix:
content:
- "sorted(x, reverse=True)"
content: "sorted(x, reverse=True)"
location:
row: 9
column: 0

View File

@@ -13,8 +13,7 @@ expression: diagnostics
row: 2
column: 13
fix:
content:
- list(x)
content: list(x)
location:
row: 2
column: 0
@@ -33,8 +32,7 @@ expression: diagnostics
row: 3
column: 14
fix:
content:
- list(x)
content: list(x)
location:
row: 3
column: 0
@@ -53,8 +51,7 @@ expression: diagnostics
row: 4
column: 14
fix:
content:
- tuple(x)
content: tuple(x)
location:
row: 4
column: 0
@@ -73,8 +70,7 @@ expression: diagnostics
row: 5
column: 15
fix:
content:
- tuple(x)
content: tuple(x)
location:
row: 5
column: 0
@@ -93,8 +89,7 @@ expression: diagnostics
row: 6
column: 11
fix:
content:
- set(x)
content: set(x)
location:
row: 6
column: 0
@@ -113,8 +108,7 @@ expression: diagnostics
row: 7
column: 12
fix:
content:
- set(x)
content: set(x)
location:
row: 7
column: 0
@@ -133,8 +127,7 @@ expression: diagnostics
row: 8
column: 13
fix:
content:
- set(x)
content: set(x)
location:
row: 8
column: 0
@@ -153,8 +146,7 @@ expression: diagnostics
row: 9
column: 14
fix:
content:
- set(x)
content: set(x)
location:
row: 9
column: 0
@@ -173,8 +165,7 @@ expression: diagnostics
row: 10
column: 16
fix:
content:
- set(x)
content: set(x)
location:
row: 10
column: 0
@@ -193,8 +184,7 @@ expression: diagnostics
row: 11
column: 15
fix:
content:
- sorted(x)
content: sorted(x)
location:
row: 11
column: 0
@@ -213,8 +203,7 @@ expression: diagnostics
row: 12
column: 16
fix:
content:
- sorted(x)
content: sorted(x)
location:
row: 12
column: 0
@@ -233,8 +222,7 @@ expression: diagnostics
row: 13
column: 17
fix:
content:
- sorted(x)
content: sorted(x)
location:
row: 13
column: 0
@@ -253,8 +241,7 @@ expression: diagnostics
row: 14
column: 19
fix:
content:
- sorted(x)
content: sorted(x)
location:
row: 14
column: 0
@@ -273,11 +260,7 @@ expression: diagnostics
row: 20
column: 1
fix:
content:
- tuple(
- " [x, 3, \"hell\"\\"
- " \"o\"]"
- " )"
content: "tuple(\n [x, 3, \"hell\"\\\n \"o\"]\n )"
location:
row: 15
column: 0

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_comprehensions/mod.rs
source: crates/ruff/src/rules/flake8_comprehensions/mod.rs
expression: diagnostics
---
- kind:
@@ -12,8 +12,7 @@ expression: diagnostics
row: 2
column: 14
fix:
content:
- list(x)
content: list(x)
location:
row: 2
column: 0
@@ -31,8 +30,7 @@ expression: diagnostics
row: 3
column: 14
fix:
content:
- set(x)
content: set(x)
location:
row: 3
column: 0

View File

@@ -12,8 +12,7 @@ expression: diagnostics
row: 3
column: 26
fix:
content:
- (x + 1 for x in nums)
content: (x + 1 for x in nums)
location:
row: 3
column: 0
@@ -31,8 +30,7 @@ expression: diagnostics
row: 4
column: 27
fix:
content:
- (str(x) for x in nums)
content: (str(x) for x in nums)
location:
row: 4
column: 0
@@ -50,8 +48,7 @@ expression: diagnostics
row: 5
column: 32
fix:
content:
- "[x * 2 for x in nums]"
content: "[x * 2 for x in nums]"
location:
row: 5
column: 0
@@ -69,8 +66,7 @@ expression: diagnostics
row: 6
column: 36
fix:
content:
- "{x % 2 == 0 for x in nums}"
content: "{x % 2 == 0 for x in nums}"
location:
row: 6
column: 0
@@ -88,8 +84,7 @@ expression: diagnostics
row: 7
column: 36
fix:
content:
- "{v: v**2 for v in nums}"
content: "{v: v**2 for v in nums}"
location:
row: 7
column: 0
@@ -107,8 +102,7 @@ expression: diagnostics
row: 8
column: 26
fix:
content:
- "(\"const\" for _ in nums)"
content: "(\"const\" for _ in nums)"
location:
row: 8
column: 0
@@ -126,8 +120,7 @@ expression: diagnostics
row: 9
column: 24
fix:
content:
- (3.0 for _ in nums)
content: (3.0 for _ in nums)
location:
row: 9
column: 0
@@ -145,8 +138,7 @@ expression: diagnostics
row: 10
column: 63
fix:
content:
- "(x in nums and \"1\" or \"0\" for x in range(123))"
content: "(x in nums and \"1\" or \"0\" for x in range(123))"
location:
row: 10
column: 12
@@ -164,8 +156,7 @@ expression: diagnostics
row: 11
column: 44
fix:
content:
- "(isinstance(v, dict) for v in nums)"
content: "(isinstance(v, dict) for v in nums)"
location:
row: 11
column: 4
@@ -183,8 +174,7 @@ expression: diagnostics
row: 12
column: 35
fix:
content:
- (v for v in nums)
content: (v for v in nums)
location:
row: 12
column: 13
@@ -202,8 +192,7 @@ expression: diagnostics
row: 15
column: 43
fix:
content:
- " {x % 2 == 0 for x in nums} "
content: " {x % 2 == 0 for x in nums} "
location:
row: 15
column: 7
@@ -221,8 +210,7 @@ expression: diagnostics
row: 16
column: 43
fix:
content:
- " {v: v**2 for v in nums} "
content: " {v: v**2 for v in nums} "
location:
row: 16
column: 7

View File

@@ -6,11 +6,12 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::registry::Rule;
use crate::settings;
use crate::test::test_path;
use crate::{assert_yaml_snapshot, settings};
#[test_case(Rule::CallDatetimeWithoutTzinfo, Path::new("DTZ001.py"); "DTZ001")]
#[test_case(Rule::CallDatetimeToday, Path::new("DTZ002.py"); "DTZ002")]

View File

@@ -7,11 +7,12 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::registry::Rule;
use crate::settings;
use crate::test::test_path;
use crate::{assert_yaml_snapshot, settings};
#[test_case(Rule::Debugger, Path::new("T100.py"); "T100")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {

View File

@@ -6,11 +6,12 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::registry::Rule;
use crate::settings;
use crate::test::test_path;
use crate::{assert_yaml_snapshot, settings};
#[test_case(Rule::NullableModelStringField, Path::new("DJ001.py"); "DJ001")]
#[test_case(Rule::ModelWithoutDunderStr, Path::new("DJ008.py"); "DJ008")]

View File

@@ -7,10 +7,11 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use crate::registry::Rule;
use crate::settings;
use crate::test::test_path;
use crate::{assert_yaml_snapshot, settings};
#[test]
fn defaults() -> Result<()> {

View File

@@ -8,11 +8,12 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::registry::Rule;
use crate::settings;
use crate::test::test_path;
use crate::{assert_yaml_snapshot, settings};
#[test_case(Path::new("EXE001_1.py"); "EXE001_1")]
#[test_case(Path::new("EXE001_2.py"); "EXE001_2")]

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_executable/mod.rs
source: crates/ruff/src/rules/flake8_executable/mod.rs
expression: diagnostics
---
- kind:
@@ -11,8 +11,7 @@ expression: diagnostics
row: 1
column: 4
fix:
content:
- ""
content: ""
location:
row: 1
column: 0

View File

@@ -7,11 +7,12 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::registry::Rule;
use crate::settings;
use crate::test::test_path;
use crate::{assert_yaml_snapshot, settings};
#[test_case(Rule::SingleLineImplicitStringConcatenation, Path::new("ISC.py"); "ISC001")]
#[test_case(Rule::MultiLineImplicitStringConcatenation, Path::new("ISC.py"); "ISC002")]

View File

@@ -7,9 +7,9 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use rustc_hash::FxHashMap;
use crate::assert_yaml_snapshot;
use crate::registry::Rule;
use crate::settings::Settings;
use crate::test::test_path;

View File

@@ -6,9 +6,9 @@ mod tests {
use std::path::{Path, PathBuf};
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::assert_yaml_snapshot;
use crate::registry::Rule;
use crate::settings::Settings;
use crate::test::{test_path, test_resource_path};

View File

@@ -6,11 +6,12 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use crate::registry::Rule;
use crate::settings;
use crate::test::test_path;
use crate::{assert_yaml_snapshot, settings};
#[test_case(Rule::DupeClassFieldDefinitions, Path::new("PIE794.py"); "PIE794")]
#[test_case(Rule::UnnecessaryDictKwargs, Path::new("PIE804.py"); "PIE804")]

View File

@@ -11,8 +11,7 @@ expression: diagnostics
row: 4
column: 8
fix:
content:
- ""
content: ""
location:
row: 4
column: 0
@@ -29,8 +28,7 @@ expression: diagnostics
row: 9
column: 8
fix:
content:
- ""
content: ""
location:
row: 9
column: 0
@@ -47,8 +45,7 @@ expression: diagnostics
row: 14
column: 8
fix:
content:
- ""
content: ""
location:
row: 14
column: 4
@@ -65,8 +62,7 @@ expression: diagnostics
row: 21
column: 8
fix:
content:
- ""
content: ""
location:
row: 21
column: 0
@@ -83,8 +79,7 @@ expression: diagnostics
row: 28
column: 8
fix:
content:
- ""
content: ""
location:
row: 28
column: 0
@@ -101,8 +96,7 @@ expression: diagnostics
row: 35
column: 8
fix:
content:
- ""
content: ""
location:
row: 35
column: 0
@@ -119,8 +113,7 @@ expression: diagnostics
row: 42
column: 8
fix:
content:
- ""
content: ""
location:
row: 42
column: 0
@@ -137,8 +130,7 @@ expression: diagnostics
row: 50
column: 8
fix:
content:
- ""
content: ""
location:
row: 50
column: 0
@@ -155,8 +147,7 @@ expression: diagnostics
row: 58
column: 8
fix:
content:
- ""
content: ""
location:
row: 58
column: 0
@@ -173,8 +164,7 @@ expression: diagnostics
row: 65
column: 8
fix:
content:
- ""
content: ""
location:
row: 65
column: 0
@@ -191,8 +181,7 @@ expression: diagnostics
row: 74
column: 8
fix:
content:
- ""
content: ""
location:
row: 74
column: 0
@@ -209,8 +198,7 @@ expression: diagnostics
row: 79
column: 8
fix:
content:
- ""
content: ""
location:
row: 79
column: 0
@@ -227,8 +215,7 @@ expression: diagnostics
row: 83
column: 8
fix:
content:
- ""
content: ""
location:
row: 83
column: 0
@@ -245,8 +232,7 @@ expression: diagnostics
row: 87
column: 8
fix:
content:
- ""
content: ""
location:
row: 87
column: 0
@@ -263,8 +249,7 @@ expression: diagnostics
row: 92
column: 8
fix:
content:
- ""
content: ""
location:
row: 92
column: 0
@@ -281,8 +266,7 @@ expression: diagnostics
row: 96
column: 8
fix:
content:
- ""
content: ""
location:
row: 96
column: 0
@@ -299,8 +283,7 @@ expression: diagnostics
row: 101
column: 8
fix:
content:
- ""
content: ""
location:
row: 101
column: 4

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_pie/mod.rs
source: crates/ruff/src/rules/flake8_pie/mod.rs
expression: diagnostics
---
- kind:
@@ -11,8 +11,7 @@ expression: diagnostics
row: 4
column: 24
fix:
content:
- ""
content: ""
location:
row: 4
column: 0
@@ -29,8 +28,7 @@ expression: diagnostics
row: 13
column: 24
fix:
content:
- ""
content: ""
location:
row: 13
column: 0
@@ -47,8 +45,7 @@ expression: diagnostics
row: 23
column: 23
fix:
content:
- ""
content: ""
location:
row: 23
column: 0
@@ -65,8 +62,7 @@ expression: diagnostics
row: 40
column: 23
fix:
content:
- ""
content: ""
location:
row: 40
column: 0

Some files were not shown because too many files have changed in this diff Show More