Assigned ByVal Parameter

Warns about parameters passed by value being assigned a new value in the body of a procedure.

Reasoning

Debugging is easier if the procedure's initial state is preserved and accessible anywhere within its scope. Mutating the inputs destroys the initial state, and makes the intent ambiguous: if the calling code is meant to be able to access the modified values, then the parameter should be passed ByRef; the ByVal modifier might be a bug.

Default severity

Warning

Inspection type

CodeQualityIssues

Examples

This example should trigger a result

MyModule (StandardModule)
Public Sub DoSomething(ByVal foo As Long) foo = foo + 1 ' is the caller supposed to see the updated value? Debug.Print foo End Sub

This example should NOT trigger a result

MyModule (StandardModule)
Public Sub DoSomething(ByVal foo As Long) Dim bar As Long bar = foo bar = bar + 1 ' clearly a local copy of the original value. Debug.Print bar End Sub

Rubberduck.CodeAnalysis.Inspections.Concrete.AssignedByValParameterInspection.cs (Prerelease-v2.5.9.6289)