RDC00115 — Implicit result local
Severity: Information · Family: RDC (Rubberduck Core analyzers) ·
Raised by: ImplicitResultLocalAnalyzer
A function or a property getter reads its own return value, as the local variable it implicitly is.
A function declaration implicitly defines a local variable that shares its name and its declared type with the function, the function result variable
(MS-VBAL §5.3.1 Procedure Declarations): what the function returns is what that variable holds when it
ends. Assigning it is how a function returns a value. Reading it - Total = Total + Item, Debug.Print Total, Items.Add x - works too, and makes the name of
the function a local variable of its own body. That reads as a call of the function, which it is everywhere else, and which Total(1) is in a function that takes
an argument. A local declared for the work, and assigned to the return value once, at the end, says what the code does.
Each read is reported where it is. A recursive call (Factorial(n - 1)) is a call, not a read, and is not reported; neither is an assignment of the return value,
nor a call of the function from another procedure.
How it is raised
| Situation | Example | Resolution |
|---|---|---|
| The return value is used as an accumulator. | For Each x In Items: Total = Total + x: Next |
Declare Dim result As Long, accumulate in it, and end with Total = result. |
| The return value is read after it is assigned. | Size = 2: Size = Size * 2 |
Compute the value in a local, and assign it once. |
| A member of an object it returns is called. | Set Items = New Collection: Items.Add 1 |
Build the object in a local (Set result = New Collection), and end with Set Items = result. |
Quick fix
The diagnostic comes with a quick fix, Declare a local for the return value of '…', which makes these edits of the procedure:
- a
Dimof a local, before its first statement:result, orresult2,result3… when the module or the procedure already has the name, of the type the procedure returns, written as it is (anAsclause, a type character, an array); - the local in place of each name of the return value in the body, read or assigned;
- the return value assigned the local before each
Exit FunctionorExit Property, on its line (If n = 0 Then Total = result: Exit Function), and on a line of its own before the end of the procedure, withSetwhen the bodySets its return value.
It is not offered for a procedure whose body starts on the line it is declared on. A function that returns an array and indexes it (Values(0) = 1) is not
reported: the index reads as a call of the function.
See also
- RD-VBAL §2.6.4 Rubberduck Core Diagnostics
- RDC00206 — Not all code paths return a value
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca