PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.8k stars 366 forks source link

Revert or Redo PossibleIncorrectUsageOfRedirectionOperator #1975

Closed etamasdan-nearform closed 2 months ago

etamasdan-nearform commented 4 months ago

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: https://github.com/PowerShell/PSScriptAnalyzer/pull/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 commented 3 months ago

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.

Hope that helps 😀!