Unassigned Variable Usage

Warns when a variable is referenced prior to being assigned.

Remarks

This inspection may produce false positives when the variable is an array, or if it's passed by reference (ByRef) to a procedure that assigns it.

Reasoning

An uninitialized variable is being read, but since it's never assigned, the only value ever read would be the data type's default initial value. Reading a variable that was never written to in any code path (especially if Option Explicit isn't specified), is likely to be a bug.

Default severity

Error

Inspection type

CodeQualityIssues

Examples

This example should trigger a result

MyModule (StandardModule)
Public Sub DoSomething() Dim i As Long Debug.Print i ' i was never assigned End Sub

This example should NOT trigger a result

MyModule (StandardModule)
Public Sub DoSomething() Dim i As Long i = 42 Debug.Print i End Sub

Rubberduck.CodeAnalysis.Inspections.Concrete.UnassignedVariableUsageInspection.cs (Prerelease-v2.5.9.6289)