Skip to content

UnassignedVariableUsage Doesn't support variable being assigned as an "out" parameter from a property/sub/function #6198

@jhiston

Description

@jhiston

Short form:

Appears that if a variable is assigned during a call to a function/sub/property, the variable not assigned inspection is triggered (correctly in one view, since within the procedure being inspected there is no assignment, but missing possibility that assignment is occurring as part of a byref passing of a parameter to a subprocedure/function/property). More specifics below, and not sure this is a bug per se but seemed most appropriate category (possibly enhancement?)

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

Version 2.5.9.6316
OS: Microsoft Windows NT 10.0.19045.0, x64
Host Product: Microsoft Office x64
Host Version: 16.0.17126.20132
Host Executable: MSACCESS.EXE

Description
RE: Code Inspection: Unassigned Variable Usage

Inspection triggers on following code:

"variable 'aViaGroupsDisposer' is used but not assigned."

Private Sub I_Is_Disposable_Dispose()

Dim aViaGroupsDisposer As I_Func_Disposer_ViaGroups
If DisposerFunctionality.HasGroupedDisposingMethods(aViaGroupsDisposer) Then
    With aViaGroupsDisposer
       .TryDisposingGroupOfObjects Me, Me _
            , This.MyDependencies.Services.ClassShared _
            
    End With
End If

End Sub

as well as following code

'aDefaultSubstitutes' is used but not assigned

Private Sub I_4LocalUse_LocalConfiguration___COMMON_ReceiveVisitCOMMON(aVisitor As I_Visitor2_4LocalUse_LocalConfiguration)

 Dim aDefaultSubstitutes As I_4LocalUse_Substitutes
 If aVisitor.CanSupplyDefaultLocalSubstitutes(aDefaultSubstitutes) Then
    Set DefaultSubstitutes = aDefaultSubstitutes
End If

End Sub

I commonly use a pattern of (simplified)
if [Object].HasPropertyOfInterest(outPropertyOfInterest as object) as boolean then
....
End if

Advantage is to make more readable (and encompass more potential checks) than

if not Object.PropertyOfInterest is nothing then
set outPropertyOfInterest = Object.PropertyOfInterest ...
end if

But this coding pattern is triggering the unassigned variable usage inspection. I can disable so not a huge deal, but surprised it was flagged.

To Reproduce
Steps to reproduce the behavior:
see above

Expected behavioUr (:))
No inspection result if variable previously passed by Ref (or implicitly byref given objects) to a property/sub/function that explicitly labels the incoming parameter with an 'out' prefix (e.g. public property get HasPropertyOfInterest(outPropertyOfInterest as object) as boolean)

This is relying on a convention with the labelling so maybe not supportable and ignoring is proper approach (not sure this is a 'bug' per se)

Screenshots
If applicable, add screenshots to help explain your problem.

Logfile
N/A

Additional context
N/A

Metadata

Metadata

Assignees

Labels

bugIdentifies work items for known bugs

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions