Skip to content

Add PredeclaredClassAssignedToVariable inspection #5876

@pchemguy

Description

@pchemguy

What
Potential bug: predeclared class instance is assigned to a variable.
Potential reason: missed New operator.
Automatic fix: Add missing New operator.

Why
Predeclared class instances are typically used as class factories. Usually, assigning the predeclared instance to a variable would make little sense.

Example
This code should trigger the inspection:

'@PredeclaredId

Public Function Create(ByVal Arg As String) As PredeclaredClass
    Dim Instance As PredeclaredClass
    Set Instance = PredeclaredClass
    Instance.Init Arg
    Set Create = Instance
End Function

QuickFixes
Should Rubberduck offer one or more quickfix(es) for this inspection? Describe them here (note: all inspections allow for IgnoreOnceQuickFix, unless explicitly specified):

  1. Add missing New operator

    Example code, after quickfix is applied:

'@PredeclaredId

Public Function Create(ByVal Arg As String) As PredeclaredClass
    Dim Instance As PredeclaredClass
    Set Instance = New PredeclaredClass
    Instance.Init Arg
    Set Create = Instance
End Function

Resources
Each inspection needs a number of resource strings - please provide a suggestion here:

  • Predeclared class instance is assigned to variable: the name of the inspection, as it appears in the inspection settings.
  • Predeclared class instance is usually used as a class factory, and it should make little sense to assign it to a variable.: the detailed rationale for the inspection, as it appears in the inspection results toolwindow's bottom panel.
  • Predeclared class {0} is assigned to variable, {0}:the name of the predeclared class: the resource string for an inspection result; if the string needs placeholders, identify what goes in for them (e.g. Variable {0} is not used, {0}:the name of the variable).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementFeature requests, or enhancements to existing features. Ideas. Anything within the project's scope.feature-inspectionsup-for-grabsUse this label in conjunction with a difficulty level label, e.g. difficulty-02-ducky

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions