Closed
Description
Sample code:
def fix_out_of_range(x: int, min_x: int, max_x: int) -> int:
if x < min_x:
x = min_x
if x > max_x:
x = max_x
return x
Command: ruff check --preview --select PLR test.py
Result:
test.py:3:2: PLR1730 [*] Replace `if` statement with `x = min(x, min_x)`
|
2 | def fix_out_of_range(x: int, min_x: int, max_x: int) -> int:
3 | if x < min_x:
| _____^
4 | | x = min_x
| |_________________^ PLR1730
5 | if x > max_x:
6 | x = max_x
|
= help: Replace with `x = min(x, min_x)`
test.py:5:2: PLR1730 [*] Replace `if` statement with `x = max(x, max_x)`
|
3 | if x < min_x:
4 | x = min_x
5 | if x > max_x:
| _____^
6 | | x = max_x
| |_________________^ PLR1730
7 | return x
|
= help: Replace with `x = max(x, max_x)`
Found 2 errors.
[*] 2 fixable with the `--fix` option.
The min / max are reversed.
The first one must be a max
, second one min
.
Passing --fix
will break the code:
def fix_out_of_range(x: int, min_x: int, max_x: int) -> int:
x = min(x, min_x)
x = max(x, max_x)
return x
The correct fix:
def fix_out_of_range(x: int, min_x: int, max_x: int) -> int:
x = max(x, min_x)
x = min(x, max_x)
return x