Skip to content

Commit 7633842

Browse files
authored
Make PossibleIncorrectComparisonWithNull rule return a SuggestCorrection for auto-fixes in VS-Code or via the -Fix switch (#1115)
* Make PossibleIncorrectComparisonWithNull rule return a SuggestCorrection for auto-fixed in VS-Code or via the -Fix switch * trigger ci
1 parent c62b8b2 commit 7633842

4 files changed

+37
-4
lines changed

Rules/PossibleIncorrectComparisonWithNull.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
3838
{
3939
if (IncorrectComparisonWithNull(binExpressionAst, ast))
4040
{
41-
yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
41+
yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName,
42+
null, suggestedCorrections: GetCorrectionExtent(binExpressionAst));
4243
}
4344
}
4445
}
@@ -60,7 +61,8 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
6061
{
6162
if (IncorrectComparisonWithNull(binAst, funcAst))
6263
{
63-
yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
64+
yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName,
65+
null, suggestedCorrections: GetCorrectionExtent(binAst));
6466
}
6567
}
6668
}
@@ -101,6 +103,21 @@ private bool IncorrectComparisonWithNull(BinaryExpressionAst binExpressionAst, A
101103
return false;
102104
}
103105

106+
private IEnumerable<CorrectionExtent> GetCorrectionExtent(BinaryExpressionAst binaryExpressionAst)
107+
{
108+
var correction = new CorrectionExtent(
109+
binaryExpressionAst.Extent.StartLineNumber,
110+
binaryExpressionAst.Extent.EndLineNumber,
111+
binaryExpressionAst.Extent.StartColumnNumber,
112+
binaryExpressionAst.Extent.EndColumnNumber,
113+
$"{binaryExpressionAst.Right.Extent.Text} {binaryExpressionAst.ErrorPosition.Text} {binaryExpressionAst.Left.Extent.Text}",
114+
binaryExpressionAst.Extent.File,
115+
Strings.PossibleIncorrectComparisonWithNullSuggesteCorrectionDescription
116+
);
117+
118+
yield return correction;
119+
}
120+
104121
/// <summary>
105122
/// GetName: Retrieves the name of this rule.
106123
/// </summary>

Rules/Strings.Designer.cs

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rules/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,4 +990,7 @@
990990
<data name="PossibleIncorrectUsageOfRedirectionOperatorName" xml:space="preserve">
991991
<value>PossibleIncorrectUsageOfRedirectionOperator</value>
992992
</data>
993+
<data name="PossibleIncorrectComparisonWithNullSuggesteCorrectionDescription" xml:space="preserve">
994+
<value>Use $null on the left hand side for safe comparison with $null.</value>
995+
</data>
993996
</root>

Tests/Rules/PossibleIncorrectComparisonWithNull.tests.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Describe "PossibleIncorrectComparisonWithNull" {
1313
It "has the correct description message" {
1414
$violations.Message | Should -Match $violationMessage
1515
}
16+
17+
It "has the correct description message" {
18+
$violations[0].SuggestedCorrections[0].Text | Should -Be '$null -eq @("dfd", "eee")'
19+
}
1620
}
1721

1822
Context "When there are no violations" {

0 commit comments

Comments
 (0)