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

Rule request: PossibleArgumentWithOperatorConfusion #2014

Open
iRon7 opened this issue Jul 16, 2024 · 1 comment
Open

Rule request: PossibleArgumentWithOperatorConfusion #2014

iRon7 opened this issue Jul 16, 2024 · 1 comment

Comments

@iRon7
Copy link

iRon7 commented Jul 16, 2024

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
Copy link
Author

iRon7 commented Jul 25, 2024

I did a number of improvements to my prototype but in fact the scope of the rule could be even more wider and might also applied to (incorrect) statements like:

Get-Date - $StartTime

Get-Date: Cannot bind parameter 'Date'. Cannot convert value "-" to type "System.DateTime". Error: "String '-' was not recognized as a valid DateTime."

Although it would already generate a clear error during run time, it would be nice to get the message and suggested correction during design time:

(Get-Date) - $StartTime

Related: #24079 Add $Now to the automatic variables

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

2 participants