-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement/add checker consider using min max builtin (#4359)
Co-authored-by: Pierre Sassoulas <[email protected]> Co-authored-by: manderj <[email protected]>
- Loading branch information
1 parent
1c0ee1d
commit bafb149
Showing
6 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
tests/functional/c/consider/consider_using_min_max_builtin.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# pylint: disable=missing-docstring, invalid-name, too-few-public-methods, redefined-outer-name | ||
|
||
value = 10 | ||
value2 = 0 | ||
value3 = 3 | ||
|
||
# Positive | ||
if value < 10: # [consider-using-max-builtin] | ||
value = 10 | ||
|
||
if value >= 10: # [consider-using-min-builtin] | ||
value = 10 | ||
|
||
if value <= 10: # [consider-using-max-builtin] | ||
value = 10 | ||
|
||
if value > 10: # [consider-using-min-builtin] | ||
value = 10 | ||
|
||
if value < value2: # [consider-using-max-builtin] | ||
value = value2 | ||
|
||
if value > value2: # [consider-using-min-builtin] | ||
value = value2 | ||
|
||
|
||
class A: | ||
def __init__(self): | ||
self.value = 13 | ||
|
||
|
||
A1 = A() | ||
if A1.value > 10: # [consider-using-min-builtin] | ||
A1.value = 10 | ||
|
||
|
||
class AA: | ||
def __init__(self, value): | ||
self.value = value | ||
|
||
def __gt__(self, b): | ||
return self.value > b | ||
|
||
def __ge__(self, b): | ||
return self.value >= b | ||
|
||
def __lt__(self, b): | ||
return self.value < b | ||
|
||
def __le__(self, b): | ||
return self.value <= b | ||
|
||
|
||
A1 = AA(0) | ||
A2 = AA(3) | ||
|
||
if A1 > A2: # [consider-using-min-builtin] | ||
A1 = A2 | ||
|
||
if A2 < A1: # [consider-using-max-builtin] | ||
A2 = A1 | ||
|
||
if A1 >= A2: # [consider-using-min-builtin] | ||
A1 = A2 | ||
|
||
if A2 <= A1: # [consider-using-max-builtin] | ||
A2 = A1 | ||
|
||
# Negative | ||
if value > 10: | ||
value = 2 | ||
|
||
if value > 10: | ||
value = 2 | ||
value2 = 3 | ||
|
||
if value > value2: | ||
value = value3 | ||
|
||
if value > 5: | ||
value = value3 | ||
|
||
if 2 < value <= 3: | ||
value = 1 | ||
|
||
if value <= 3: | ||
value = 5 | ||
|
||
if value <= 3: | ||
value = 5 | ||
elif value == 3: | ||
value = 2 | ||
|
||
if value > 10: | ||
value = 10 | ||
else: | ||
value = 3 |
11 changes: 11 additions & 0 deletions
11
tests/functional/c/consider/consider_using_min_max_builtin.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
consider-using-max-builtin:8:0::"Consider using 'value = max(value, 10)' instead of unnecessary if block" | ||
consider-using-min-builtin:11:0::"Consider using 'value = min(value, 10)' instead of unnecessary if block" | ||
consider-using-max-builtin:14:0::"Consider using 'value = max(value, 10)' instead of unnecessary if block" | ||
consider-using-min-builtin:17:0::"Consider using 'value = min(value, 10)' instead of unnecessary if block" | ||
consider-using-max-builtin:20:0::"Consider using 'value = max(value, value2)' instead of unnecessary if block" | ||
consider-using-min-builtin:23:0::"Consider using 'value = min(value, value2)' instead of unnecessary if block" | ||
consider-using-min-builtin:33:0::"Consider using 'value = min(value, 10)' instead of unnecessary if block" | ||
consider-using-min-builtin:57:0::"Consider using 'A1 = min(A1, A2)' instead of unnecessary if block" | ||
consider-using-max-builtin:60:0::"Consider using 'A2 = max(A2, A1)' instead of unnecessary if block" | ||
consider-using-min-builtin:63:0::"Consider using 'A1 = min(A1, A2)' instead of unnecessary if block" | ||
consider-using-max-builtin:66:0::"Consider using 'A2 = max(A2, A1)' instead of unnecessary if block" |