Skip to content

Suggestion for new rule - Ensure that variables used in a function is always declared in the local scope #632

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

Open
hallgeirl opened this issue Oct 11, 2016 · 3 comments

Comments

@hallgeirl
Copy link

hallgeirl commented Oct 11, 2016

In order to reduce the number of hard to debug errors, we run our PowerShell scripts with Set-StrictMode -Version Latest. This lets us catch issues where we e.g. have misspelled a variable name (or forgot to assign it before using it) during runtime. But it would be immensely useful to catch these kinds of errors before we run the scripts, e.g. during an automated build.

We have deployment scripts that run for quite a long time (several hours), and silly misspelled variable names or misplaced variable assignments then costs quite a bit of development time. Catching these errors early would be immensely useful.

Example of something that should cause an error:

function Get-Foobar()
{
    if ($b + 2 -gt 5)
    {
        return $b
    }

    return 5
}

This would be OK:

function Get-Foobar($b)
{
    if ($b + 2 -gt 5)
    {
        return $b
    }

    return 5
}

As would this:

function Get-Foobar()
{
    $b = 123;
    if ($b + 2 -gt 5)
    {
        return $b
    }

    return 5
}
@ghidalgo3
Copy link

I agree, this rule would have saved me a lot of time in long-running scripts that cannot be easily reentered. I expect that someone will say "just make sure you spell your variables correctly!" and obviously that defeats the point of the analyzer.

Bumping this suggestion, hope it becomes a feature.

@kapilmb
Copy link

kapilmb commented Dec 2, 2016

Moving this issue to backlog as I think it is closely related to the issue #636. We will revisit this issue after #636 is solved.

@kapilmb kapilmb added this to the backlog milestone Dec 2, 2016
@CJHarmath
Copy link

+1

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

No branches or pull requests

4 participants