Skip to content

Commit 07b1e14

Browse files
committed
Merge pull request #252 from PowerShell/BugFixes
Merge BugFixes to Master
2 parents 0805538 + 610b12c commit 07b1e14

7 files changed

+119
-12
lines changed

CHANGELOG.MD

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
## Released v1.0.2 (June.25, 2015)
2+
###Features:
3+
- Perf improvements in the Engine to execute rules concurrently.
4+
5+
6+
###Rules:
7+
- New rule to validate the presence of deprecated module manifest fields.
8+
- Removed PSAvoidTrapStatement rule from the builtin set – since this is not deprecated and using trap is a better choice in certain scenarios.
9+
10+
11+
###Fixes:
12+
- Verbose Message rule applies to only DSC cmdlet based resources.
13+
- Multiple fixes to AvoidUninitializedVariable to work with non-mandatory parameters, fix in the flow graphs for throw statements; support for missing preference variables; support for automatic variables.
14+
- PSAvoidUsingInternalsURLs to work with xPath expressions.
15+
- UseSingularNouns rule to raise warnings for plural phrases.
16+
- Added .gitignore to exclude certain files from being reported as untracked.
17+
- Revisited severity for DSC rules.
18+
- PSUseOutputTypeCorrectly rule not to get triggered for functions returning system.void type.
19+
- PSAvoidDefaultTrueValueSwitchParameter to work with switch attribute when supplied as fully qualified.
20+
- Ignore PSObject and PSCustomObject for UseOutputTypeCorrectly rule.
21+
- Only raise NullComparisonRule if the RHS is an array or has unknown type.
22+
- PSUseDeclaredVarsMoreThanAssignments rule to be raised for script variables and for setting the property of a variable.
23+
- Support for using PSUseCmdletCorrectly rule when mandatory parameters are supplied in a splatted hashtable.
24+
- AvoidUsingPlainTextForPassword rule to be raised only strings or object types.
25+
- Fix for PositionalParameterUsed method (Helper.cs) uses unsafe method to exclude ForEach-Object and Where-Object.
26+
27+
128
## Released v1.0.1 (May.8, 2015)
229
###Features:
330
- Integrated with waffle.io for Project Management.
@@ -18,7 +45,6 @@
1845

1946

2047
##Released v1.0.0 on Apr.24, 2015
21-
2248
###Features:
2349
- Finalized three levels of Severity - Error/Warning/Information.
2450
- Improved PSScriptAnalyzer engine behavior: emits non-terminating errors (Ex: for failed ast parse) and continues rule application when running on multiple scripts.

