-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Emit possibly-used-before-assignment
after if/else switches
#8952
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4ee174f
Emit `used-before-assignment` after if/else switches
jacobtylerwalls 9b42d21
Update expected results
jacobtylerwalls f17a83c
Add news
jacobtylerwalls 1c09c0a
Add annotation; move example to details.rst
jacobtylerwalls 55a86e4
Merge remote-tracking branch 'origin/main' into 1727-uba-false-negative
Pierre-Sassoulas 7a983a2
Fix false negative
jacobtylerwalls 99b2fc9
Update output
jacobtylerwalls a771383
Treat continue just like break
jacobtylerwalls f06f2f5
Add test case
jacobtylerwalls 8c758e2
Don't unnecessarily lower confidence level
jacobtylerwalls 48a0f25
Add test
jacobtylerwalls 35fb7d2
Reverse confidence order
jacobtylerwalls 76fe5ca
First stab at distinguishing new message
jacobtylerwalls 79fac0f
Initialize `tocheck` variable
jacobtylerwalls 3fefce6
Add good/bad
jacobtylerwalls 282150b
Reclassify changelog as new check
jacobtylerwalls a6dfb21
Move details.rst
jacobtylerwalls cc80eb9
Add expected msg
jacobtylerwalls d40710e
Update docs
jacobtylerwalls b362668
Apply suggestions from code review
jacobtylerwalls File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kind of a shame, but I can live with it.