Skip to content

@OptionStrict annotation #4936

Open
Open
@retailcoder

Description

@retailcoder

Justification

VBA is very forgiving in terms of implicit type conversions. Late-bound member calls move glaring compile-time errors from compile-time to run-time; variables don't even need a declared type! VB.NET addressed these loose ends with Option Strict, and I think we have everything we need to make VBA do the same.

Description

Microsoft Docs describes Option Strict as follows:

Option Strict Statement
Restricts implicit data type conversions to only widening conversions, disallows late binding, and disallows implicit typing that results in an Object type.

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/option-strict-statement

In a VBA context, "Object" means "Object or Variant".

Let's introduce a new @OptionStrict annotation, a MissingOptionStrictInspection "Rubberduck Opportunity" inspection that finds modules without it, and quickfix to add it in this module... and then another error-level OptionStrictInspection that point to all violations.

Bonus: a checkbox setting to "use 'Option Strict' everywhere", and have the annotation added automatically to every new (& existing?) module.

Additional context
These annotations should ideally all be valid (with or without the parentheses):

'@OptionStrict
'@OptionStrict(On)
'@OptionStrict("On")
'@OptionStrict Off
'@OptionStrict "Off"

...but it's completely fine to start with just a single @OptionStrict annotation, and have it support a parameter later: if the annotation is there, it's on, if not, it's off.

When '@OptionStrict is on, the following inspections are in effect:

Note that explicit Variant and Object declarations are legal with '@OptionStrict: what's not allowed is to make a member call against it without first casting it to an early-bound interface.

The advantages are:

  • IntelliSense everywhere!
  • Compile-time validation: pretty much eradicates run-time error 438.
  • Better run-time performance: since types are bound at compile-time, no interface lookups need to happen at run-time to locate a member.
  • Optimal Rubberduck insight: with every statement early-bound, a lot of tough problems solve themselves: we could have inspections that might require @OptionStrict in a module in order to run in that module.
  • VBA code that's just as robust as any VB.NET code, and that can easily convert to it.

The OptionStrict inspections should be public nested types in an all-encompassing OptionStrictInspection, so that '@Ignore OptionStrict works for all OptionStrict results. Disabling the OptionStrictInspection could fire an OptionStrictDisabledInspection result that flags '@OptionStrict annotations when the OptionStrictInspection is disabled, warning about the option not being enacted. Or (perhaps simpler, and less... recursive "what it that one is disabled?"), we special-case OptionStrictInspection and disable changing/configuring that inspection's severity level (Error).

Wiki should also be updated with a table of Widening Conversions adapted to VBA data types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    difficulty-03-duckInvolves more challenging problems and/or developing within and revising the internals APIenhancementFeature requests, or enhancements to existing features. Ideas. Anything within the project's scope.feature-annotationsfeature-inspection-quickfixesfeature-inspectionsup-for-grabsUse this label in conjunction with a difficulty level label, e.g. difficulty-02-ducky

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions