Self Assigned Declaration

Identifies auto-assigned object declarations.

Reasoning

Auto-assigned objects are automatically re-created as soon as they are referenced. It is therefore impossible to set one such reference to 'Nothing' and then verifying whether the object 'Is Nothing': it will never be. This behavior is potentially confusing and bug-prone.

Default severity

Suggestion

Inspection type

CodeQualityIssues

Examples

This example should trigger a result

MyModule (StandardModule)
Public Sub DoSomething() Dim c As New Collection Set c = Nothing c.Add 42 ' no error 91 raised Debug.Print c.Count ' 1 Set c = Nothing Debug.Print c Is Nothing ' False End Sub

This example should NOT trigger a result

MyModule (StandardModule)
Public Sub DoSomething() Dim c As Collection Set c = New Collection Set c = Nothing c.Add 42 ' error 91 Debug.Print c.Count ' error 91 Set c = Nothing Debug.Print c Is Nothing ' True End Sub

Rubberduck.CodeAnalysis.Inspections.Concrete.SelfAssignedDeclarationInspection.cs (Prerelease-v2.5.9.6289)