Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
majkaz opened this issue Nov 20, 2020 · 2 comments
Closed

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

majkaz opened this issue Nov 20, 2020 · 2 comments
Labels
feature-inspections status-bydesign "It's not a bug, it's a feature!" support Whether you're using Rubberduck or you've forked it and have questions, never hesitate to ask!

Comments

@majkaz
Copy link

majkaz commented Nov 20, 2020

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.

@majkaz majkaz added the bug Identifies work items for known bugs label Nov 20, 2020
@retailcoder
Copy link
Member

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!

@retailcoder retailcoder added feature-inspections status-bydesign "It's not a bug, it's a feature!" support Whether you're using Rubberduck or you've forked it and have questions, never hesitate to ask! and removed bug Identifies work items for known bugs labels Nov 20, 2020
@majkaz
Copy link
Author

majkaz commented Nov 21, 2020

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.

@majkaz majkaz closed this as completed Nov 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-inspections status-bydesign "It's not a bug, it's a feature!" support Whether you're using Rubberduck or you've forked it and have questions, never hesitate to ask!
Projects
None yet
Development

No branches or pull requests

2 participants