rubberduck-vba / Rubberduck

Every programmer needs a rubberduck. COM add-in for the VBA & VB6 IDE (VBE).
https://rubberduckvba.com
GNU General Public License v3.0
1.91k stars 301 forks source link

Auto Instantiated inspection ignores module-level variables #3568

Open ThunderFrame opened 6 years ago

ThunderFrame commented 6 years ago

The following code generates Object variable reference is auto-instantiated inspections for fizz and buzz but not for foo and bar

Option Explicit

Private foo(10) As New Class2
Private bar As New Class2

Sub test()

  Dim fizz(10) As New Class2
  Dim buzz As New Class2

  Debug.Print foo(1) Is Nothing, bar Is Nothing, fizz(1) Is Nothing, buzz Is Nothing

  Set foo(1) = Nothing
  Set bar = Nothing
  Set fizz(1) = Nothing
  Set buzz = Nothing

  Debug.Print foo(1) Is Nothing, bar Is Nothing, fizz(1) Is Nothing, buzz Is Nothing

End Sub
retailcoder commented 6 years ago

That's by design actually; the inspection explicitly only looks at procedure-scope variables. Arguably the issue is the same at module-scope, but it was thought that a module-level variable declared As New pretty much implied that it means to live as long as the class it's declared in.

I guess it wouldn't hurt to enhance the inspection to also look at module scope... The meta/descriptions need to be updated too.

ThunderFrame commented 6 years ago

Maybe split it into 2 inspections? The descriptions could vary by module/procedure scope, with differing levels of severity and caution/advice to the user? Likewise, the inspections could then be disabled per scope.