RDC01003: Empty If block
Severity: Information · Family: RDC (Rubberduck Core analyzers) ·
Raised by: EmptyIfBlockAnalyzer
A branch of an If statement has no statement: the If or an ElseIf of a block If
(MS-VBAL §5.4.2.8 If Statement), or the Then of a single-line If that has only an Else
(MS-VBAL §5.4.2.9 Single-line If Statement).
When its condition is True, the branch does nothing. A branch that only an Else follows usually states the condition the wrong way around. Comments, line labels and
Dim, Static and Const declarations are no statements: a branch that holds only those is empty.
The finding is the header of the branch, its keyword and its condition.
How it is raised
| Situation | Example | Resolution |
|---|---|---|
An If with no statement, followed by an Else. |
If foo ThenElse DoSomethingEnd If |
Invert the condition: If Not foo Then, DoSomething, End If. |
An ElseIf with no statement. |
ElseIf n = 2 ThenElse |
Write what it does, or fold its condition into the branch that follows it. |
A single-line If with nothing after Then. |
If foo Then Else DoSomething |
If Not foo Then DoSomething. |
Not inverts a Boolean condition. A number other than 0 and -1 is True, and so is its Not (Not 1 is -2); a Null condition runs the Else, and
its Not is Null too. Inverting such a condition takes more than Not.
Quick fix
A block If with nothing at all in it (no comment, label or declaration), no ElseIf and no Else, comes with a quick fix, Remove the empty If,
where its condition can neither call a procedure nor raise an error. The program does the same without it. Such a condition reads only literals and
variables or parameters of the procedure declared as a number, a Boolean or a Date, compares them, and combines the comparisons with Not, And,
Or, Xor, Eqv, Imp; or tests an object variable with Is (If o Is Nothing Then).
A condition that calls a function, reads a property, a String, a Variant or an element of an array, or computes (n + 1, which can overflow) gets no
quick fix: removing the If would remove what it does. No quick fix inverts a condition either.
See also
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca