diff --git a/Rules/PossibleIncorrectComparisonWithNull.cs b/Rules/PossibleIncorrectComparisonWithNull.cs index 1928538fa..74db4f523 100644 --- a/Rules/PossibleIncorrectComparisonWithNull.cs +++ b/Rules/PossibleIncorrectComparisonWithNull.cs @@ -38,7 +38,8 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { { if (IncorrectComparisonWithNull(binExpressionAst, ast)) { - yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName); + yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, + null, suggestedCorrections: GetCorrectionExtent(binExpressionAst)); } } } @@ -60,7 +61,8 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { { if (IncorrectComparisonWithNull(binAst, funcAst)) { - yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName); + yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, + null, suggestedCorrections: GetCorrectionExtent(binAst)); } } } @@ -101,6 +103,21 @@ private bool IncorrectComparisonWithNull(BinaryExpressionAst binExpressionAst, A return false; } + private IEnumerable GetCorrectionExtent(BinaryExpressionAst binaryExpressionAst) + { + var correction = new CorrectionExtent( + binaryExpressionAst.Extent.StartLineNumber, + binaryExpressionAst.Extent.EndLineNumber, + binaryExpressionAst.Extent.StartColumnNumber, + binaryExpressionAst.Extent.EndColumnNumber, + $"{binaryExpressionAst.Right.Extent.Text} {binaryExpressionAst.ErrorPosition.Text} {binaryExpressionAst.Left.Extent.Text}", + binaryExpressionAst.Extent.File, + Strings.PossibleIncorrectComparisonWithNullSuggesteCorrectionDescription + ); + + yield return correction; + } + /// /// GetName: Retrieves the name of this rule. /// diff --git a/Rules/Strings.Designer.cs b/Rules/Strings.Designer.cs index edb0a29f4..530969d87 100644 --- a/Rules/Strings.Designer.cs +++ b/Rules/Strings.Designer.cs @@ -537,7 +537,7 @@ internal static string AvoidTrailingWhitespaceName { return ResourceManager.GetString("AvoidTrailingWhitespaceName", resourceCulture); } } - + /// /// Looks up a localized string similar to Module Must Be Loadable. /// @@ -807,7 +807,7 @@ internal static string AvoidUsingEmptyCatchBlockName { return ResourceManager.GetString("AvoidUsingEmptyCatchBlockName", resourceCulture); } } - + /// /// Looks up a localized string similar to Avoid Using Internal URLs. /// @@ -1492,6 +1492,15 @@ internal static string PossibleIncorrectComparisonWithNullName { } } + /// + /// Looks up a localized string similar to Use $null on the left hand side for safe comparison with $null.. + /// + internal static string PossibleIncorrectComparisonWithNullSuggesteCorrectionDescription { + get { + return ResourceManager.GetString("PossibleIncorrectComparisonWithNullSuggesteCorrectionDescription", resourceCulture); + } + } + /// /// Looks up a localized string similar to '=' is not an assignment operator. Did you mean the equality operator '-eq'?. /// diff --git a/Rules/Strings.resx b/Rules/Strings.resx index 56cdb1794..88fe6f0a1 100644 --- a/Rules/Strings.resx +++ b/Rules/Strings.resx @@ -990,4 +990,7 @@ PossibleIncorrectUsageOfRedirectionOperator + + Use $null on the left hand side for safe comparison with $null. + \ No newline at end of file diff --git a/Tests/Rules/PossibleIncorrectComparisonWithNull.tests.ps1 b/Tests/Rules/PossibleIncorrectComparisonWithNull.tests.ps1 index f00d3129a..5c9f45553 100644 --- a/Tests/Rules/PossibleIncorrectComparisonWithNull.tests.ps1 +++ b/Tests/Rules/PossibleIncorrectComparisonWithNull.tests.ps1 @@ -13,6 +13,10 @@ Describe "PossibleIncorrectComparisonWithNull" { It "has the correct description message" { $violations.Message | Should -Match $violationMessage } + + It "has the correct description message" { + $violations[0].SuggestedCorrections[0].Text | Should -Be '$null -eq @("dfd", "eee")' + } } Context "When there are no violations" {