Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inspection for use of variable Is Null #2235

Open
ThunderFrame opened this issue Sep 14, 2016 · 5 comments
Open

Inspection for use of variable Is Null #2235

ThunderFrame opened this issue Sep 14, 2016 · 5 comments
Labels
difficulty-02-ducky Resolving these involves the internal API, but with relatively easy problems to solve. enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. feature-inspections up-for-grabs Use this label in conjunction with a difficulty level label, e.g. difficulty-02-ducky

Comments

@ThunderFrame
Copy link
Member

Runtime errors when code uses:

If varX Is Null Then

'or

If varX = Null Then

Should almost always be:

If IsNull(varX) Then
@retailcoder
Copy link
Member

When are Is Null and = Null ever valid? Can we somehow skip the legit cases?

@retailcoder retailcoder added enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. feature-inspections labels Sep 14, 2016
@comintern
Copy link
Contributor

AFAIK, Is Null should always result in an Object required error. Any comparison with Null will always result in Null. I'd say it really should be 2 inspections though. The first is a problem because Null isn't an object. The second is a problem because the conditional expression is meaningless in the same way as If foo = foo is.

@ThunderFrame
Copy link
Member Author

Same applies for:

If varX Is Empty Then

'but this isn't a runtime error (unless varX Is Nothing), but it is a logical error:

If varX = Empty Then

@retailcoder
Copy link
Member

I can totally see this happening:

Condition 'varX = Null' is always false

@ThunderFrame
Copy link
Member Author

A StackOverflow user got tripped on this:

https://stackoverflow.com/questions/47429509/excel-vba-check-if-number-is-null-doesnt-work-as-expected

Sub testNull()
    Dim a As Variant
    Dim b As Double

    a = Null
    b = 0

    If a = Null Then
        MsgBox (a & "null") 'doesn't print
    ElseIf a <> Null Then
        MsgBox (a & " not null") 'doesn't print
    Else
        MsgBox (b & " don't know") 'prints
    End If

    If b = Null Then
        MsgBox (b & "null") 'doesn't print
    ElseIf b <> Null Then
        MsgBox (b & " not null") 'doesn't print
    Else
        MsgBox (b & " don't know") 'prints
    End If

End Sub

@retailcoder retailcoder added difficulty-02-ducky Resolving these involves the internal API, but with relatively easy problems to solve. up-for-grabs Use this label in conjunction with a difficulty level label, e.g. difficulty-02-ducky labels Nov 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty-02-ducky Resolving these involves the internal API, but with relatively easy problems to solve. enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. feature-inspections up-for-grabs Use this label in conjunction with a difficulty level label, e.g. difficulty-02-ducky
Projects
None yet
Development

No branches or pull requests

3 participants