mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-01 20:30:49 +00:00
[pylint] Reverse min-max logic in if-stmt-min-max (#10890)
Closes https://github.com/astral-sh/ruff/issues/10889.
This commit is contained in:
parent
9b9098c3dc
commit
e7d1d43f39
2 changed files with 43 additions and 49 deletions
|
|
@ -110,10 +110,10 @@ pub(crate) fn if_stmt_min_max(checker: &mut Checker, stmt_if: &ast::StmtIf) {
|
|||
// Determine whether to use `min()` or `max()`, and whether to flip the
|
||||
// order of the arguments, which is relevant for breaking ties.
|
||||
let (min_max, flip_args) = match op {
|
||||
CmpOp::Gt => (MinMax::Max, true),
|
||||
CmpOp::GtE => (MinMax::Max, false),
|
||||
CmpOp::Lt => (MinMax::Min, true),
|
||||
CmpOp::LtE => (MinMax::Min, false),
|
||||
CmpOp::Gt => (MinMax::Min, true),
|
||||
CmpOp::GtE => (MinMax::Min, false),
|
||||
CmpOp::Lt => (MinMax::Max, true),
|
||||
CmpOp::LtE => (MinMax::Max, false),
|
||||
_ => return,
|
||||
};
|
||||
|
||||
|
|
@ -121,12 +121,6 @@ pub(crate) fn if_stmt_min_max(checker: &mut Checker, stmt_if: &ast::StmtIf) {
|
|||
return;
|
||||
};
|
||||
|
||||
let _min_or_max = match op {
|
||||
CmpOp::Gt | CmpOp::GtE => MinMax::Min,
|
||||
CmpOp::Lt | CmpOp::LtE => MinMax::Max,
|
||||
_ => return,
|
||||
};
|
||||
|
||||
let left_cmp = ComparableExpr::from(left);
|
||||
let body_target_cmp = ComparableExpr::from(body_target);
|
||||
let right_statement_cmp = ComparableExpr::from(right);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
source: crates/ruff_linter/src/rules/pylint/mod.rs
|
||||
---
|
||||
if_stmt_min_max.py:8:1: PLR1730 [*] Replace `if` statement with `value = min(value, 10)`
|
||||
if_stmt_min_max.py:8:1: PLR1730 [*] Replace `if` statement with `value = max(value, 10)`
|
||||
|
|
||||
7 | # Positive
|
||||
8 | / if value < 10: # [max-instead-of-if]
|
||||
|
|
@ -10,7 +10,7 @@ if_stmt_min_max.py:8:1: PLR1730 [*] Replace `if` statement with `value = min(val
|
|||
10 |
|
||||
11 | if value <= 10: # [max-instead-of-if]
|
||||
|
|
||||
= help: Replace with `value = min(value, 10)`
|
||||
= help: Replace with `value = max(value, 10)`
|
||||
|
||||
ℹ Safe fix
|
||||
5 5 | value3 = 3
|
||||
|
|
@ -18,12 +18,12 @@ if_stmt_min_max.py:8:1: PLR1730 [*] Replace `if` statement with `value = min(val
|
|||
7 7 | # Positive
|
||||
8 |-if value < 10: # [max-instead-of-if]
|
||||
9 |- value = 10
|
||||
8 |+value = min(value, 10)
|
||||
8 |+value = max(value, 10)
|
||||
10 9 |
|
||||
11 10 | if value <= 10: # [max-instead-of-if]
|
||||
12 11 | value = 10
|
||||
|
||||
if_stmt_min_max.py:11:1: PLR1730 [*] Replace `if` statement with `value = min(10, value)`
|
||||
if_stmt_min_max.py:11:1: PLR1730 [*] Replace `if` statement with `value = max(10, value)`
|
||||
|
|
||||
9 | value = 10
|
||||
10 |
|
||||
|
|
@ -33,7 +33,7 @@ if_stmt_min_max.py:11:1: PLR1730 [*] Replace `if` statement with `value = min(10
|
|||
13 |
|
||||
14 | if value < value2: # [max-instead-of-if]
|
||||
|
|
||||
= help: Replace with `value = min(10, value)`
|
||||
= help: Replace with `value = max(10, value)`
|
||||
|
||||
ℹ Safe fix
|
||||
8 8 | if value < 10: # [max-instead-of-if]
|
||||
|
|
@ -41,12 +41,12 @@ if_stmt_min_max.py:11:1: PLR1730 [*] Replace `if` statement with `value = min(10
|
|||
10 10 |
|
||||
11 |-if value <= 10: # [max-instead-of-if]
|
||||
12 |- value = 10
|
||||
11 |+value = min(10, value)
|
||||
11 |+value = max(10, value)
|
||||
13 12 |
|
||||
14 13 | if value < value2: # [max-instead-of-if]
|
||||
15 14 | value = value2
|
||||
|
||||
if_stmt_min_max.py:14:1: PLR1730 [*] Replace `if` statement with `value = min(value, value2)`
|
||||
if_stmt_min_max.py:14:1: PLR1730 [*] Replace `if` statement with `value = max(value, value2)`
|
||||
|
|
||||
12 | value = 10
|
||||
13 |
|
||||
|
|
@ -56,7 +56,7 @@ if_stmt_min_max.py:14:1: PLR1730 [*] Replace `if` statement with `value = min(va
|
|||
16 |
|
||||
17 | if value > 10: # [min-instead-of-if]
|
||||
|
|
||||
= help: Replace with `value = min(value, value2)`
|
||||
= help: Replace with `value = max(value, value2)`
|
||||
|
||||
ℹ Safe fix
|
||||
11 11 | if value <= 10: # [max-instead-of-if]
|
||||
|
|
@ -64,12 +64,12 @@ if_stmt_min_max.py:14:1: PLR1730 [*] Replace `if` statement with `value = min(va
|
|||
13 13 |
|
||||
14 |-if value < value2: # [max-instead-of-if]
|
||||
15 |- value = value2
|
||||
14 |+value = min(value, value2)
|
||||
14 |+value = max(value, value2)
|
||||
16 15 |
|
||||
17 16 | if value > 10: # [min-instead-of-if]
|
||||
18 17 | value = 10
|
||||
|
||||
if_stmt_min_max.py:17:1: PLR1730 [*] Replace `if` statement with `value = max(value, 10)`
|
||||
if_stmt_min_max.py:17:1: PLR1730 [*] Replace `if` statement with `value = min(value, 10)`
|
||||
|
|
||||
15 | value = value2
|
||||
16 |
|
||||
|
|
@ -79,7 +79,7 @@ if_stmt_min_max.py:17:1: PLR1730 [*] Replace `if` statement with `value = max(va
|
|||
19 |
|
||||
20 | if value >= 10: # [min-instead-of-if]
|
||||
|
|
||||
= help: Replace with `value = max(value, 10)`
|
||||
= help: Replace with `value = min(value, 10)`
|
||||
|
||||
ℹ Safe fix
|
||||
14 14 | if value < value2: # [max-instead-of-if]
|
||||
|
|
@ -87,12 +87,12 @@ if_stmt_min_max.py:17:1: PLR1730 [*] Replace `if` statement with `value = max(va
|
|||
16 16 |
|
||||
17 |-if value > 10: # [min-instead-of-if]
|
||||
18 |- value = 10
|
||||
17 |+value = max(value, 10)
|
||||
17 |+value = min(value, 10)
|
||||
19 18 |
|
||||
20 19 | if value >= 10: # [min-instead-of-if]
|
||||
21 20 | value = 10
|
||||
|
||||
if_stmt_min_max.py:20:1: PLR1730 [*] Replace `if` statement with `value = max(10, value)`
|
||||
if_stmt_min_max.py:20:1: PLR1730 [*] Replace `if` statement with `value = min(10, value)`
|
||||
|
|
||||
18 | value = 10
|
||||
19 |
|
||||
|
|
@ -102,7 +102,7 @@ if_stmt_min_max.py:20:1: PLR1730 [*] Replace `if` statement with `value = max(10
|
|||
22 |
|
||||
23 | if value > value2: # [min-instead-of-if]
|
||||
|
|
||||
= help: Replace with `value = max(10, value)`
|
||||
= help: Replace with `value = min(10, value)`
|
||||
|
||||
ℹ Safe fix
|
||||
17 17 | if value > 10: # [min-instead-of-if]
|
||||
|
|
@ -110,12 +110,12 @@ if_stmt_min_max.py:20:1: PLR1730 [*] Replace `if` statement with `value = max(10
|
|||
19 19 |
|
||||
20 |-if value >= 10: # [min-instead-of-if]
|
||||
21 |- value = 10
|
||||
20 |+value = max(10, value)
|
||||
20 |+value = min(10, value)
|
||||
22 21 |
|
||||
23 22 | if value > value2: # [min-instead-of-if]
|
||||
24 23 | value = value2
|
||||
|
||||
if_stmt_min_max.py:23:1: PLR1730 [*] Replace `if` statement with `value = max(value, value2)`
|
||||
if_stmt_min_max.py:23:1: PLR1730 [*] Replace `if` statement with `value = min(value, value2)`
|
||||
|
|
||||
21 | value = 10
|
||||
22 |
|
||||
|
|
@ -123,7 +123,7 @@ if_stmt_min_max.py:23:1: PLR1730 [*] Replace `if` statement with `value = max(va
|
|||
24 | | value = value2
|
||||
| |__________________^ PLR1730
|
||||
|
|
||||
= help: Replace with `value = max(value, value2)`
|
||||
= help: Replace with `value = min(value, value2)`
|
||||
|
||||
ℹ Safe fix
|
||||
20 20 | if value >= 10: # [min-instead-of-if]
|
||||
|
|
@ -131,12 +131,12 @@ if_stmt_min_max.py:23:1: PLR1730 [*] Replace `if` statement with `value = max(va
|
|||
22 22 |
|
||||
23 |-if value > value2: # [min-instead-of-if]
|
||||
24 |- value = value2
|
||||
23 |+value = max(value, value2)
|
||||
23 |+value = min(value, value2)
|
||||
25 24 |
|
||||
26 25 |
|
||||
27 26 | class A:
|
||||
|
||||
if_stmt_min_max.py:33:1: PLR1730 [*] Replace `if` statement with `A1.value = min(A1.value, 10)`
|
||||
if_stmt_min_max.py:33:1: PLR1730 [*] Replace `if` statement with `A1.value = max(A1.value, 10)`
|
||||
|
|
||||
32 | A1 = A()
|
||||
33 | / if A1.value < 10: # [max-instead-of-if]
|
||||
|
|
@ -145,7 +145,7 @@ if_stmt_min_max.py:33:1: PLR1730 [*] Replace `if` statement with `A1.value = min
|
|||
35 |
|
||||
36 | if A1.value > 10: # [min-instead-of-if]
|
||||
|
|
||||
= help: Replace with `A1.value = min(A1.value, 10)`
|
||||
= help: Replace with `A1.value = max(A1.value, 10)`
|
||||
|
||||
ℹ Safe fix
|
||||
30 30 |
|
||||
|
|
@ -153,12 +153,12 @@ if_stmt_min_max.py:33:1: PLR1730 [*] Replace `if` statement with `A1.value = min
|
|||
32 32 | A1 = A()
|
||||
33 |-if A1.value < 10: # [max-instead-of-if]
|
||||
34 |- A1.value = 10
|
||||
33 |+A1.value = min(A1.value, 10)
|
||||
33 |+A1.value = max(A1.value, 10)
|
||||
35 34 |
|
||||
36 35 | if A1.value > 10: # [min-instead-of-if]
|
||||
37 36 | A1.value = 10
|
||||
|
||||
if_stmt_min_max.py:36:1: PLR1730 [*] Replace `if` statement with `A1.value = max(A1.value, 10)`
|
||||
if_stmt_min_max.py:36:1: PLR1730 [*] Replace `if` statement with `A1.value = min(A1.value, 10)`
|
||||
|
|
||||
34 | A1.value = 10
|
||||
35 |
|
||||
|
|
@ -166,7 +166,7 @@ if_stmt_min_max.py:36:1: PLR1730 [*] Replace `if` statement with `A1.value = max
|
|||
37 | | A1.value = 10
|
||||
| |_________________^ PLR1730
|
||||
|
|
||||
= help: Replace with `A1.value = max(A1.value, 10)`
|
||||
= help: Replace with `A1.value = min(A1.value, 10)`
|
||||
|
||||
ℹ Safe fix
|
||||
33 33 | if A1.value < 10: # [max-instead-of-if]
|
||||
|
|
@ -174,12 +174,12 @@ if_stmt_min_max.py:36:1: PLR1730 [*] Replace `if` statement with `A1.value = max
|
|||
35 35 |
|
||||
36 |-if A1.value > 10: # [min-instead-of-if]
|
||||
37 |- A1.value = 10
|
||||
36 |+A1.value = max(A1.value, 10)
|
||||
36 |+A1.value = min(A1.value, 10)
|
||||
38 37 |
|
||||
39 38 |
|
||||
40 39 | class AA:
|
||||
|
||||
if_stmt_min_max.py:60:1: PLR1730 [*] Replace `if` statement with `A2 = min(A2, A1)`
|
||||
if_stmt_min_max.py:60:1: PLR1730 [*] Replace `if` statement with `A2 = max(A2, A1)`
|
||||
|
|
||||
58 | A2 = AA(3)
|
||||
59 |
|
||||
|
|
@ -189,7 +189,7 @@ if_stmt_min_max.py:60:1: PLR1730 [*] Replace `if` statement with `A2 = min(A2, A
|
|||
62 |
|
||||
63 | if A2 <= A1: # [max-instead-of-if]
|
||||
|
|
||||
= help: Replace with `A2 = min(A2, A1)`
|
||||
= help: Replace with `A2 = max(A2, A1)`
|
||||
|
||||
ℹ Safe fix
|
||||
57 57 | A1 = AA(0)
|
||||
|
|
@ -197,12 +197,12 @@ if_stmt_min_max.py:60:1: PLR1730 [*] Replace `if` statement with `A2 = min(A2, A
|
|||
59 59 |
|
||||
60 |-if A2 < A1: # [max-instead-of-if]
|
||||
61 |- A2 = A1
|
||||
60 |+A2 = min(A2, A1)
|
||||
60 |+A2 = max(A2, A1)
|
||||
62 61 |
|
||||
63 62 | if A2 <= A1: # [max-instead-of-if]
|
||||
64 63 | A2 = A1
|
||||
|
||||
if_stmt_min_max.py:63:1: PLR1730 [*] Replace `if` statement with `A2 = min(A1, A2)`
|
||||
if_stmt_min_max.py:63:1: PLR1730 [*] Replace `if` statement with `A2 = max(A1, A2)`
|
||||
|
|
||||
61 | A2 = A1
|
||||
62 |
|
||||
|
|
@ -212,7 +212,7 @@ if_stmt_min_max.py:63:1: PLR1730 [*] Replace `if` statement with `A2 = min(A1, A
|
|||
65 |
|
||||
66 | if A2 > A1: # [min-instead-of-if]
|
||||
|
|
||||
= help: Replace with `A2 = min(A1, A2)`
|
||||
= help: Replace with `A2 = max(A1, A2)`
|
||||
|
||||
ℹ Safe fix
|
||||
60 60 | if A2 < A1: # [max-instead-of-if]
|
||||
|
|
@ -220,12 +220,12 @@ if_stmt_min_max.py:63:1: PLR1730 [*] Replace `if` statement with `A2 = min(A1, A
|
|||
62 62 |
|
||||
63 |-if A2 <= A1: # [max-instead-of-if]
|
||||
64 |- A2 = A1
|
||||
63 |+A2 = min(A1, A2)
|
||||
63 |+A2 = max(A1, A2)
|
||||
65 64 |
|
||||
66 65 | if A2 > A1: # [min-instead-of-if]
|
||||
67 66 | A2 = A1
|
||||
|
||||
if_stmt_min_max.py:66:1: PLR1730 [*] Replace `if` statement with `A2 = max(A2, A1)`
|
||||
if_stmt_min_max.py:66:1: PLR1730 [*] Replace `if` statement with `A2 = min(A2, A1)`
|
||||
|
|
||||
64 | A2 = A1
|
||||
65 |
|
||||
|
|
@ -235,7 +235,7 @@ if_stmt_min_max.py:66:1: PLR1730 [*] Replace `if` statement with `A2 = max(A2, A
|
|||
68 |
|
||||
69 | if A2 >= A1: # [min-instead-of-if]
|
||||
|
|
||||
= help: Replace with `A2 = max(A2, A1)`
|
||||
= help: Replace with `A2 = min(A2, A1)`
|
||||
|
||||
ℹ Safe fix
|
||||
63 63 | if A2 <= A1: # [max-instead-of-if]
|
||||
|
|
@ -243,12 +243,12 @@ if_stmt_min_max.py:66:1: PLR1730 [*] Replace `if` statement with `A2 = max(A2, A
|
|||
65 65 |
|
||||
66 |-if A2 > A1: # [min-instead-of-if]
|
||||
67 |- A2 = A1
|
||||
66 |+A2 = max(A2, A1)
|
||||
66 |+A2 = min(A2, A1)
|
||||
68 67 |
|
||||
69 68 | if A2 >= A1: # [min-instead-of-if]
|
||||
70 69 | A2 = A1
|
||||
|
||||
if_stmt_min_max.py:69:1: PLR1730 [*] Replace `if` statement with `A2 = max(A1, A2)`
|
||||
if_stmt_min_max.py:69:1: PLR1730 [*] Replace `if` statement with `A2 = min(A1, A2)`
|
||||
|
|
||||
67 | A2 = A1
|
||||
68 |
|
||||
|
|
@ -258,7 +258,7 @@ if_stmt_min_max.py:69:1: PLR1730 [*] Replace `if` statement with `A2 = max(A1, A
|
|||
71 |
|
||||
72 | # Negative
|
||||
|
|
||||
= help: Replace with `A2 = max(A1, A2)`
|
||||
= help: Replace with `A2 = min(A1, A2)`
|
||||
|
||||
ℹ Safe fix
|
||||
66 66 | if A2 > A1: # [min-instead-of-if]
|
||||
|
|
@ -266,12 +266,12 @@ if_stmt_min_max.py:69:1: PLR1730 [*] Replace `if` statement with `A2 = max(A1, A
|
|||
68 68 |
|
||||
69 |-if A2 >= A1: # [min-instead-of-if]
|
||||
70 |- A2 = A1
|
||||
69 |+A2 = max(A1, A2)
|
||||
69 |+A2 = min(A1, A2)
|
||||
71 70 |
|
||||
72 71 | # Negative
|
||||
73 72 | if value < 10:
|
||||
|
||||
if_stmt_min_max.py:132:1: PLR1730 [*] Replace `if` statement with `max` call
|
||||
if_stmt_min_max.py:132:1: PLR1730 [*] Replace `if` statement with `min` call
|
||||
|
|
||||
131 | # Parenthesized expressions
|
||||
132 | / if value.attr > 3:
|
||||
|
|
@ -281,7 +281,7 @@ if_stmt_min_max.py:132:1: PLR1730 [*] Replace `if` statement with `max` call
|
|||
136 | | ) = 3
|
||||
| |_________^ PLR1730
|
||||
|
|
||||
= help: Replace with `max` call
|
||||
= help: Replace with `min` call
|
||||
|
||||
ℹ Safe fix
|
||||
129 129 | value = 2
|
||||
|
|
@ -293,4 +293,4 @@ if_stmt_min_max.py:132:1: PLR1730 [*] Replace `if` statement with `max` call
|
|||
134 133 | value.
|
||||
135 134 | attr
|
||||
136 |- ) = 3
|
||||
135 |+ ) = max(value.attr, 3)
|
||||
135 |+ ) = min(value.attr, 3)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue