Rubberduck: Variable is used but not assigned

Created on 14 Mar 2017  路  6Comments  路  Source: rubberduck-vba/Rubberduck

This is still being reported for object variables.
I thought I had raised this before but a search in the issues didn't bring up that subject.

Usually I get the inspection "Variable is never assigned" as well, which seems redundant.

bug feature-inspections inspection-false-positive

Most helpful comment

I'm getting this bug all the time - variables that are assigned being flagged as not so. Quite often it seems that the setting of the variable is within an If..Then construct, but not always. Eg here's a very simple proc with this issue

Sub Tests()
Dim mynmr As String
Dim rng As Range
mynmr = HasNamedRange(ActiveCell)
Set rng = GetRangeFromNMR(mynmr)
Debug.Print Intersect(rng, ActiveSheet.Cells) Is Nothing
End Sub

This is flagged as "Variable rng is never assigned" which it clearly is.

And I have this issue on tens of variables in the same project.

FYI from a quick inspection, it appears this is only for object variables needing the "Set" keyword, but this is not 100%.

Also get similar problems with "Return value for member xxx is never assigned" when again it clearly is.

All 6 comments

I thought we had an open one for this too, but it's possible that we might have been "tracking" this on another tangential issue that was closed. Is there a specific piece of code that's tripping this inspection?

Here's one:
line 11 Dim CodeMod As VBIDE.CodeModule
but it is assigned in
line 31 Set CodeMod = VBComp.CodeModule

This also happens with variables assigned byref but I can understand that'll be hard to catch.

Here's another
Line 12 Private mStack As Collection, <--- module private
but later
Line 48 Set mStack = New Collection

OK, thanks. That will at least provide a start for some failing unit tests.

I'm getting this bug all the time - variables that are assigned being flagged as not so. Quite often it seems that the setting of the variable is within an If..Then construct, but not always. Eg here's a very simple proc with this issue

Sub Tests()
Dim mynmr As String
Dim rng As Range
mynmr = HasNamedRange(ActiveCell)
Set rng = GetRangeFromNMR(mynmr)
Debug.Print Intersect(rng, ActiveSheet.Cells) Is Nothing
End Sub

This is flagged as "Variable rng is never assigned" which it clearly is.

And I have this issue on tens of variables in the same project.

FYI from a quick inspection, it appears this is only for object variables needing the "Set" keyword, but this is not 100%.

Also get similar problems with "Return value for member xxx is never assigned" when again it clearly is.

Excuse me, I'm new to this github stuff. Is there a value in saying "me too"? If so, what is the proper way to add a "me too" on an open issue? How do you measure prevalence of an issue?

@A9G-Data-Droid you can use the "reactions" drop-down and "thumbs-up" the issue :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

susnick picture susnick  路  3Comments

retailcoder picture retailcoder  路  3Comments

ThunderFrame picture ThunderFrame  路  3Comments

Gener4tor picture Gener4tor  路  3Comments

ChrisBrackett picture ChrisBrackett  路  3Comments