-
-
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.
Emit
possibly-used-before-assignment
after if/else switches (#8952)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
- Loading branch information
1 parent
73e82bd
commit 67bfab4
Showing
23 changed files
with
156 additions
and
73 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,4 @@ | ||
def check_lunchbox(items: list[str]): | ||
if not items: | ||
empty = True | ||
print(empty) # [possibly-used-before-assignment] |
15 changes: 15 additions & 0 deletions
15
doc/data/messages/p/possibly-used-before-assignment/details.rst
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,15 @@ | ||
If you rely on a pattern like: | ||
|
||
.. sourcecode:: python | ||
|
||
if guarded(): | ||
var = 1 | ||
|
||
if guarded(): | ||
print(var) # emits possibly-used-before-assignment | ||
|
||
you may be concerned that ``possibly-used-before-assignment`` is not totally useful | ||
in this instance. However, consider that pylint, as a static analysis tool, does | ||
not know if ``guarded()`` is deterministic or talks to | ||
a database. (Likewise, for ``guarded`` instead of ``guarded()``, any other | ||
part of your program may have changed its value in the meantime.) |
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,5 @@ | ||
def check_lunchbox(items: list[str]): | ||
empty = False | ||
if not items: | ||
empty = True | ||
print(empty) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Add check ``possibly-used-before-assignment`` when relying on names after an ``if/else`` | ||
switch when one branch failed to define the name, raise, or return. | ||
|
||
Closes #1727 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
redefined-outer-name:11:4:12:12::Redefining name 'err' from outer scope (line 8):UNDEFINED | ||
redefined-outer-name:57:8:58:16::Redefining name 'err' from outer scope (line 51):UNDEFINED | ||
used-before-assignment:69:14:69:29:func:Using variable 'CustomException' before assignment:CONTROL_FLOW | ||
used-before-assignment:69:14:69:29:func:Using variable 'CustomException' before assignment:HIGH | ||
redefined-outer-name:71:4:72:12:func:Redefining name 'CustomException' from outer scope (line 62):UNDEFINED |
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
2 changes: 1 addition & 1 deletion
2
tests/functional/u/used/used_before_assignment_except_handler_for_try_with_return.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,7 +1,7 @@ | ||
used-before-assignment:16:14:16:29:function:Using variable 'failure_message' before assignment:CONTROL_FLOW | ||
used-before-assignment:120:10:120:13:func_invalid1:Using variable 'msg' before assignment:CONTROL_FLOW | ||
used-before-assignment:131:10:131:13:func_invalid2:Using variable 'msg' before assignment:CONTROL_FLOW | ||
used-before-assignment:150:10:150:13:func_invalid3:Using variable 'msg' before assignment:CONTROL_FLOW | ||
used-before-assignment:150:10:150:13:func_invalid3:Using variable 'msg' before assignment:INFERENCE | ||
used-before-assignment:163:10:163:13:func_invalid4:Using variable 'msg' before assignment:CONTROL_FLOW | ||
used-before-assignment:175:10:175:13:func_invalid5:Using variable 'msg' before assignment:CONTROL_FLOW | ||
used-before-assignment:187:10:187:13:func_invalid6:Using variable 'msg' before assignment:CONTROL_FLOW |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
used-before-assignment:12:14:12:17:main:Using variable 'res' before assignment:CONTROL_FLOW | ||
used-before-assignment:12:14:12:17:main:Using variable 'res' before assignment:INFERENCE | ||
used-before-assignment:30:18:30:35:nested_except_blocks:Using variable 'more_bad_division' before assignment:CONTROL_FLOW | ||
used-before-assignment:31:18:31:21:nested_except_blocks:Using variable 'res' before assignment:CONTROL_FLOW | ||
used-before-assignment:31:18:31:21:nested_except_blocks:Using variable 'res' before assignment: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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
unused-variable:5:4:6:12:main1:Unused variable 'e':UNDEFINED | ||
used-before-assignment:8:10:8:11:main1:Using variable 'e' before assignment:CONTROL_FLOW | ||
used-before-assignment:8:10:8:11:main1:Using variable 'e' before assignment:HIGH | ||
unused-variable:21:4:22:12:main3:Unused variable 'e':UNDEFINED | ||
unused-variable:31:4:32:12:main4:Unused variable 'e':UNDEFINED | ||
used-before-assignment:44:10:44:11:main4:Using variable 'e' before assignment:CONTROL_FLOW | ||
used-before-assignment:44:10:44:11:main4:Using variable 'e' before assignment:HIGH |
2 changes: 1 addition & 1 deletion
2
tests/functional/u/used/used_before_assignment_postponed_evaluation.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 +1 @@ | ||
used-before-assignment:10:6:10:9::Using variable 'var' before assignment:CONTROL_FLOW | ||
used-before-assignment:10:6:10:9::Using variable 'var' before assignment: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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
used-before-assignment:10:13:10:21:func_two:Using variable 'datetime' before assignment:CONTROL_FLOW | ||
used-before-assignment:16:12:16:20:func:Using variable 'datetime' before assignment:CONTROL_FLOW | ||
used-before-assignment:10:13:10:21:func_two:Using variable 'datetime' before assignment:INFERENCE | ||
used-before-assignment:16:12:16:20:func:Using variable 'datetime' before assignment:INFERENCE |
Oops, something went wrong.