-
Notifications
You must be signed in to change notification settings - Fork 301
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
AssignmentNotUsed Inspection false positive #6142
Comments
I suppose this is linked somehow : the following piece of code is extracted from https://codereview.stackexchange.com/q/156070
AssignementNotUsed is triggered for "Keys" and "items" (as expected), but VariableNotUsed is also triggered for these two variables as well, which is not expected |
The wording is difficult to nail right, but if that snippet is all there is to it, then VariableNotUsed is a correct diagnostic because while both are assigned, neither are being read - which is really what "not used" means in this context: the code would behave identically without these variables existing at all. If there's other code underneath that snippet that reads I don't think the inspection is accounting for conditional re-assignments, and really it probably should. Excluding inspection results when an assignment is conditionally overwritten (parent context is a child of an "if" statement or block) would remove most of the false positive cases, I believe. |
In the second example, it is indeed unused. should have thought a little more before complaining ...
I wonder if there is more than that. Here is a snippet that I forgot to include in my first post after the second stripped part :
At the very end of the sub, I check wheter previousColor is positive or not, so the initial assignment to -1 is used, with or without conditional assignments. After reading your answer I thought that this access was ignored because it is conditional too. But even after removing the |
The problem seems to be that the inspection thinks that This inspection was supposed to do proper code path analysis, but apparently, it fails at that. @retailcoder Actually, if there was code using |
I don't know if this related but the following code also triggers the inspection Option Explicit
Public Sub verifBug()
Dim lastColumn As Long
' retrieve requirements column
lastColumn = 1
Do Until IsEmpty(ActiveSheet.Cells(4, lastColumn))
lastColumn = lastColumn + 1
Loop
lastColumn = lastColumn + 1 ' triggers AssignmentNotUsed
Debug.Print "Last column is " & lastColumn
End Sub What seems to confuse the parser is the Do Until loop: when I remove it the inspection warning is gone. |
It's not exactly wrong: the value assigned by the last iteration of the loop is indeed overwritten before it's read. There should be a way to rephrase the loop (do...while?) such that That said, finding the last column of a range doesn't need a loop: the |
Actually, it's not: the RHS is evaluated first, and should be counted as a read of the last assignment. I believe the inspection might be having trouble with taking RHS into account in an expression like |
There's definitely something weird going on that deserves attention, thanks for digging! |
Don't know if this is related but the following code also triggers a false positive
It's something with the assignment in the conditional if statement, commenting out the assignment in the If statement does not trigger the inspection. |
IIRC |
Yes the status bar says 4 references. The following code without Debug statements has the same issue:
|
Rubberduck version information
Version 2.5.2.5906
OS: Microsoft Windows NT 10.0.19045.0, x64
Host Product: Microsoft Office 2016 x64
Host Version: 16.0.5404.1000
Host Executable: EXCEL.EXE
Description
The code below generates AssignmentNotUsed warnings for the first assignement to previousColor, humanComment, expectedPart and colorPart, although all assignments after the first ones are conditional and the first assigned value may be (are) used afterward.
Expected behavior
previousColor = -1, humanComment = "", expectedPart = "" and colorPart = "" should not be flagged as unused.
I can understand that explicitly assign "" to a newly declared string is useless and may generate a warning (but in my opinion it should not since it is making an implicit assignment explicit), but previousColor = -1 should definitily not trigger a warning
Additional context
There is no jump in this sub, only If blocks so it not the same as #5456
The text was updated successfully, but these errors were encountered: