Rubberduck
Rubberduck copied to clipboard
Inspection for boolean expression parsed to function expecting string parameter
While typing some code, I inadvertently made the following typo while copying and pasting.
If Right$(prp.Name <> "EmMacro", 7) Then
When I meant to type:
If Right$(prp.Name,7) <> "EmMacro" Then
And Right$(prp.Name <> "EmMacro", 7) will always resolve to either "True" or "False", and those strings will implicitly cast to Boolean values of True and False.
If RD had an inspection to check for boolean expressions passed as string arguments, I'd have spotted this bug sooner.
There is perhaps an opportunity to extend or augment this inspection to check for the use of string-returning functions in boolean conditions. Eg. a statement with a string-returning function that resolves to If "True" Then is valid runtime VBA, but if that function were to resolve to If "Foo" Then then it would result in a runtime error 13 - Type Mismatch
There is also an opportunity to generalize this inspection to something like Implicit cast in arguments to strongly typed function parameter.