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.92k stars 302 forks source link

Inspection for suspicious implicit conversion in specific syntax nodes #4281

Open retailcoder opened 6 years ago

retailcoder commented 6 years ago

This requires us to be able to evaluate expression types.

Consider a For loop:

For i = 1 To foo

If foo is an integer type, no problem. However what if it's a Double? Or a Boolean?

For i = 1 To Cells(i + 2, 1).Value = 0)

(source)

When Rubberduck can evaluate expression types, we'll be able to warn the user about a suspicious implicit conversion from Boolean to Long happening here (assuming i is Long).

Similarly, consider this Open statement:

Dim fn As Long
fn = FreeFile
Open path For Input As #fn

Here fn is correctly assigned from FreeFile, however it's declared As Long; thus we can have a warning here, about an implicit downcast to Byte (right?)

There are a bunch of statements in the grammar, that demand specific types or have specific constraints, and take expressions that VBA coerces to a specific type - and we can inspect that.

MDoerner commented 5 years ago

Please note that according to the specification all three expressions in For i = <expr1> To <expr2> Step <expr3> are coerced to Double, not to Long.