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 299 forks source link

VBA.Array() Inspection #6212

Open retailcoder opened 5 months ago

retailcoder commented 5 months ago

What An inspection that locates qualified VBA.Array() function calls to warn about possibly unexpected behavior; depending on how it's implemented this one could be a hint under language opportunities, or a warning under code quality, ...or both πŸ˜ƒ

Why Qualified VBA.Array() function calls yield an array that is always zero-based, but unqualified 'Array()' yields an array that is one-based given Option Base 1.

Example This code should trigger the inspection:

Option Base 1 '<~ Implicit array lower bound is 1

Public Sub DoSomething()
    Dim Values As Variant

    Values = Array(42)
    Debug.Print LBound(Values) '<~ 1 as per Option Base

    Values = VBA.Array(42) '<<< inspection result here
    Debug.Print LBound(Values) '<~ not 1
End Sub

QuickFixes Rubberduck could offer to remove the qualifier, albeit with a disclaimer that doing so may introduce off-by-one errors if the code makes any assumptions about array boundaries: it changes what the code does, not just how it does it.

mansellan commented 5 months ago

Agree that this is a worthwhile inspection.

Would it not also be worth an inspection to discourage use of Option Base 1 though? Most languages are zero-based, and VBA itself fights against base 1: image

retailcoder commented 5 months ago

There's already one πŸ˜‰

Although, I'm not sure the part about ParamArray is mentioned in the xmldoc. Or perhaps an inspection that flags (hint?) ParamArray parameters when Option Base 1 is on would be more targeted.