forked from PowerShell/PSScriptAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert to not check assigned variable usage of RHS but add optional c…
…lang suppression, split rule and enhance assignment operator rule to not warn for more special cases on the RHS
- Loading branch information
1 parent
b467d10
commit 594414f
Showing
11 changed files
with
361 additions
and
149 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
RuleDocumentation/PossibleIncorrectUsageOAssignmentOperator.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# PossibleIncorrectUsageOfAssignmentOperator | ||
|
||
**Severity Level: Information** | ||
|
||
## Description | ||
|
||
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. | ||
|
||
The rule looks for usages of `==` and `=` operators inside if 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. | ||
|
||
## Example | ||
|
||
### Wrong | ||
|
||
```` PowerShell | ||
if ($a = $b) | ||
{ | ||
... | ||
} | ||
```` | ||
|
||
```` PowerShell | ||
if ($a == $b) | ||
{ | ||
} | ||
```` | ||
|
||
### Correct | ||
|
||
```` PowerShell | ||
if ($a -eq $b) # Compare $a with $b | ||
{ | ||
... | ||
} | ||
```` | ||
|
||
```` PowerShell | ||
if ($a = Get-Something) # Only execute action if command returns something and assign result to variable | ||
{ | ||
Do-SomethingWith $a | ||
} | ||
```` | ||
|
||
## Implicit suppresion using Clang style | ||
|
||
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: | ||
|
||
```` powershell | ||
if (($shortVariableName = $SuperLongVariableName['SpecialItem']['AnotherItem'])) | ||
{ | ||
... | ||
} | ||
```` |
43 changes: 0 additions & 43 deletions
43
RuleDocumentation/PossibleIncorrectUsageOfComparisonOperator.md
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
RuleDocumentation/PossibleIncorrectUsageOfRedirectionOperator.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# PossibleIncorrectUsageOfRedirectionOperator | ||
|
||
**Severity Level: Information** | ||
|
||
## Description | ||
|
||
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. | ||
|
||
The rule looks for usages of `>` or `>=` operators inside if statements because this is likely going to be unintentional usage. | ||
|
||
## Example | ||
|
||
### Wrong | ||
|
||
```` PowerShell | ||
if ($a > $b) | ||
{ | ||
... | ||
} | ||
```` | ||
|
||
### Correct | ||
|
||
```` PowerShell | ||
if ($a -eq $b) | ||
{ | ||
... | ||
} | ||
```` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Management.Automation.Language; | ||
|
||
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer | ||
{ | ||
/// <summary> | ||
/// 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. | ||
/// </summary> | ||
internal static class ClangSuppresion | ||
{ | ||
/// <summary> | ||
/// 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 | ||
/// See here for details: https://github.com/Microsoft/clang/blob/349091162fcf2211a2e55cf81db934978e1c4f0c/test/SemaCXX/warn-assignment-condition.cpp#L15-L18 | ||
/// </summary> | ||
/// <param name="scriptExtent"></param> | ||
/// <returns></returns> | ||
internal static bool ScriptExtendIsWrappedInParenthesis(IScriptExtent scriptExtent) | ||
{ | ||
return scriptExtent.Text.StartsWith("(") && scriptExtent.Text.EndsWith(")"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.