Skip to content

Commit

Permalink
[pylint] Reverse min-max logic in if-stmt-min-max (#10890)
Browse files Browse the repository at this point in the history
Closes #10889.
  • Loading branch information
charliermarsh authored Apr 11, 2024
1 parent 9b9098c commit e7d1d43
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 49 deletions.
14 changes: 4 additions & 10 deletions crates/ruff_linter/src/rules/pylint/rules/if_stmt_min_max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,17 @@ 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,
};

let [right] = &**comparators else {
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -10,20 +10,20 @@ 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
6 6 |
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 |
Expand All @@ -33,20 +33,20 @@ 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]
9 9 | value = 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 |
Expand All @@ -56,20 +56,20 @@ 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]
12 12 | value = 10
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 |
Expand All @@ -79,20 +79,20 @@ 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]
15 15 | value = value2
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 |
Expand All @@ -102,41 +102,41 @@ 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]
18 18 | value = 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 |
23 | / if value > value2: # [min-instead-of-if]
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]
21 21 | value = 10
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]
Expand All @@ -145,41 +145,41 @@ 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 |
31 31 |
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 |
36 | / if A1.value > 10: # [min-instead-of-if]
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]
34 34 | A1.value = 10
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 |
Expand All @@ -189,20 +189,20 @@ 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)
58 58 | A2 = AA(3)
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 |
Expand All @@ -212,20 +212,20 @@ 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]
61 61 | A2 = A1
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 |
Expand All @@ -235,20 +235,20 @@ 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]
64 64 | A2 = A1
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 |
Expand All @@ -258,20 +258,20 @@ 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]
67 67 | A2 = A1
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:
Expand All @@ -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
Expand All @@ -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)

0 comments on commit e7d1d43

Please sign in to comment.