Engine/Helper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ internal string GetTypeFromMemberExpressionAstHelper(MemberExpressionAst memberA
679679
/// <param name="varAst"></param>
680680
/// <param name="ast"></param>
681681
/// <returns></returns>
682-
internal Type GetTypeFromAnalysis(VariableExpressionAst varAst, Ast ast)
682+
public Type GetTypeFromAnalysis(VariableExpressionAst varAst, Ast ast)
683683
{
684684
try
685685
{

Engine/PSScriptAnalyzer.psd1

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Author = 'Microsoft Corporation'
1111
RootModule = 'Microsoft.Windows.PowerShell.ScriptAnalyzer.dll'
1212

1313
# Version number of this module.
14-
ModuleVersion = '1.0.1'
14+
ModuleVersion = '1.0.2'
1515

1616
# ID used to uniquely identify this module
1717
GUID = '324fc715-36bf-4aee-8e58-72e9b4a08ad9'

Rules/PossibleIncorrectComparisonWithNull.cs

+58-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//
1212

1313
using System;
14+
using System.Linq;
1415
using System.Collections.Generic;
1516
using System.Management.Automation.Language;
1617
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
@@ -33,17 +34,67 @@ public class PossibleIncorrectComparisonWithNull : IScriptRule {
3334
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
3435
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
3536

36-
IEnumerable<Ast> binExpressionAsts = ast.FindAll(testAst => testAst is BinaryExpressionAst, true);
37+
IEnumerable<Ast> binExpressionAsts = ast.FindAll(testAst => testAst is BinaryExpressionAst, false);
3738

38-
if (binExpressionAsts != null) {
39-
foreach (BinaryExpressionAst binExpressionAst in binExpressionAsts) {
40-
if ((binExpressionAst.Operator.Equals(TokenKind.Equals) || binExpressionAst.Operator.Equals(TokenKind.Ceq)
41-
|| binExpressionAst.Operator.Equals(TokenKind.Cne) || binExpressionAst.Operator.Equals(TokenKind.Ine) || binExpressionAst.Operator.Equals(TokenKind.Ieq))
42-
&& binExpressionAst.Right.Extent.Text.Equals("$null", StringComparison.OrdinalIgnoreCase)) {
43-
yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
39+
foreach (BinaryExpressionAst binExpressionAst in binExpressionAsts) {
40+
if ((binExpressionAst.Operator.Equals(TokenKind.Equals) || binExpressionAst.Operator.Equals(TokenKind.Ceq)
41+
|| binExpressionAst.Operator.Equals(TokenKind.Cne) || binExpressionAst.Operator.Equals(TokenKind.Ine) || binExpressionAst.Operator.Equals(TokenKind.Ieq))
42+
&& binExpressionAst.Right.Extent.Text.Equals("$null", StringComparison.OrdinalIgnoreCase))
43+
{
44+
if (IncorrectComparisonWithNull(binExpressionAst, ast))
45+
{
46+
yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
4447
}
4548
}
4649
}
50+
51+
IEnumerable<Ast> funcAsts = ast.FindAll(item => item is FunctionDefinitionAst, true).Union(ast.FindAll(item => item is FunctionMemberAst, true));
52+
foreach (Ast funcAst in funcAsts)
53+
{
54+
IEnumerable<Ast> binAsts = funcAst.FindAll(item => item is BinaryExpressionAst, true);
55+
foreach (BinaryExpressionAst binAst in binAsts)
56+
{
57+
if (IncorrectComparisonWithNull(binAst, funcAst))
58+
{
59+
yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
60+
}
61+
}
62+
}
63+
}
64+
65+
private bool IncorrectComparisonWithNull(BinaryExpressionAst binExpressionAst, Ast ast)
66+
{
67+
if ((binExpressionAst.Operator.Equals(TokenKind.Equals) || binExpressionAst.Operator.Equals(TokenKind.Ceq)
68+
|| binExpressionAst.Operator.Equals(TokenKind.Cne) || binExpressionAst.Operator.Equals(TokenKind.Ine) || binExpressionAst.Operator.Equals(TokenKind.Ieq))
69+
&& binExpressionAst.Right.Extent.Text.Equals("$null", StringComparison.OrdinalIgnoreCase))
70+
{
71+
if (binExpressionAst.Left.StaticType.IsArray)
72+
{
73+
return true;
74+
}
75+
else if (binExpressionAst.Left is VariableExpressionAst)
76+
{
77+
// ignores if the variable is a special variable
78+
if (!Helper.Instance.HasSpecialVars((binExpressionAst.Left as VariableExpressionAst).VariablePath.UserPath))
79+
{
80+
Type lhsType = Helper.Instance.GetTypeFromAnalysis(binExpressionAst.Left as VariableExpressionAst, ast);
81+
if (lhsType == null)
82+
{
83+
return true;
84+
}
85+
else if (lhsType.IsArray || lhsType == typeof(object) || lhsType == typeof(Undetermined) || lhsType == typeof(Unreached))
86+
{
87+
return true;
88+
}
89+
}
90+
}
91+
else if (binExpressionAst.Left.StaticType == typeof(object))
92+
{
93+
return true;
94+
}
95+
}
96+
97+
return false;
4798
}
4899

49100
/// <summary>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
function CompareWithNull {
22
if ($DebugPreference -eq $null) {
33
}
4+
}
5+
6+
if (@("dfd", "eee") -eq $null)
7+
{
8+
}
9+
10+
if ($randomUninitializedVariable -eq $null)
11+
{
12+
}
13+
14+
function Test
15+
{
16+
$b = "dd", "ddfd";
17+
if ($b -ceq $null)
18+
{
19+
if ("dd","ee" -eq $null)
20+
{
21+
}
22+
}
423
}

Tests/Rules/PossibleIncorrectComparisonWithNull.tests.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ $noViolations = Invoke-ScriptAnalyzer $directory\PossibleIncorrectComparisonWith
77

88
Describe "PossibleIncorrectComparisonWithNull" {
99
Context "When there are violations" {
10-
It "has 1 possible incorrect comparison with null violation" {
11-
$violations.Count | Should Be 1
10+
It "has 4 possible incorrect comparison with null violation" {
11+
$violations.Count | Should Be 4
1212
}
1313

1414
It "has the correct description message" {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
function CompareWithNull {
22
if ($null -eq $DebugPreference) {
33
}
4+
if ($DebugPreference -eq $null) {
5+
}
6+
}
7+
8+
$a = 3
9+
10+
if ($a -eq $null)
11+
{
12+
if (3 -eq $null)
13+
{
14+
}
415
}

0 commit comments

Comments
 (0)