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
Warn when using FileRedirection operator inside if/while statements and improve new PossibleIncorrectUsageOfAssignmentOperator rule (#881)
* first working prototype that warns against usage of the redirection operator inside if statements.
TODO: rename rule, adapt documentation and error message strings
* adapt error message strings and add tests
* remove old todo comment
* remove duplicated test case
* syntax fix from last cleanup commits
* tweak extents in error message: for equal sign warnings, it will point to the equal sign (instead of the RHS only) and fore file redirections it will be the redirection ast (e.g. '> $b')
* Enhance check for assignment by rather checking if assigned variable is used in assignment block
to avoid false positives for assignments: check if variable on LHS is used in statement block
update documentation and change level to warning since we are now much more certain that a violation. is happening
Commits:
* prototype of detecting variable usage on LHS to avoid false positives
* simplify and reduce nesting
* fix regression by continue statements
* fix last regression
* add simple test case for new improvemeent
* finish it off and adapt tests
* update docs
* minor style update
* fix typo
* fix test due to warning level change
* tweak messages and readme
* update to pester v4 syntax
* Revert to not check assigned variable usage of RHS but add optional clang suppression, split rule and enhance assignment operator rule to not warn for more special cases on the RHS
* Minor fix resource variable naming
* uncommented accidental comment out of ipmo pssa in tests
* do not exclude BinaryExpressionAst on RHS because this case is the chance that it is by design is much smaller, therefore rather having some false positives is preferred
* update test to test against clang suppression
* make clang suppression work again
* reword warning text to use 'equality operator' instead of 'equals operator'
* Always warn when LHS is $null
* Enhance PossibleIncorrectUsageOfAssignmentOperator rule to do the same analysis for while and do-while statements as well, which is the exact same use case as if statements
* tweak message to be more generic in terms of the conditional statement
* Address PR comments
In many programming languages, the equality operator is denoted as `==` or `=` in many programming languages, but `PowerShell` uses `-eq`. Therefore it can easily happen that the wrong operator is used unintentionally and this rule catches a few special cases where the likelihood of that is quite high.
8
+
9
+
The rule looks for usages of `==` and `=` operators inside `if`, `else if`, `while` and `do-while` statements but it will not warn if any kind of command or expression is used at the right hand side as this is probably by design.
10
+
11
+
## Example
12
+
13
+
### Wrong
14
+
15
+
````PowerShell
16
+
if ($a = $b)
17
+
{
18
+
...
19
+
}
20
+
````
21
+
22
+
````PowerShell
23
+
if ($a == $b)
24
+
{
25
+
26
+
}
27
+
````
28
+
29
+
### Correct
30
+
31
+
````PowerShell
32
+
if ($a -eq $b) # Compare $a with $b
33
+
{
34
+
...
35
+
}
36
+
````
37
+
38
+
````PowerShell
39
+
if ($a = Get-Something) # Only execute action if command returns something and assign result to variable
40
+
{
41
+
Do-SomethingWith $a
42
+
}
43
+
````
44
+
45
+
## Implicit suppresion using Clang style
46
+
47
+
There are some rare cases where assignment of variable inside an if statement is by design. Instead of suppression the rule, one can also signal that assignment was intentional by wrapping the expression in extra parenthesis. An exception for this is when `$null` is used on the LHS is used because there is no use case for this.
48
+
49
+
````powershell
50
+
if (($shortVariableName = $SuperLongVariableName['SpecialItem']['AnotherItem']))
In many programming languages, the comparison operator for 'greater than' is `>` but `PowerShell` uses `-gt` for it and `-ge` (greater or equal) for `>=`. Therefore it can easily happen that the wrong operator is used unintentionally and this rule catches a few special cases where the likelihood of that is quite high.
8
+
9
+
The rule looks for usages of `>` or `>=` operators inside if, elseif, while and do-while statements because this is likely going to be unintentional usage.
/// The idea behind clang suppresion style is to wrap a statement in extra parenthesis to implicitly suppress warnings of its content to signal that the offending operation was deliberate.
10
+
/// </summary>
11
+
internalstaticclassClangSuppresion
12
+
{
13
+
/// <summary>
14
+
/// The community requested an implicit suppression mechanism that follows the clang style where warnings are not issued if the expression is wrapped in extra parenthesis.
15
+
/// See here for details: https://github.com/Microsoft/clang/blob/349091162fcf2211a2e55cf81db934978e1c4f0c/test/SemaCXX/warn-assignment-condition.cpp#L15-L18
/// PossibleIncorrectUsageOfAssignmentOperator: Warn if someone uses the '=' or '==' by accident in an if statement because in most cases that is not the intention.
16
+
/// PossibleIncorrectUsageOfAssignmentOperator: Warn if someone uses '>', '=' or '==' operators inside an if, elseif, while and do-while statement because in most cases that is not the intention.
17
+
/// The origin of this rule is that people often forget that operators change when switching between different languages such as C# and PowerShell.
// Check if someone used '==', which can easily happen when the person is used to coding a lot in C#.
39
-
// In most cases, this will be a runtime error because PowerShell will look for a cmdlet name starting with '=', which is technically possible to define
// Check if someone used '==', which can easily happen when the person is used to coding a lot in C#.
65
+
// In most cases, this will be a runtime error because PowerShell will look for a cmdlet name starting with '=', which is technically possible to define
// If the RHS contains a CommandAst at some point, then we do not want to warn because this could be intentional in cases like 'if ($a = Get-ChildItem){ }'
// If the RHS contains an InvokeMemberExpressionAst, then we also do not want to warn because this could e.g. be 'if ($f = [System.IO.Path]::GetTempFileName()){ }'
/// PossibleIncorrectUsageOfRedirectionOperator: Warn if someone uses '>' or '>=' inside an if, elseif, while or do-while statement because in most cases that is not the intention.
17
+
/// The origin of this rule is that people often forget that operators change when switching between different languages such as C# and PowerShell.
0 commit comments