Skip to content

Commit e7d1d43

Browse files
[pylint] Reverse min-max logic in if-stmt-min-max (#10890)
Closes #10889.
1 parent 9b9098c commit e7d1d43

File tree

2 files changed

+43
-49
lines changed

2 files changed

+43
-49
lines changed

crates/ruff_linter/src/rules/pylint/rules/if_stmt_min_max.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,17 @@ pub(crate) fn if_stmt_min_max(checker: &mut Checker, stmt_if: &ast::StmtIf) {
110110
// Determine whether to use `min()` or `max()`, and whether to flip the
111111
// order of the arguments, which is relevant for breaking ties.
112112
let (min_max, flip_args) = match op {
113-
CmpOp::Gt => (MinMax::Max, true),
114-
CmpOp::GtE => (MinMax::Max, false),
115-
CmpOp::Lt => (MinMax::Min, true),
116-
CmpOp::LtE => (MinMax::Min, false),
113+
CmpOp::Gt => (MinMax::Min, true),
114+
CmpOp::GtE => (MinMax::Min, false),
115+
CmpOp::Lt => (MinMax::Max, true),
116+
CmpOp::LtE => (MinMax::Max, false),
117117
_ => return,
118118
};
119119

120120
let [right] = &**comparators else {
121121
return;
122122
};
123123

124-
let _min_or_max = match op {
125-
CmpOp::Gt | CmpOp::GtE => MinMax::Min,
126-
CmpOp::Lt | CmpOp::LtE => MinMax::Max,
127-
_ => return,
128-
};
129-
130124
let left_cmp = ComparableExpr::from(left);
131125
let body_target_cmp = ComparableExpr::from(body_target);
132126
let right_statement_cmp = ComparableExpr::from(right);
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
source: crates/ruff_linter/src/rules/pylint/mod.rs
33
---
4-
if_stmt_min_max.py:8:1: PLR1730 [*] Replace `if` statement with `value = min(value, 10)`
4+
if_stmt_min_max.py:8:1: PLR1730 [*] Replace `if` statement with `value = max(value, 10)`
55
|
66
7 | # Positive
77
8 | / if value < 10: # [max-instead-of-if]
@@ -10,20 +10,20 @@ if_stmt_min_max.py:8:1: PLR1730 [*] Replace `if` statement with `value = min(val
1010
10 |
1111
11 | if value <= 10: # [max-instead-of-if]
1212
|
13-
= help: Replace with `value = min(value, 10)`
13+
= help: Replace with `value = max(value, 10)`
1414

1515
Safe fix
1616
5 5 | value3 = 3
1717
6 6 |
1818
7 7 | # Positive
1919
8 |-if value < 10: # [max-instead-of-if]
2020
9 |- value = 10
21-
8 |+value = min(value, 10)
21+
8 |+value = max(value, 10)
2222
10 9 |
2323
11 10 | if value <= 10: # [max-instead-of-if]
2424
12 11 | value = 10
2525

26-
if_stmt_min_max.py:11:1: PLR1730 [*] Replace `if` statement with `value = min(10, value)`
26+
if_stmt_min_max.py:11:1: PLR1730 [*] Replace `if` statement with `value = max(10, value)`
2727
|
2828
9 | value = 10
2929
10 |
@@ -33,20 +33,20 @@ if_stmt_min_max.py:11:1: PLR1730 [*] Replace `if` statement with `value = min(10
3333
13 |
3434
14 | if value < value2: # [max-instead-of-if]
3535
|
36-
= help: Replace with `value = min(10, value)`
36+
= help: Replace with `value = max(10, value)`
3737

3838
Safe fix
3939
8 8 | if value < 10: # [max-instead-of-if]
4040
9 9 | value = 10
4141
10 10 |
4242
11 |-if value <= 10: # [max-instead-of-if]
4343
12 |- value = 10
44-
11 |+value = min(10, value)
44+
11 |+value = max(10, value)
4545
13 12 |
4646
14 13 | if value < value2: # [max-instead-of-if]
4747
15 14 | value = value2
4848

49-
if_stmt_min_max.py:14:1: PLR1730 [*] Replace `if` statement with `value = min(value, value2)`
49+
if_stmt_min_max.py:14:1: PLR1730 [*] Replace `if` statement with `value = max(value, value2)`
5050
|
5151
12 | value = 10
5252
13 |
@@ -56,20 +56,20 @@ if_stmt_min_max.py:14:1: PLR1730 [*] Replace `if` statement with `value = min(va
5656
16 |
5757
17 | if value > 10: # [min-instead-of-if]
5858
|
59-
= help: Replace with `value = min(value, value2)`
59+
= help: Replace with `value = max(value, value2)`
6060

6161
Safe fix
6262
11 11 | if value <= 10: # [max-instead-of-if]
6363
12 12 | value = 10
6464
13 13 |
6565
14 |-if value < value2: # [max-instead-of-if]
6666
15 |- value = value2
67-
14 |+value = min(value, value2)
67+
14 |+value = max(value, value2)
6868
16 15 |
6969
17 16 | if value > 10: # [min-instead-of-if]
7070
18 17 | value = 10
7171

72-
if_stmt_min_max.py:17:1: PLR1730 [*] Replace `if` statement with `value = max(value, 10)`
72+
if_stmt_min_max.py:17:1: PLR1730 [*] Replace `if` statement with `value = min(value, 10)`
7373
|
7474
15 | value = value2
7575
16 |
@@ -79,20 +79,20 @@ if_stmt_min_max.py:17:1: PLR1730 [*] Replace `if` statement with `value = max(va
7979
19 |
8080
20 | if value >= 10: # [min-instead-of-if]
8181
|
82-
= help: Replace with `value = max(value, 10)`
82+
= help: Replace with `value = min(value, 10)`
8383

8484
Safe fix
8585
14 14 | if value < value2: # [max-instead-of-if]
8686
15 15 | value = value2
8787
16 16 |
8888
17 |-if value > 10: # [min-instead-of-if]
8989
18 |- value = 10
90-
17 |+value = max(value, 10)
90+
17 |+value = min(value, 10)
9191
19 18 |
9292
20 19 | if value >= 10: # [min-instead-of-if]
9393
21 20 | value = 10
9494

95-
if_stmt_min_max.py:20:1: PLR1730 [*] Replace `if` statement with `value = max(10, value)`
95+
if_stmt_min_max.py:20:1: PLR1730 [*] Replace `if` statement with `value = min(10, value)`
9696
|
9797
18 | value = 10
9898
19 |
@@ -102,41 +102,41 @@ if_stmt_min_max.py:20:1: PLR1730 [*] Replace `if` statement with `value = max(10
102102
22 |
103103
23 | if value > value2: # [min-instead-of-if]
104104
|
105-
= help: Replace with `value = max(10, value)`
105+
= help: Replace with `value = min(10, value)`
106106

107107
Safe fix
108108
17 17 | if value > 10: # [min-instead-of-if]
109109
18 18 | value = 10
110110
19 19 |
111111
20 |-if value >= 10: # [min-instead-of-if]
112112
21 |- value = 10
113-
20 |+value = max(10, value)
113+
20 |+value = min(10, value)
114114
22 21 |
115115
23 22 | if value > value2: # [min-instead-of-if]
116116
24 23 | value = value2
117117

118-
if_stmt_min_max.py:23:1: PLR1730 [*] Replace `if` statement with `value = max(value, value2)`
118+
if_stmt_min_max.py:23:1: PLR1730 [*] Replace `if` statement with `value = min(value, value2)`
119119
|
120120
21 | value = 10
121121
22 |
122122
23 | / if value > value2: # [min-instead-of-if]
123123
24 | | value = value2
124124
| |__________________^ PLR1730
125125
|
126-
= help: Replace with `value = max(value, value2)`
126+
= help: Replace with `value = min(value, value2)`
127127

128128
Safe fix
129129
20 20 | if value >= 10: # [min-instead-of-if]
130130
21 21 | value = 10
131131
22 22 |
132132
23 |-if value > value2: # [min-instead-of-if]
133133
24 |- value = value2
134-
23 |+value = max(value, value2)
134+
23 |+value = min(value, value2)
135135
25 24 |
136136
26 25 |
137137
27 26 | class A:
138138

139-
if_stmt_min_max.py:33:1: PLR1730 [*] Replace `if` statement with `A1.value = min(A1.value, 10)`
139+
if_stmt_min_max.py:33:1: PLR1730 [*] Replace `if` statement with `A1.value = max(A1.value, 10)`
140140
|
141141
32 | A1 = A()
142142
33 | / if A1.value < 10: # [max-instead-of-if]
@@ -145,41 +145,41 @@ if_stmt_min_max.py:33:1: PLR1730 [*] Replace `if` statement with `A1.value = min
145145
35 |
146146
36 | if A1.value > 10: # [min-instead-of-if]
147147
|
148-
= help: Replace with `A1.value = min(A1.value, 10)`
148+
= help: Replace with `A1.value = max(A1.value, 10)`
149149

150150
Safe fix
151151
30 30 |
152152
31 31 |
153153
32 32 | A1 = A()
154154
33 |-if A1.value < 10: # [max-instead-of-if]
155155
34 |- A1.value = 10
156-
33 |+A1.value = min(A1.value, 10)
156+
33 |+A1.value = max(A1.value, 10)
157157
35 34 |
158158
36 35 | if A1.value > 10: # [min-instead-of-if]
159159
37 36 | A1.value = 10
160160

161-
if_stmt_min_max.py:36:1: PLR1730 [*] Replace `if` statement with `A1.value = max(A1.value, 10)`
161+
if_stmt_min_max.py:36:1: PLR1730 [*] Replace `if` statement with `A1.value = min(A1.value, 10)`
162162
|
163163
34 | A1.value = 10
164164
35 |
165165
36 | / if A1.value > 10: # [min-instead-of-if]
166166
37 | | A1.value = 10
167167
| |_________________^ PLR1730
168168
|
169-
= help: Replace with `A1.value = max(A1.value, 10)`
169+
= help: Replace with `A1.value = min(A1.value, 10)`
170170

171171
Safe fix
172172
33 33 | if A1.value < 10: # [max-instead-of-if]
173173
34 34 | A1.value = 10
174174
35 35 |
175175
36 |-if A1.value > 10: # [min-instead-of-if]
176176
37 |- A1.value = 10
177-
36 |+A1.value = max(A1.value, 10)
177+
36 |+A1.value = min(A1.value, 10)
178178
38 37 |
179179
39 38 |
180180
40 39 | class AA:
181181

182-
if_stmt_min_max.py:60:1: PLR1730 [*] Replace `if` statement with `A2 = min(A2, A1)`
182+
if_stmt_min_max.py:60:1: PLR1730 [*] Replace `if` statement with `A2 = max(A2, A1)`
183183
|
184184
58 | A2 = AA(3)
185185
59 |
@@ -189,20 +189,20 @@ if_stmt_min_max.py:60:1: PLR1730 [*] Replace `if` statement with `A2 = min(A2, A
189189
62 |
190190
63 | if A2 <= A1: # [max-instead-of-if]
191191
|
192-
= help: Replace with `A2 = min(A2, A1)`
192+
= help: Replace with `A2 = max(A2, A1)`
193193

194194
Safe fix
195195
57 57 | A1 = AA(0)
196196
58 58 | A2 = AA(3)
197197
59 59 |
198198
60 |-if A2 < A1: # [max-instead-of-if]
199199
61 |- A2 = A1
200-
60 |+A2 = min(A2, A1)
200+
60 |+A2 = max(A2, A1)
201201
62 61 |
202202
63 62 | if A2 <= A1: # [max-instead-of-if]
203203
64 63 | A2 = A1
204204

205-
if_stmt_min_max.py:63:1: PLR1730 [*] Replace `if` statement with `A2 = min(A1, A2)`
205+
if_stmt_min_max.py:63:1: PLR1730 [*] Replace `if` statement with `A2 = max(A1, A2)`
206206
|
207207
61 | A2 = A1
208208
62 |
@@ -212,20 +212,20 @@ if_stmt_min_max.py:63:1: PLR1730 [*] Replace `if` statement with `A2 = min(A1, A
212212
65 |
213213
66 | if A2 > A1: # [min-instead-of-if]
214214
|
215-
= help: Replace with `A2 = min(A1, A2)`
215+
= help: Replace with `A2 = max(A1, A2)`
216216

217217
Safe fix
218218
60 60 | if A2 < A1: # [max-instead-of-if]
219219
61 61 | A2 = A1
220220
62 62 |
221221
63 |-if A2 <= A1: # [max-instead-of-if]
222222
64 |- A2 = A1
223-
63 |+A2 = min(A1, A2)
223+
63 |+A2 = max(A1, A2)
224224
65 64 |
225225
66 65 | if A2 > A1: # [min-instead-of-if]
226226
67 66 | A2 = A1
227227

228-
if_stmt_min_max.py:66:1: PLR1730 [*] Replace `if` statement with `A2 = max(A2, A1)`
228+
if_stmt_min_max.py:66:1: PLR1730 [*] Replace `if` statement with `A2 = min(A2, A1)`
229229
|
230230
64 | A2 = A1
231231
65 |
@@ -235,20 +235,20 @@ if_stmt_min_max.py:66:1: PLR1730 [*] Replace `if` statement with `A2 = max(A2, A
235235
68 |
236236
69 | if A2 >= A1: # [min-instead-of-if]
237237
|
238-
= help: Replace with `A2 = max(A2, A1)`
238+
= help: Replace with `A2 = min(A2, A1)`
239239

240240
Safe fix
241241
63 63 | if A2 <= A1: # [max-instead-of-if]
242242
64 64 | A2 = A1
243243
65 65 |
244244
66 |-if A2 > A1: # [min-instead-of-if]
245245
67 |- A2 = A1
246-
66 |+A2 = max(A2, A1)
246+
66 |+A2 = min(A2, A1)
247247
68 67 |
248248
69 68 | if A2 >= A1: # [min-instead-of-if]
249249
70 69 | A2 = A1
250250

251-
if_stmt_min_max.py:69:1: PLR1730 [*] Replace `if` statement with `A2 = max(A1, A2)`
251+
if_stmt_min_max.py:69:1: PLR1730 [*] Replace `if` statement with `A2 = min(A1, A2)`
252252
|
253253
67 | A2 = A1
254254
68 |
@@ -258,20 +258,20 @@ if_stmt_min_max.py:69:1: PLR1730 [*] Replace `if` statement with `A2 = max(A1, A
258258
71 |
259259
72 | # Negative
260260
|
261-
= help: Replace with `A2 = max(A1, A2)`
261+
= help: Replace with `A2 = min(A1, A2)`
262262

263263
Safe fix
264264
66 66 | if A2 > A1: # [min-instead-of-if]
265265
67 67 | A2 = A1
266266
68 68 |
267267
69 |-if A2 >= A1: # [min-instead-of-if]
268268
70 |- A2 = A1
269-
69 |+A2 = max(A1, A2)
269+
69 |+A2 = min(A1, A2)
270270
71 70 |
271271
72 71 | # Negative
272272
73 72 | if value < 10:
273273

274-
if_stmt_min_max.py:132:1: PLR1730 [*] Replace `if` statement with `max` call
274+
if_stmt_min_max.py:132:1: PLR1730 [*] Replace `if` statement with `min` call
275275
|
276276
131 | # Parenthesized expressions
277277
132 | / if value.attr > 3:
@@ -281,7 +281,7 @@ if_stmt_min_max.py:132:1: PLR1730 [*] Replace `if` statement with `max` call
281281
136 | | ) = 3
282282
| |_________^ PLR1730
283283
|
284-
= help: Replace with `max` call
284+
= help: Replace with `min` call
285285

286286
Safe fix
287287
129 129 | value = 2
@@ -293,4 +293,4 @@ if_stmt_min_max.py:132:1: PLR1730 [*] Replace `if` statement with `max` call
293293
134 133 | value.
294294
135 134 | attr
295295
136 |- ) = 3
296-
135 |+ ) = max(value.attr, 3)
296+
135 |+ ) = min(value.attr, 3)

0 commit comments

Comments
 (0)