Rubberduck: Inspection for SetStmt mismatched assigned type and member return type

Created on 25 Nov 2016  路  1Comment  路  Source: rubberduck-vba/Rubberduck

(ref.#2382)
Given this code:

Sub DoSomething()
    Dim foo As Object
    Set foo = New Something
End Sub

When the LHS type mismatches the RHS type of a SetStmt, we should issue an inspection result. This one has a few subtleties though:

  • If the LHS is Object or 'Variant', Rubberduck should recommend using the RHS type instead.
  • If the RHS type implements a user interface, Rubberduck should suggest declaring the LHS variable with the interface type. For example if Something implements ISomething and IFoo then we should suggest declaring foo As ISomething or foo As IFoo.
  • If the LHS is a value type (or UDT), Rubberduck should issue an error-level inspection result to warn the user about the illegal assignment.
enhancement feature-inspections

Most helpful comment

To take this another step, I'd personally like to see RD recommend using New in cases like this...

Sub DoSomething()
    Dim foo As Bar
    Set foo = CreateObject("SomeApp.Bar")
End Sub

...with a quick-fix to Set foo = New SomeApp.Bar.

>All comments

To take this another step, I'd personally like to see RD recommend using New in cases like this...

Sub DoSomething()
    Dim foo As Bar
    Set foo = CreateObject("SomeApp.Bar")
End Sub

...with a quick-fix to Set foo = New SomeApp.Bar.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

retailcoder picture retailcoder  路  4Comments

susnick picture susnick  路  3Comments

DecimalTurn picture DecimalTurn  路  3Comments

retailcoder picture retailcoder  路  3Comments

Gener4tor picture Gener4tor  路  3Comments