Compare commits

...

19 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
409 changed files with 4195 additions and 4247 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

@@ -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

14
Cargo.lock generated
View File

@@ -753,7 +753,7 @@ checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "flake8-to-ruff"
version = "0.0.249"
version = "0.0.250"
dependencies = [
"anyhow",
"clap 4.1.6",
@@ -1927,7 +1927,7 @@ dependencies = [
[[package]]
name = "ruff"
version = "0.0.249"
version = "0.0.250"
dependencies = [
"anyhow",
"bisection",
@@ -1983,7 +1983,7 @@ dependencies = [
[[package]]
name = "ruff_cli"
version = "0.0.249"
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.249'
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.249'
rev: 'v0.0.250'
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]

View File

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

View File

@@ -1,6 +1,6 @@
[package]
name = "ruff"
version = "0.0.249"
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

@@ -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

@@ -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

@@ -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 { .. }
)
})
}

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

@@ -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);
}

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

@@ -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

@@ -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

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: 3
column: 53
fix:
content:
- list
content: list
location:
row: 3
column: 43
@@ -29,8 +28,7 @@ expression: diagnostics
row: 7
column: 45
fix:
content:
- list
content: list
location:
row: 7
column: 35
@@ -47,8 +45,7 @@ expression: diagnostics
row: 11
column: 37
fix:
content:
- list
content: list
location:
row: 11
column: 27

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::PrintFound, Path::new("T201.py"); "T201")]
#[test_case(Rule::PPrintFound, Path::new("T203.py"); "T203")]

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::PrefixTypeParams, Path::new("PYI001.pyi"))]
#[test_case(Rule::PrefixTypeParams, Path::new("PYI001.py"))]

View File

@@ -8,13 +8,14 @@ mod tests {
use std::path::Path;
use anyhow::Result;
use insta::assert_yaml_snapshot;
use test_case::test_case;
use super::settings::Settings;
use super::types;
use crate::registry::Rule;
use crate::settings;
use crate::test::test_path;
use crate::{assert_yaml_snapshot, settings};
#[test_case(Rule::IncorrectFixtureParenthesesStyle, Path::new("PT001.py"), Settings::default(), "PT001_default"; "PT001_0")]
#[test_case(

View File

@@ -1,17 +1,26 @@
use anyhow::bail;
use anyhow::Result;
use libcst_native::{
Assert, BooleanOp, Codegen, CodegenState, CompoundStatement, Expression,
ParenthesizableWhitespace, ParenthesizedNode, SimpleStatementLine, SimpleWhitespace,
SmallStatement, Statement, Suite, TrailingWhitespace, UnaryOp, UnaryOperation,
};
use rustpython_parser::ast::{
Boolop, Excepthandler, ExcepthandlerKind, Expr, ExprKind, Keyword, Stmt, StmtKind, Unaryop,
Boolop, Excepthandler, ExcepthandlerKind, Expr, ExprKind, Keyword, Location, Stmt, StmtKind,
Unaryop,
};
use ruff_macros::{define_violation, derive_message_formats};
use crate::ast::helpers::{create_expr, create_stmt, unparse_stmt};
use crate::ast::helpers::unparse_stmt;
use crate::ast::types::Range;
use crate::ast::visitor;
use crate::ast::visitor::Visitor;
use crate::ast::{visitor, whitespace};
use crate::checkers::ast::Checker;
use crate::cst::matchers::match_module;
use crate::fix::Fix;
use crate::registry::Diagnostic;
use crate::source_code::Stylist;
use crate::source_code::{Locator, Stylist};
use crate::violation::{AutofixKind, Availability, Violation};
use super::helpers::is_falsy_constant;
@@ -295,66 +304,130 @@ fn is_composite_condition(test: &Expr) -> CompositionKind {
}
/// Negate a condition, i.e., `a` => `not a` and `not a` => `a`.
fn negate(f: Expr) -> Expr {
match f.node {
ExprKind::UnaryOp {
op: Unaryop::Not,
operand,
} => *operand,
_ => create_expr(ExprKind::UnaryOp {
op: Unaryop::Not,
operand: Box::new(f),
}),
fn negate<'a>(expression: &Expression<'a>) -> Expression<'a> {
if let Expression::UnaryOperation(ref expression) = expression {
if matches!(expression.operator, UnaryOp::Not { .. }) {
return *expression.expression.clone();
}
}
Expression::UnaryOperation(Box::new(UnaryOperation {
operator: UnaryOp::Not {
whitespace_after: ParenthesizableWhitespace::SimpleWhitespace(SimpleWhitespace(" ")),
},
expression: Box::new(expression.clone()),
lpar: vec![],
rpar: vec![],
}))
}
/// Replace composite condition `assert a == "hello" and b == "world"` with two statements
/// `assert a == "hello"` and `assert b == "world"`.
fn fix_composite_condition(stylist: &Stylist, stmt: &Stmt, test: &Expr) -> Fix {
let mut conditions: Vec<Expr> = vec![];
match &test.node {
ExprKind::BoolOp {
op: Boolop::And,
values,
} => {
// Compound, so split.
conditions.extend(values.clone());
}
ExprKind::UnaryOp {
op: Unaryop::Not,
operand,
} => {
match &operand.node {
ExprKind::BoolOp {
op: Boolop::Or,
values,
} => {
// Split via `not (a or b)` equals `not a and not b`.
conditions.extend(values.iter().map(|f| negate(f.clone())));
}
_ => {
// Do not split.
conditions.push(*operand.clone());
fn fix_composite_condition(stmt: &Stmt, locator: &Locator, stylist: &Stylist) -> Result<Fix> {
// Infer the indentation of the outer block.
let Some(outer_indent) = whitespace::indentation(locator, stmt) else {
bail!("Unable to fix multiline statement");
};
// Extract the module text.
let contents = locator.slice(&Range::new(
Location::new(stmt.location.row(), 0),
Location::new(stmt.end_location.unwrap().row() + 1, 0),
));
// "Embed" it in a function definition, to preserve indentation while retaining valid source
// code. (We'll strip the prefix later on.)
let module_text = format!("def f():{}{contents}", stylist.line_ending().as_str());
// Parse the CST.
let mut tree = match_module(&module_text)?;
// Extract the assert statement.
let statements: &mut Vec<Statement> = {
let [Statement::Compound(CompoundStatement::FunctionDef(embedding))] = &mut *tree.body else {
bail!("Expected statement to be embedded in a function definition")
};
let Suite::IndentedBlock(indented_block) = &mut embedding.body else {
bail!("Expected indented block")
};
indented_block.indent = Some(outer_indent);
&mut indented_block.body
};
let [Statement::Simple(simple_statement_line)] = statements.as_mut_slice() else {
bail!("Expected one simple statement")
};
let [SmallStatement::Assert(assert_statement)] = &mut *simple_statement_line.body else {
bail!("Expected simple statement to be an assert")
};
if !(assert_statement.test.lpar().is_empty() && assert_statement.test.rpar().is_empty()) {
bail!("Unable to split parenthesized condition");
}
// Extract the individual conditions.
let mut conditions: Vec<Expression> = Vec::with_capacity(2);
match &assert_statement.test {
Expression::UnaryOperation(op) => {
if matches!(op.operator, UnaryOp::Not { .. }) {
if let Expression::BooleanOperation(op) = &*op.expression {
if matches!(op.operator, BooleanOp::Or { .. }) {
conditions.push(negate(&op.left));
conditions.push(negate(&op.right));
} else {
bail!("Expected assert statement to be a composite condition");
}
} else {
bail!("Expected assert statement to be a composite condition");
}
}
}
_ => {}
};
// For each condition, create an `assert condition` statement.
let mut content: Vec<String> = Vec::with_capacity(conditions.len());
for condition in conditions {
content.push(unparse_stmt(
&create_stmt(StmtKind::Assert {
test: Box::new(condition.clone()),
msg: None,
}),
stylist,
));
Expression::BooleanOperation(op) => {
if matches!(op.operator, BooleanOp::And { .. }) {
conditions.push(*op.left.clone());
conditions.push(*op.right.clone());
} else {
bail!("Expected assert statement to be a composite condition");
}
}
_ => bail!("Expected assert statement to be a composite condition"),
}
let content = content.join(stylist.line_ending().as_str());
Fix::replacement(content, stmt.location, stmt.end_location.unwrap())
// For each condition, create an `assert condition` statement.
statements.clear();
for condition in conditions {
statements.push(Statement::Simple(SimpleStatementLine {
body: vec![SmallStatement::Assert(Assert {
test: condition,
msg: None,
comma: None,
whitespace_after_assert: SimpleWhitespace(" "),
semicolon: None,
})],
leading_lines: Vec::default(),
trailing_whitespace: TrailingWhitespace::default(),
}));
}
let mut state = CodegenState {
default_newline: stylist.line_ending(),
default_indent: stylist.indentation(),
..CodegenState::default()
};
tree.codegen(&mut state);
// Reconstruct and reformat the code.
let module_text = state.to_string();
let contents = module_text
.strip_prefix(&format!("def f():{}", stylist.line_ending().as_str()))
.unwrap()
.to_string();
Ok(Fix::replacement(
contents,
Location::new(stmt.location.row(), 0),
Location::new(stmt.end_location.unwrap().row() + 1, 0),
))
}
/// PT018
@@ -365,7 +438,9 @@ pub fn composite_condition(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg:
let mut diagnostic =
Diagnostic::new(CompositeAssertion { fixable }, Range::from_located(stmt));
if fixable && checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(fix_composite_condition(checker.stylist, stmt, test));
if let Ok(fix) = fix_composite_condition(stmt, checker.locator, checker.stylist) {
diagnostic.amend(fix);
}
}
checker.diagnostics.push(diagnostic);
}

View File

@@ -114,9 +114,11 @@ pub fn complex_raises(checker: &mut Checker, stmt: &Stmt, items: &[Withitem], bo
}
StmtKind::If { .. }
| StmtKind::For { .. }
| StmtKind::Match { .. }
| StmtKind::AsyncFor { .. }
| StmtKind::While { .. }
| StmtKind::Try { .. } => {
| StmtKind::Try { .. }
| StmtKind::TryStar { .. } => {
is_too_complex = true;
}
_ => {}

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_pytest_style/mod.rs
source: crates/ruff/src/rules/flake8_pytest_style/mod.rs
expression: diagnostics
---
- kind:
@@ -13,8 +13,7 @@ expression: diagnostics
row: 9
column: 15
fix:
content:
- ()
content: ()
location:
row: 9
column: 15
@@ -33,8 +32,7 @@ expression: diagnostics
row: 34
column: 8
fix:
content:
- ()
content: ()
location:
row: 34
column: 8
@@ -53,8 +51,7 @@ expression: diagnostics
row: 59
column: 8
fix:
content:
- ()
content: ()
location:
row: 59
column: 8

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_pytest_style/mod.rs
source: crates/ruff/src/rules/flake8_pytest_style/mod.rs
expression: diagnostics
---
- kind:
@@ -13,8 +13,7 @@ expression: diagnostics
row: 14
column: 17
fix:
content:
- ""
content: ""
location:
row: 14
column: 15
@@ -33,8 +32,7 @@ expression: diagnostics
row: 26
column: 1
fix:
content:
- ""
content: ""
location:
row: 24
column: 15
@@ -53,8 +51,7 @@ expression: diagnostics
row: 39
column: 10
fix:
content:
- ""
content: ""
location:
row: 39
column: 8
@@ -73,8 +70,7 @@ expression: diagnostics
row: 51
column: 1
fix:
content:
- ""
content: ""
location:
row: 49
column: 8
@@ -93,8 +89,7 @@ expression: diagnostics
row: 64
column: 10
fix:
content:
- ""
content: ""
location:
row: 64
column: 8
@@ -113,8 +108,7 @@ expression: diagnostics
row: 76
column: 1
fix:
content:
- ""
content: ""
location:
row: 74
column: 8

View File

@@ -1,5 +1,5 @@
---
source: src/rules/flake8_pytest_style/mod.rs
source: crates/ruff/src/rules/flake8_pytest_style/mod.rs
expression: diagnostics
---
- kind:
@@ -11,8 +11,7 @@ expression: diagnostics
row: 14
column: 32
fix:
content:
- ""
content: ""
location:
row: 14
column: 16
@@ -29,8 +28,7 @@ expression: diagnostics
row: 19
column: 32
fix:
content:
- ""
content: ""
location:
row: 19
column: 16
@@ -47,8 +45,7 @@ expression: diagnostics
row: 24
column: 51
fix:
content:
- ""
content: ""
location:
row: 24
column: 33
@@ -65,8 +62,7 @@ expression: diagnostics
row: 29
column: 51
fix:
content:
- ""
content: ""
location:
row: 29
column: 35
@@ -83,8 +79,7 @@ expression: diagnostics
row: 37
column: 46
fix:
content:
- ""
content: ""
location:
row: 37
column: 28
@@ -101,8 +96,7 @@ expression: diagnostics
row: 43
column: 20
fix:
content:
- ""
content: ""
location:
row: 43
column: 4
@@ -119,8 +113,7 @@ expression: diagnostics
row: 52
column: 20
fix:
content:
- ""
content: ""
location:
row: 51
column: 21
@@ -137,8 +130,7 @@ expression: diagnostics
row: 67
column: 18
fix:
content:
- ""
content: ""
location:
row: 66
column: 4

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