You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The unintentional usage of = or == inside if statements also applies to <, <=, > and >= comparison and is even easier to make when switching languages (.e.g. C# to PowerShell). The first 2 cases are already covered by the PowerShell language parser, which will throw the error The '<' operator is reserved for future use at.
Mistakingly using > instead of -gt happened not only to colleagues of mine but also me and in the past (several times) and caused time to be wasted until debugging and stepping through the code found this mistake. To me it makes sense to do this right now before this rule gets shipped the first time in the next version.
Proposal: New rule name: PossibleIncorrectUsageOfComparisonOperator
Implementation: Should be similar to the existing one but the case >= probably needs some heuristics similar to the one for ==. As far as I am aware there should not be a special case that could lead to false positive where people using the redirection operator on purpose. Please comment below if you have a use case.
Test cases to be flagged up:
if ($a > $b){ }
if ($a > ($b)){ }
if ($a > "$b"){ }
if ($a >= $b){ }
if ($a > = $b){ }
if ($a > =$b){ }
same as above for elseif
The text was updated successfully, but these errors were encountered:
bergmeister
changed the title
Improve/extend new PossibleIncorrectUsageOfAssignmentOperator rule to also catch unintentional usage of '>' and '>=' instead of inside if statement
Improve/extend new PossibleIncorrectUsageOfAssignmentOperator rule to also catch unintentional usage of '>' and '>=' inside if statement
Feb 7, 2018
The unintentional usage of
=
or==
inside if statements also applies to<
,<=
,>
and>=
comparison and is even easier to make when switching languages (.e.g.C#
toPowerShell
). The first 2 cases are already covered by the PowerShell language parser, which will throw the errorThe '<' operator is reserved for future use at
.Mistakingly using
>
instead of-gt
happened not only to colleagues of mine but also me and in the past (several times) and caused time to be wasted until debugging and stepping through the code found this mistake. To me it makes sense to do this right now before this rule gets shipped the first time in the next version.Proposal: New rule name:
PossibleIncorrectUsageOfComparisonOperator
Implementation: Should be similar to the existing one but the case
>=
probably needs some heuristics similar to the one for==
. As far as I am aware there should not be a special case that could lead to false positive where people using the redirection operator on purpose. Please comment below if you have a use case.Test cases to be flagged up:
if ($a > $b){ }
if ($a > ($b)){ }
if ($a > "$b"){ }
if ($a >= $b){ }
if ($a > = $b){ }
if ($a > =$b){ }
elseif
The text was updated successfully, but these errors were encountered: