Skip to content

Improve/extend new PossibleIncorrectUsageOfAssignmentOperator rule to also catch unintentional usage of '>' and '>=' inside if statement #878

Closed
@bergmeister

Description

@bergmeister

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions