PSScriptAnalyzer icon indicating copy to clipboard operation
PSScriptAnalyzer copied to clipboard

Rule request: PossibleArgumentWithOperatorConfusion

Open iRon7 opened this issue 7 months ago • 1 comments

PowerShell has two parsing modes:

It is not always clear whenever PowerShell is in argument mode, as in:

if (&{ (1,2,3).Where{ $_ -eq 4 } } -isnot [int]) { Throw "The expression should return an integer"

For background information, see: Call operator (&{}) with no results doesn't type compare #24054

Yet from the argument name (and position and the operators) you could make a reasonable assumption that it probably concerns an operator instead.

For a (PowerShell based) rule prototype, see: https://github.com/iRon7/PSRules/blob/main/PossibleArgumentWithOperatorConfusion.psm1

Invoke-ScriptAnalyzer -CustomRulePath $RulePath -ScriptDefinition 'if (&{ (1,2,3).Where{ $_ -eq 4 } } -isnot [int]) { Throw "The expression should return an integer" }' | Select-Object *

Line                 : 1
Column               : 5
Message              : Possible argument with operator (-isnot) confusion.
Extent               : &{ (1,2,3).Where{ $_ -eq 4 } } -isnot [int]
RuleName             : PSPossibleArgumentWithOperatorConfusion
Severity             : Warning
ScriptName           :
ScriptPath           :
RuleSuppressionID    :
SuggestedCorrections : {(&{ (1,2,3).Where{ $_ -eq 4} }) -isnot [int]}
IsSuppressed         : False

iRon7 avatar Jul 16 '24 12:07 iRon7