mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:21 +00:00
[flake8-simplify
] Simplify double negatives in SIM103
(#11684)
## Summary Closes: https://github.com/astral-sh/ruff/issues/11685.
This commit is contained in:
parent
fd9d68051e
commit
b36dd1aa51
3 changed files with 71 additions and 4 deletions
|
@ -111,3 +111,15 @@ def f():
|
||||||
if a:
|
if a:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
if not 10 < a:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
if 10 < a:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
|
@ -202,11 +202,20 @@ pub(crate) fn needless_bool(checker: &mut Checker, stmt: &Stmt) {
|
||||||
} else {
|
} else {
|
||||||
// If the return values are inverted, wrap the condition in a `not`.
|
// If the return values are inverted, wrap the condition in a `not`.
|
||||||
if inverted {
|
if inverted {
|
||||||
|
if let Expr::UnaryOp(ast::ExprUnaryOp {
|
||||||
|
op: ast::UnaryOp::Not,
|
||||||
|
operand,
|
||||||
|
..
|
||||||
|
}) = if_test
|
||||||
|
{
|
||||||
|
Some((**operand).clone())
|
||||||
|
} else {
|
||||||
Some(Expr::UnaryOp(ast::ExprUnaryOp {
|
Some(Expr::UnaryOp(ast::ExprUnaryOp {
|
||||||
op: ast::UnaryOp::Not,
|
op: ast::UnaryOp::Not,
|
||||||
operand: Box::new(if_test.clone()),
|
operand: Box::new(if_test.clone()),
|
||||||
range: TextRange::default(),
|
range: TextRange::default(),
|
||||||
}))
|
}))
|
||||||
|
}
|
||||||
} else if if_test.is_compare_expr() {
|
} else if if_test.is_compare_expr() {
|
||||||
// If the condition is a comparison, we can replace it with the condition, since we
|
// If the condition is a comparison, we can replace it with the condition, since we
|
||||||
// know it's a boolean.
|
// know it's a boolean.
|
||||||
|
|
|
@ -213,3 +213,49 @@ SIM103.py:111:5: SIM103 [*] Return the condition `not a` directly
|
||||||
112 |- return False
|
112 |- return False
|
||||||
113 |- return True
|
113 |- return True
|
||||||
111 |+ return not a
|
111 |+ return not a
|
||||||
|
114 112 |
|
||||||
|
115 113 |
|
||||||
|
116 114 | def f():
|
||||||
|
|
||||||
|
SIM103.py:117:5: SIM103 [*] Return the condition `10 < a` directly
|
||||||
|
|
|
||||||
|
116 | def f():
|
||||||
|
117 | if not 10 < a:
|
||||||
|
| _____^
|
||||||
|
118 | | return False
|
||||||
|
119 | | return True
|
||||||
|
| |_______________^ SIM103
|
||||||
|
|
|
||||||
|
= help: Replace with `return 10 < a`
|
||||||
|
|
||||||
|
ℹ Unsafe fix
|
||||||
|
114 114 |
|
||||||
|
115 115 |
|
||||||
|
116 116 | def f():
|
||||||
|
117 |- if not 10 < a:
|
||||||
|
118 |- return False
|
||||||
|
119 |- return True
|
||||||
|
117 |+ return 10 < a
|
||||||
|
120 118 |
|
||||||
|
121 119 |
|
||||||
|
122 120 | def f():
|
||||||
|
|
||||||
|
SIM103.py:123:5: SIM103 [*] Return the condition `not 10 < a` directly
|
||||||
|
|
|
||||||
|
122 | def f():
|
||||||
|
123 | if 10 < a:
|
||||||
|
| _____^
|
||||||
|
124 | | return False
|
||||||
|
125 | | return True
|
||||||
|
| |_______________^ SIM103
|
||||||
|
|
|
||||||
|
= help: Replace with `return not 10 < a`
|
||||||
|
|
||||||
|
ℹ Unsafe fix
|
||||||
|
120 120 |
|
||||||
|
121 121 |
|
||||||
|
122 122 | def f():
|
||||||
|
123 |- if 10 < a:
|
||||||
|
124 |- return False
|
||||||
|
125 |- return True
|
||||||
|
123 |+ return not 10 < a
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue