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

False Positive "Parameter can be passed by value" #5635

Closed majkaz closed 3 years ago

majkaz commented 3 years ago

Rubberduck version information Version 2.5.1.5557 OS: Microsoft Windows NT 10.0.14393.0, x64 Host Product: Microsoft Outlook x86 Host Version: 16.0.4266.1001 Host Executable: OUTLOOK.EXE

Description As part of my old code, currently in refactoring, a MailItem is passed ByRef to a function and the function manipulates the subject if a condition is met. Inspection shows "Parameter can be passed by value".

The complete function for review:

Private Function DeferedDaysFromSubject(ByRef oMail As Outlook.MailItem) As Single

    Dim NumBegin As String
    NumBegin = GetNumericBeginning(oMail.Subject)

    If NumBegin <> vbNullString Then

        DeferedDaysFromSubject = VBA.CSng(NumBegin)

        Dim mailSubject As String
        mailSubject = VBA.Trim(VBA.Mid$(oMail.Subject, VBA.Len(NumBegin) + 1))
        oMail.Subject = mailSubject

    End If

End Function

In my understanding, this one is false positive.

retailcoder commented 3 years ago

Hi! Thanks for the feedback!

The reference itself is not being Set, so the result is legitimate, and the parameter can (arguably should) indeed be passed by value. oMail.Subject = ... is not altering the object reference ByRef/ByVal is referring to, only a property of that object; passing the reference ByVal would guarantee that the function cannot alter that reference as a side-effect.

Try adding Set oMail = Nothing just before End Function: with ByRef the caller probably blows up with error 91 now, with ByVal only the local pointer was (needlessly) nullified, and the calling code carries on without flinching!

majkaz commented 3 years ago

My mistake, the issue can be closed.

I know I am dating myself, but I swear I am aware of the situation when referencing an object since the time I had to change my macros from WordBasic to VBA. Looking at the spaghetti mess I have accumulated in years, I had a brain freeze.