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 redundant variable initializations #2732

Open
retailcoder opened this issue Feb 23, 2017 · 1 comment
Open

Inspection for redundant variable initializations #2732

retailcoder opened this issue Feb 23, 2017 · 1 comment

Comments

@retailcoder
Copy link
Member

@retailcoder retailcoder commented Feb 23, 2017

If a variable's first assignment (taking code paths into account) is a literal with the default value for that variable's declared type, Rubberduck should suggest removing that redundant assignment.

    Dim foo As String
    foo = vbNullString ' or ""

    Dim foo As Integer ' or any numeric type actually
    foo = 0

    Dim foo As Boolean
    foo = False

    Dim foo As Object ' or any reference type
    Set foo = Nothing

    Dim foo As Date
    foo = 0 ' 1899-12-30 12:00:00AM actually, so #1899-12-30#

    Dim foo As Variant
    foo = Empty ' vbEmpty still makes "(foo = vbEmpty)" evaluate to True, but converts to 0
@SystemsModelling
Copy link

@SystemsModelling SystemsModelling commented Feb 25, 2017

Does that conflict with another inspection?

Dim foo As String
If condition then
    foo="foo"
End If

could raise the inspection "foo may not be assigned a value"

or

Dim foo As Long
For i=1 to 3
    foo=foo + i 
End If

could raise "foo is referenced before being assigned a value"

I may be misremembering some similar inspection in another parser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants