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.91k stars 299 forks source link

Code Inspection: Highlighted variable is not the variable discussed in the inspection #5149

Closed ghost closed 5 years ago

ghost commented 5 years ago

Rubberduck version information

Version 2.4.1.4940 OS: Microsoft Windows NT 10.0.18362.0, x64 Host Product: Microsoft Office x64 Host Version: 16.0.12026.20108 Host Executable: WINWORD.EXE

Description

I'm getting the following inspection result, but the variable highlighted when I double click the inpsection report is not that referred to by the inspection report

Error: In a context that requires a value type, the expression 'Split(p.Value, mySeparator)' of object type 'VBAExtensions.tlb;VBAExtensions.Kvp' is used that does not have a suitable default member. - (Utuls) Utils.OStrActor

The object Kvp is provided by a C# library, the result of my first foray into c# programming.

I've refreshed the project several times to make sure that I'm not looking at out of date information.

The variable p.value is a string.

Public Function ToKvpBySubStrAsOStr(ByVal ipSeparator As Variant) As Kvp

Dim myArray                     As Variant
Dim myKvp                       As Kvp
Dim myItem                      As Variant
Dim mySeparator                 As String

    mySeparator = EnsureSeparator(ipSeparator)
    myArray = Split(p.Value, mySeparator)
    Set myKvp = New Kvp

    For Each myItem In myArray

        myKvp.AddByIndex OStrActor.Make(myItem)

    Next

    Set ToKvpBySubStrAsOStr = myKvp

End Function
bclothier commented 5 years ago

@SteveLaycock is it possible to share the interface definition for the Kvp? Knowing how the dispatch IDs are arranged may help clarify the issue.

ghost commented 5 years ago

No problem. I've attached the source code file. Please be aware that I am not a professional programmer have only previously coded in VBA (apart from some 6502 assembly and a small amount of ANSI C back in the 1980's doing image processing stuff for my PhD). This was my first foray into c# and it did take a few months googling and trial and error to get to the current state. I'm currently working up courage to put it up on code review for comment.

Kvp.zip

The VBA test suite for the Kvp object is also attached

TestKvp.zip

MDoerner commented 5 years ago

@SteveLaycock Two questions:

  1. You wrote that when double-clicking something else than the reported expression is highlighted. Could you please tell us what is selected when you double-click. That might help us find the issue.
  2. Could you also tell us how exactly p is declared. That might also help.

So far, I have not been able to reproduce this issue ar any discrepancy between the selection and expression.

Final stupid question, just in case: you have not defined a public function Split in your project or a private one in the module, right?

ghost commented 5 years ago

Your stupid question is entirely correct in its surmise. I am stupid, I will go and stand in the corner with my pointy hat on for the rest of the evening. The code in question should of course read VBA.Split... Which of course raises the follow on inspection thought that VBA has missed this duplication of names so maybe its a job for rubberduck?

BUT what would be causing a similar error in the following case

'@TestMethod("HelperForStrings")
Private Sub T2A_Stringify_object()

Dim myString                                   As String
Dim myTest                                     As String
Dim myValue                                    As Collection
    On Error GoTo TestFail

    'Arrange:
    myString = "{Can't stringify Type: Collection}"
    Set myValue = New Collection
    myValue.Add "Hurrah"
    myTest = HelperForStrings.Stringify(Array(myValue), vbNullString)

    'Assert:
    Assert.AreEqual myString, myTest

'TestExit:
    Exit Sub
TestFail:
    Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
End Sub

which gives the inspection report

Error: In a context that requires a value type, the expression 'myValue' of object type 'VBE7.DLL;VBA.Collection' is used that does not have a suitable default member. - (Utuls) Utils.TestStrings, line 261

bclothier commented 5 years ago

FWIW we do have shadowed inspection. I think that should have been caught.

MDoerner commented 5 years ago

OK, this is a legitimate bug. The resolver does not correctly handle param arrays, atm.

MDoerner commented 5 years ago

I have to correct myself, the problem is not strictly with param arrays but with the type names provided for arrays by the part of RD that loads libraries. It adds parentheses to the type name ,if the type is an array. This interferes with the check for the parameter type Variant for param arrays.