Rubberduck: Obsolete While...Wend loop inspection

Created on 15 Nov 2018  路  3Comments  路  Source: rubberduck-vba/Rubberduck

The While...Wend construct was made obsolete with the introduction of Do While...Loop blocks, forever ago. Rubberduck should have an inspection that flags them, and a quickfix that replaces While...Wend with Do While...Loop.

Obsolete While...Wend loop
'While...Wend' loops cannot be prematurely exited without a 'GoTo' statement. Use the equivalent and more modern 'Do While...Loop' construct instead, which can be exited with an 'Exit Do' statement.

Therefore, a further enhancement (involving a bit of code path analysis) could look for a line label/number immediately after Wend:

While foo
    DoSomething foo
    If bar = 42 Then GoTo ExitWhile
Wend
ExitWhile:
'...

...and replace it with an Exit Do statement:

Do While foo
    DoSomething foo
    If bar = 42 Then Exit Do
Loop
'...

(removing the line label/number if it's left unreferenced)

difficulty-02-ducky enhancement feature-inspection-quickfixes feature-inspections up-for-grabs

Most helpful comment

Bonus: If it's While Not foo, convert to Do Until foo instead. Let's encourage people to not double-negative themselves!

All 3 comments

Bonus: If it's While Not foo, convert to Do Until foo instead. Let's encourage people to not double-negative themselves!

Linking chat for context. :)

Just to clarify - a first iteration/implementation of the inspection+quickfix can very well just flag While...Wend and replace with Do While...Loop without altering the rest of the code/logic. The rest is nice-to-have, cherry-on-top sugar.

Was this page helpful?
0 / 5 - 0 ratings