mirror of https://github.com/astral-sh/ruff
Treat `not` operations as boolean tests (#12301)
## Summary Closes https://github.com/astral-sh/ruff/issues/12285.
This commit is contained in:
parent
6febd96dfe
commit
4e6ecb2348
|
|
@ -13,6 +13,10 @@ if (k) in d and d[k]:
|
||||||
if k in d and d[(k)]:
|
if k in d and d[(k)]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
not ("key" in dct and dct["key"])
|
||||||
|
|
||||||
|
bool("key" in dct and dct["key"])
|
||||||
|
|
||||||
# OK
|
# OK
|
||||||
v = "k" in d and d["k"]
|
v = "k" in d and d["k"]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1140,6 +1140,13 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||||
self.visit_expr(body);
|
self.visit_expr(body);
|
||||||
self.visit_expr(orelse);
|
self.visit_expr(orelse);
|
||||||
}
|
}
|
||||||
|
Expr::UnaryOp(ast::ExprUnaryOp {
|
||||||
|
op: UnaryOp::Not,
|
||||||
|
operand,
|
||||||
|
range: _,
|
||||||
|
}) => {
|
||||||
|
self.visit_boolean_test(operand);
|
||||||
|
}
|
||||||
Expr::Call(ast::ExprCall {
|
Expr::Call(ast::ExprCall {
|
||||||
func,
|
func,
|
||||||
arguments,
|
arguments,
|
||||||
|
|
|
||||||
|
|
@ -77,6 +77,46 @@ RUF019.py:13:4: RUF019 [*] Unnecessary key check before dictionary access
|
||||||
13 |+if d.get((k)):
|
13 |+if d.get((k)):
|
||||||
14 14 | pass
|
14 14 | pass
|
||||||
15 15 |
|
15 15 |
|
||||||
16 16 | # OK
|
16 16 | not ("key" in dct and dct["key"])
|
||||||
|
|
||||||
|
RUF019.py:16:6: RUF019 [*] Unnecessary key check before dictionary access
|
||||||
|
|
|
||||||
|
14 | pass
|
||||||
|
15 |
|
||||||
|
16 | not ("key" in dct and dct["key"])
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF019
|
||||||
|
17 |
|
||||||
|
18 | bool("key" in dct and dct["key"])
|
||||||
|
|
|
||||||
|
= help: Replace with `dict.get`
|
||||||
|
|
||||||
|
ℹ Safe fix
|
||||||
|
13 13 | if k in d and d[(k)]:
|
||||||
|
14 14 | pass
|
||||||
|
15 15 |
|
||||||
|
16 |-not ("key" in dct and dct["key"])
|
||||||
|
16 |+not (dct.get("key"))
|
||||||
|
17 17 |
|
||||||
|
18 18 | bool("key" in dct and dct["key"])
|
||||||
|
19 19 |
|
||||||
|
|
||||||
|
RUF019.py:18:6: RUF019 [*] Unnecessary key check before dictionary access
|
||||||
|
|
|
||||||
|
16 | not ("key" in dct and dct["key"])
|
||||||
|
17 |
|
||||||
|
18 | bool("key" in dct and dct["key"])
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF019
|
||||||
|
19 |
|
||||||
|
20 | # OK
|
||||||
|
|
|
||||||
|
= help: Replace with `dict.get`
|
||||||
|
|
||||||
|
ℹ Safe fix
|
||||||
|
15 15 |
|
||||||
|
16 16 | not ("key" in dct and dct["key"])
|
||||||
|
17 17 |
|
||||||
|
18 |-bool("key" in dct and dct["key"])
|
||||||
|
18 |+bool(dct.get("key"))
|
||||||
|
19 19 |
|
||||||
|
20 20 | # OK
|
||||||
|
21 21 | v = "k" in d and d["k"]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue