Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VBA.Array() Inspection #6212

Open
retailcoder opened this issue Apr 20, 2024 · 2 comments
Open

VBA.Array() Inspection #6212

retailcoder opened this issue Apr 20, 2024 · 2 comments
Labels
enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. feature-inspections up-for-grabs Use this label in conjunction with a difficulty level label, e.g. difficulty-02-ducky

Comments

@retailcoder
Copy link
Member

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.

  • If Option Base 1 is specified, qualified VBA.Array() calls become inconsistent zero-based arrays in a module where implicitly-sized arrays can reasonably be assumed to be one-based.
  • If Option Base 1 is NOT specified, qualifying VBA.Array() calls should warn/hint about the behavior being different than that of an unqualified Array() call.

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.

@retailcoder retailcoder added enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. feature-inspections up-for-grabs Use this label in conjunction with a difficulty level label, e.g. difficulty-02-ducky labels Apr 20, 2024
@mansellan
Copy link
Member

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
Copy link
Member Author

retailcoder commented Apr 20, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. feature-inspections up-for-grabs Use this label in conjunction with a difficulty level label, e.g. difficulty-02-ducky
Projects
None yet
Development

No branches or pull requests

2 participants