Skip to content

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

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

Closed
bergmeister opened this issue Feb 7, 2018 · 0 comments · Fixed by #881
Assignees

Comments

@bergmeister
Copy link
Collaborator

bergmeister commented 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# 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
@bergmeister 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
@bergmeister bergmeister self-assigned this Feb 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants