Skip to content

AssignmentNotUsed Inspection false positive #6142

Open
@thefasquelle

Description

@thefasquelle

Rubberduck version information
Version 2.5.2.5906
OS: Microsoft Windows NT 10.0.19045.0, x64
Host Product: Microsoft Office 2016 x64
Host Version: 16.0.5404.1000
Host Executable: EXCEL.EXE

Description
The code below generates AssignmentNotUsed warnings for the first assignement to previousColor, humanComment, expectedPart and colorPart, although all assignments after the first ones are conditional and the first assigned value may be (are) used afterward.

Private Sub processCell(ByVal cell As Range, ByVal rowDefaultColor As Long, Optional ByVal entry As Variant = Nothing)
    Dim autoGeneratedComment As String
    Dim previousColor As Long: previousColor = -1

    ' three parts of the comment
    Dim humanComment As String: humanComment = ""
    Dim expectedPart As String: expectedPart = ""
    Dim colorPart As String: colorPart = ""

    ' parse existing comment
    Dim result As Object
    ' Matches ReportParser comment alone or with human comment (group 0), with or witouht color info (group 1)
    ' No match means human comment alone
    ' SubMatches(0) is human added comment to be kept. Empty means no human addded comment, only ReportParser comment
    ' SubMatches(1) cell color prior to error to restore if error is fixed. Empty means no color information (restaure default color)
    If Not cell.Comment Is Nothing Then
        Set result = regexAutoComment.Execute(cell.Comment.text)
        If result.Count <> 0 Then
            ' got a match, this means there is an auto generated part in this comment
            If Not IsEmpty(result(0).SubMatches(0)) Then humanComment = result(0).SubMatches(0)
            If Not IsEmpty(result(0).SubMatches(1)) Then previousColor = CLng(result(0).SubMatches(1))

            humanComment = Utils.RemoveTrailingWhiteSpaces(humanComment)
        Else
            ' No match -> the whole comment is human added, so we must keep it as it is
            humanComment = cell.Comment.text
        End If
    End If
    '''''''''''''''''''
    ' Stripped part with where previousColor, humanComment, expectedPart are read multiple times but never assigned
    ' + Some code where colorPart is conditionally assigned
    '''''''''''''''''''
    If humanComment <> "" And expectedPart <> "" Then
        autoGeneratedComment = humanComment & vbNewLine & vbNewLine & expectedPart & colorPart
    ElseIf humanComment <> "" And expectedPart = "" Then
        autoGeneratedComment = humanComment
    Else
        autoGeneratedComment = expectedPart & colorPart
    End If

    ''''''''''''''''''
    ' Stripped irrelevant code
    ''''''''''''''''''
End Sub

Expected behavior
previousColor = -1, humanComment = "", expectedPart = "" and colorPart = "" should not be flagged as unused.

I can understand that explicitly assign "" to a newly declared string is useless and may generate a warning (but in my opinion it should not since it is making an implicit assignment explicit), but previousColor = -1 should definitily not trigger a warning

Additional context
There is no jump in this sub, only If blocks so it not the same as #5456

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIdentifies work items for known bugs

    Type

    No type

    Projects

    Status

    Backlog | Inspections

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions