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.92k stars 302 forks source link

False positive `Assignmnet is not used` #5429

Closed daFreeMan closed 4 years ago

daFreeMan commented 4 years ago

Rubberduck version information

Version 2.5.0.5384
OS: Microsoft Windows NT 10.0.17134.0, x64
Host Product: Microsoft Office 2016 x64
Host Version: 16.0.4924.1000
Host Executable: MSACCESS.EXE

Description I get an Assignment is not used inspection result when the assigned variable is used later. It seems to be code specific.

To Reproduce This code produces the false positive:

Private Sub InsertIntoPermanentTable(ByVal surveys As DAO.Recordset)

  LogManager.Log DebugLevel, "EdwAccess.InsertIntoPermanentTable"

  If Not (surveys.EOF) Then
    Dim clinicID As Long
    clinicID = surveys.Fields("ClinicID").Value
  End If

  Dim insertStatement As String
  insertStatement = "INSERT INTO SurveyAnswers (ClinicID, ResponseID, QuestionID, Answer, CompletionDate) " & vbCrLf & _
                    "VALUES "

  Dim insertValues As String
  Dim insertCount As Long
  insertCount = 0

  Set serverCommand = New sqlCommand
  Dim serverConnection As ADODB.connection
  Set serverConnection = New ADODB.connection
  serverConnection.connectionString = CStr(TempVars.Item("constring").Value)

  Do While Not (surveys.EOF)
    insertValues = vbNullString
    Dim loopCount As Long
    loopCount = 1
    Do While Not (surveys.EOF) And (loopCount Mod 6 <> 0)
      insertValues = IIf(insertValues = vbNullString, insertValues, insertValues & ", ") & _
                     "       (" & surveys.Fields("ClinicID").Value & ", " & _
                     surveys.Fields("AppointmentID").Value & ", " & _
                     surveys.Fields("ID").Value & ", " & _
                     "'" & surveys.Fields("ResponseID").Value & "', " & _
                     "'" & surveys.Fields("CompleteDate").Value & "')" & vbCrLf
      loopCount = loopCount + 1
      surveys.MoveNext
    Loop
    Dim insertQuery As String
    insertQuery = insertStatement & insertValues
    Dim insertResult As Boolean
    insertResult = serverCommand.ExecuteNonQuery(serverConnection, insertQuery)
    If insertResult Then
      insertCount = insertCount + loopCount - 1
    Else
      LogManager.Log ErrorLevel, "Error inserting: " & insertQuery
    End If
  Loop

  LogManager.Log InfoLevel, "ClinicID: " & CStr(clinicID) & " -- Inserted record count: " & CStr(insertCount)
  Debug.Print "clinicID: ", clinicID, " -- insert count: "; insertCount

End Sub

This MCVE, however, does NOT produce an inspection:

Private Sub test(ByVal surveys As DAO.Recordset)
  Dim clinicID As Long
  clinicID = surveys.Fields("clinicID").Value
  LogManager.Log InfoLevel, "Test: " & CStr(clinicID)
  Debug.Print "clinicID: ", clinicID
End Sub

Expected behavior If a variable is assigned and later used within the module, the inspection should not appear

Screenshots N/A

Logfile RubberduckLog.txt

Additional context Add any other context about the problem here.

daFreeMan commented 4 years ago

Here's another example I just ran across:

Private Sub test()
  Dim foo As String
  foo = "asdf"
  foo = foo & "qwerty"
End Sub

The foo = "asdf" is flagged as not used even though it's clearly used on the very next line.

MDoerner commented 4 years ago

In the original example, where does the result point/navigate to?

The second one is a duplicate of #4913.

daFreeMan commented 4 years ago

Sorry, that would have been helpful...

  If Not (surveys.EOF) Then
    Dim ClinicID As Long
    ClinicID = surveys.Fields.Item("ClinicID").Value
  End If

It highlights ClinicID =.

MDoerner commented 4 years ago

That case is also covered in #4913. The code path analysis part makes the wrong assumption that if blocks define a scope in VBA, which is not true.

Closing as a duplicate.