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

Inspection for possibly failing cast between user defined object types #4245

Open MDoerner opened 6 years ago

MDoerner commented 6 years ago

I think, once we are able to evaluate the declared type of an expression, it would be good to have an inspection that finds all possibly failing casts between user-defined object types.

Such casts can happen implicitly in two situaions:

  1. Set-assignments
  2. passing an object as an argument

In both cases, the VBE does not verify that the object types are compatible at compile time. This can lead to a lot of runtime errors when one simply forgot to adapt some type after copying some code.

The inspection should evaluate the type of the RHS of Set-assignments and of object type arguments and verify that it is the target type or implements it.

This is currently blocked by the implementation/unification and extension of the expression evaluation engine. After that, this should be fairly easy to implement.

The focus here is on user-defined types because objects from COM libraries have the potential to generate a lot of false positives because they do not have to adhere to VBA's own restrictions on inheritance.

comintern commented 6 years ago

I think we could do library objects fairly easily. The COM collector already picks up all of the inherited interfaces from each type, so it would simply be a matter of exposing that from the generated Declarations. The only ones that would be difficult would be the evaluate members, but IIR we generally just assume that those are valid anyway because there's no way to check them without making a call to the host application.