-
-
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.
Add
consider-using-augmented-assign
checker (#7514)
Co-authored-by: clavedeluna <danalitovsky+git@gmail.com> Co-authored-by: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com>
- Loading branch information
1 parent
1918a4d
commit 705ac4c
Showing
15 changed files
with
203 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = 1 | ||
x = x + 1 # [consider-using-augmented-assign] |
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,2 @@ | ||
x = 1 | ||
x += 1 |
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,4 @@ | ||
Added ``consider-using-augmented-assign`` which flags ``x = x + 1`` to simplify to | ||
``x += 1``. | ||
|
||
Closes #3391 |
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
77 changes: 77 additions & 0 deletions
77
tests/functional/c/consider/consider_using_augmented_assign.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,77 @@ | ||
"""Tests for consider-using-augmented-assign.""" | ||
|
||
# pylint: disable=invalid-name,too-few-public-methods,import-error,consider-using-f-string | ||
|
||
from unknown import Unknown | ||
|
||
x = 1 | ||
x = x + 1 # [consider-using-augmented-assign] | ||
x = 1 + x # [consider-using-augmented-assign] | ||
x, y = 1 + x, 2 + x | ||
# We don't warn on intricate expressions as we lack knowledge | ||
# of simplifying such expressions which is necessary to see | ||
# if they can become augmented | ||
x = 1 + x - 2 | ||
x = 1 + x + 2 | ||
|
||
# For anything other than a float or an int we only want to warn on | ||
# assignments where the 'itself' is on the left side of the assignment | ||
my_list = [2, 3, 4] | ||
my_list = [1] + my_list | ||
|
||
|
||
class MyClass: | ||
"""Simple base class.""" | ||
|
||
def __init__(self) -> None: | ||
self.x = 1 | ||
self.x = self.x + 1 # [consider-using-augmented-assign] | ||
self.x = 1 + self.x # [consider-using-augmented-assign] | ||
|
||
x = 1 # [redefined-outer-name] | ||
self.x = x | ||
|
||
|
||
instance = MyClass() | ||
|
||
x = instance.x + 1 | ||
|
||
my_str = "" | ||
my_str = my_str + "foo" # [consider-using-augmented-assign] | ||
my_str = "foo" + my_str | ||
|
||
my_bytes = b"" | ||
my_bytes = my_bytes + b"foo" # [consider-using-augmented-assign] | ||
my_bytes = b"foo" + my_bytes | ||
|
||
|
||
def return_str() -> str: | ||
"""Return a string.""" | ||
return "" | ||
|
||
|
||
# Currently we disregard all calls | ||
my_str = return_str() + my_str | ||
|
||
my_str = my_str % return_str() | ||
my_str = my_str % 1 # [consider-using-augmented-assign] | ||
my_str = my_str % (1, 2) # [consider-using-augmented-assign] | ||
my_str = "%s" % my_str | ||
my_str = return_str() % my_str | ||
my_str = Unknown % my_str | ||
my_str = my_str % Unknown # [consider-using-augmented-assign] | ||
|
||
x = x - 3 # [consider-using-augmented-assign] | ||
x = x * 3 # [consider-using-augmented-assign] | ||
x = x / 3 # [consider-using-augmented-assign] | ||
x = x // 3 # [consider-using-augmented-assign] | ||
x = x << 3 # [consider-using-augmented-assign] | ||
x = x >> 3 # [consider-using-augmented-assign] | ||
x = x % 3 # [consider-using-augmented-assign] | ||
x = x**3 # [consider-using-augmented-assign] | ||
x = x ^ 3 # [consider-using-augmented-assign] | ||
x = x & 3 # [consider-using-augmented-assign] | ||
x = x > 3 | ||
x = x < 3 | ||
x = x >= 3 | ||
x = x <= 3 |
20 changes: 20 additions & 0 deletions
20
tests/functional/c/consider/consider_using_augmented_assign.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,20 @@ | ||
consider-using-augmented-assign:8:0:8:9::Use '+=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:9:0:9:9::Use '+=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:28:8:28:27:MyClass.__init__:Use '+=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:29:8:29:27:MyClass.__init__:Use '+=' to do an augmented assign directly:INFERENCE | ||
redefined-outer-name:31:8:31:9:MyClass.__init__:Redefining name 'x' from outer scope (line 7):UNDEFINED | ||
consider-using-augmented-assign:40:0:40:23::Use '+=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:44:0:44:28::Use '+=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:57:0:57:19::Use '%=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:58:0:58:24::Use '%=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:62:0:62:25::Use '%=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:64:0:64:9::Use '-=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:65:0:65:9::Use '*=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:66:0:66:9::Use '/=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:67:0:67:10::Use '//=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:68:0:68:10::Use '<<=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:69:0:69:10::Use '>>=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:70:0:70:9::Use '%=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:71:0:71:8::Use '**=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:72:0:72:9::Use '^=' to do an augmented assign directly:INFERENCE | ||
consider-using-augmented-assign:73:0:73:9::Use '&=' to do an augmented assign directly:INFERENCE |
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
3 changes: 3 additions & 0 deletions
3
tests/functional/u/used/used_before_assignment_conditional.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
4 changes: 2 additions & 2 deletions
4
tests/functional/u/used/used_before_assignment_conditional.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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
used-before-assignment:3:8:3:9::Using variable 'x' before assignment:HIGH | ||
used-before-assignment:5:3:5:4::Using variable 'y' before assignment:HIGH | ||
used-before-assignment:6:8:6:9::Using variable 'x' before assignment:HIGH | ||
used-before-assignment:8:3:8:4::Using variable 'y' before assignment:HIGH |
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