RDC00207: IsMissing that is always False
Severity: Warning · Family: RDC (Rubberduck Core analyzers) ·
Raised by: IsMissingAlwaysFalseAnalyzer
IsMissing is given a parameter, a variable or a constant that can never be missing, so it always returns False.
IsMissing tells whether an Optional argument was left out of a call (MS-VBAL §6.1.2.7.1.6). It can only tell for a
Variant parameter: an argument left out of a call leaves a parameter of any other type the default value of its type - 0, an empty string, Nothing - which is a
value like any other, and gives an Optional Variant its default value when it has one. Code that tests IsMissing of such a parameter looks like it handles the
argument being left out, and never does. MS-VBA compiles it without a word.
A Variant parameter with no default value can be missing, whether it is Optional or not (an argument that was left out, passed on to it), and so can a
ParamArray that was given no arguments: neither is reported.
IsMissing returns True for any value that is the Missing value. A variable holds it only when it is a Variant that was assigned a missing argument, so
IsMissing of a variable that is not a Variant, or of a constant, is always False too. A Variant variable is not reported.
How it is raised
| Situation | Example | Resolution |
|---|---|---|
The parameter is not a Variant. |
Sub Report(Optional Copies As Long) … If IsMissing(Copies) Then |
Give it a default value and test for that (Optional Copies As Long = 1), or declare it As Variant. |
The parameter is an Optional Variant with a default value. |
Sub Report(Optional Title As Variant = "") … If IsMissing(Title) Then |
Test for the default value, or remove it so that IsMissing can tell. |
A variable that is not a Variant, or a constant. |
Dim Copies As Long … If IsMissing(Copies) Then |
Test the parameter the value came from, or remove the test. |
The results are MS-VBA's, recorded in Excel: IsMissing of an Optional n As Long, an Optional s As String, an Optional o As Object and an Optional v As Variant = 5 is False when the argument is left out; of an Optional v As Variant, and of a ParamArray given nothing, it is True.
Quick fix
The diagnostic comes with a quick fix, Replace 'IsMissing(…)' with False, which writes False in place of the call (If Not IsMissing(Copies) Then becomes
If Not False Then). The code does what it did: the call is always False, and its argument is a name, whose evaluation has no other effect. The condition that remains can then be rewritten or
removed.
See also
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca