Rubberduck: Variable not referred to - false positive?

Created on 28 Oct 2020  路  5Comments  路  Source: rubberduck-vba/Rubberduck

Rubberduck version information
The info below can be copy-paste-completed from the first lines of Rubberduck's log or the About box:

Rubberduck version [2.5.1.5622]
Operating System: [Win10]
Host Product: [vb6]
Host Version: [sp6]
Host Executable: [N/A]

Description
Variable not referred to false positive in for loop

To Reproduce

Public Function CreateNonce(Optional ByVal NonceLength As Long = 32) As String
    Dim web_Str As String
    Dim web_Result As String
    Dim web_Random As Long
    Dim web_Count As Long

    web_Str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUIVWXYZ"
    web_Result = vbNullString

    VBA.Randomize
    For web_Count = 1 To NonceLength
        web_Random = VBA.Int(((VBA.Len(web_Str) - 1) * VBA.Rnd) + 1)
        web_Result = web_Result & VBA.Mid$(web_Str, web_Random, 1)
    Next
    CreateNonce = web_Result
End Function

In this case it's referring to the variable web_Count.

Expected behavior
Should not trip

I know that is a silly variable name but it's someone else's code (using it as a library from GitHub) and I'd rather not make big changes so merging new versions will be as easy as possible.

bug

All 5 comments

The base reason for the inspection result is that we do not count assignments to the variable as a use. However, I think we should actually make an exception for variables used in a For loop, since there is no way to use one without a counter variable.
Still, I would not do the same for For Each loops since it is kind of suspicious that you do something for each items in a collection without actually referring to the item.

Agreed - it's definitely a code-smell to put something in a For Each and then not refer to it. But so far I haven't been able to think of a good way to refactor other devs libraries that I want to use without introducing major deltas between upstream and my code. It would be very counter productive if I do a bunch of code cleanup and upstream decides to ignore my pull requests. Every time they do an update and I pull the changes down it would conflict with my cleanup efforts. It's happened to me already and it's a pain in the neck.

could you check for me whether Next web_Count also trips the inspection? If it does a fix would be to mark the Next statement as a usage and augmenting it with the variable referred to in the loop head.
If it doesn't we can just do the latter.

I definitely will but it will be a little while before I can get back to it (a few days probably) I'm right in the middle of a critical job for a new client. The above issue is part of a new app I'm building so it's not a hot priority.

Not the OP, but in my case, macro in Word, simply printing one or more empty lines to immediate window:

Sub PrintEmptyLine(Optional ByVal Pocet As Long = 1)

    Dim i As Long
    For i = 1 To Pocet
        Debug.Print ""
    Next i
End Sub

Written like this, it doesn't trip the inspection. Using plain and simple Next without the i variable, I get the "variable not used" as well.

Was this page helpful?
0 / 5 - 0 ratings