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

Revert or Redo PossibleIncorrectUsageOfRedirectionOperator #1975

Closed
nf-developer opened this issue Feb 28, 2024 · 1 comment
Closed

Revert or Redo PossibleIncorrectUsageOfRedirectionOperator #1975

nf-developer opened this issue Feb 28, 2024 · 1 comment

Comments

@nf-developer
Copy link

Summary of the new feature

As a developer I have valid Powershell code in my if-statement and I expect zero warnings but my IDE is displaying a warning:

if (!(command 2> $null)) {
   ...
}

The above code has 2> $null highlighted with the warning: PossibleIncorrectUsageOfRedirectionOperator.

Proposed technical implementation details (optional)

Revert or redo this offending PR: #881

What is the latest version of PSScriptAnalyzer at the point of writing

How do I obtain this? The one that comes with VS Code > ms-vscode's PowerShell extension.

@liamjpeters
Copy link
Contributor

Hey @etamasdan-nearform 👋,

The intent of the PossibleIncorrectUsageOfRedirectionOperator rule is to catch possible incorrect usage.

A Redirect operator in the clause of an if-statement is pretty niche.

I appreciate you're using it intentionally here, but in most cases the redirect operator is used in error - by those more familiar with other coding languages or switching coding languages frequently. I include myself as someone that has used it in error many times!

I'd, personally, resolve this in my script by first capturing the command output, then performing the check.

$commandOutput = command 2> $null
if (!$commandOutput) {
   ...
}

You can also tell PSScriptAnalyzer, in a few ways, to ignore this rule if it isn't to your personal preference.

  • Supress the rule explicitly in the file or function in which the code appears:

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSPossibleIncorrectUsageOfRedirectionOperator")]
    Param()
  • Perhaps more sustainably, you could create a settings file that has your own preferences in it. Something like the below perhaps?

    # PSScriptAnalyzerSettings.psd1
    @{
        ExcludeRules=@(
            'PSPossibleIncorrectUsageOfRedirectionOperator'
        )
    }

    Then you could tell the Powershell extension in VSCode to use that settings file.

    "powershell.scriptAnalysis.settingsPath": ""

Hope that helps 😀!

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

3 participants