Rubberduck: VariableTypeNotDeclaredInspection could try to infer the variable's type

Created on 12 Feb 2015  路  5Comments  路  Source: rubberduck-vba/Rubberduck

Consider this declaration:

Public Const MyConstant = "foo"

Rubberduck will suggest to _explicitly_ declare the constant as a Variant. But the parse tree _knows_ the value assigned is a _string literal_ - therefore Rubberduck should have enough information to declare it as a String.

Same here:

Dim foo
foo = 123

The parse tree _knows_ the assigned value is a number. Rubberduck should declare as Long.

Dim bar
bar = #2015-05-15#

Date literal.


  • [ ] Number literals: either Long or Double (if value has decimals)
  • [ ] String literals: String
  • [ ] Date literals: Date
  • [ ] Assigned value is Set: Object is still vague, but better than a Variant.
  • [ ] Right-hand side of the assignment is a native function call. Use the [hard-coded] return type of that known native function. e.g. CInt function will always return an Integer; MsgBox calls return a Long, etc.
  • [ ] Right-hand side of the assignment is a function defined in the active VBProject. Use the function's return type to determine the type of the assigned variable. Careful with scopes: you will only want to look for functions defined in the same module, or those defined in another module with a Public or Friend accessibility.
  • [ ] Right-hand side of the assignment is another variable (or constant), defined in the active VBProject. Find the declaration and use the declared type to determine the type of the assigned variable. Careful with scopes: you will only want to look at variables defined at _module scope_ in the same module, or Public, Friend or Global fields in other modules' _declarations section_.

Otherwise, give up and leave as a Variant.

code-path-analysis enhancement feature-inspections

Most helpful comment

Yes Please! Gets my vote. Hell, I'll implement it myself if I have to. :+1: :+1: :+1:

All 5 comments

Yes Please! Gets my vote. Hell, I'll implement it myself if I have to. :+1: :+1: :+1:

With the Declarations API this is now getting more and more realistic - if a constant, variable, function or member is referenced in the right-hand side of an assignment, then the type to use on the left-hand side is known, even if it's an Object, and the scoping issue is already solved.

Literals can be picked up pretty easily.

If we say anything else remains Variant (or Object if the left-hand side is Set), then we're already in a pretty good position to infer the return type.

We can also check if it is passed as a parameter in a function call, and if so, whether the parameter has specified a type.

linking #3225

This should perhaps honor any type hints:

Const LONG_NUMBER = &H1 is implicitly an Integer
Const LONG_NUMBER = &H1& is a type-hinted long

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ChrisBrackett picture ChrisBrackett  路  3Comments

philippetev picture philippetev  路  3Comments

SteGriff picture SteGriff  路  3Comments

Gener4tor picture Gener4tor  路  3Comments

Gener4tor picture Gener4tor  路  3Comments