Skip to content

Commit 11507fc

Browse files
author
Quoc Truong
committed
Merge pull request #231 from PowerShell/FixRuleSuppressionErrorsForMoreThanOneVariable
Simplifies logic of suppressing rule and fix a bug where rule suppression does not work for more than one variable
2 parents be47a8e + 8b973b9 commit 11507fc

File tree

2 files changed

+44
-73
lines changed

2 files changed

+44
-73
lines changed

Engine/Helper.cs

Lines changed: 35 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -859,102 +859,64 @@ public Tuple<List<SuppressedRecord>, List<DiagnosticRecord>> SuppressRule(string
859859
List<RuleSuppression> ruleSuppressions = ruleSuppressionsDict[ruleName];
860860

861861
int recordIndex = 0;
862-
int ruleSuppressionIndex = 0;
863-
DiagnosticRecord record = diagnostics.First();
864-
RuleSuppression ruleSuppression = ruleSuppressions.First();
865-
int suppressionCount = 0;
862+
int startRecord = 0;
863+
bool[] suppressed = new bool[diagnostics.Count];
866864

867-
while (recordIndex < diagnostics.Count)
865+
foreach (RuleSuppression ruleSuppression in ruleSuppressions)
868866
{
869-
if (!String.IsNullOrWhiteSpace(ruleSuppression.Error))
867+
int suppressionCount = 0;
868+
while (startRecord < diagnostics.Count && diagnostics[startRecord].Extent.StartOffset < ruleSuppression.StartOffset)
870869
{
871-
ruleSuppressionIndex += 1;
870+
startRecord += 1;
871+
}
872+
873+
// at this point, start offset of startRecord is greater or equals to rulesuppression.startoffset
874+
recordIndex = startRecord;
875+
876+
while (recordIndex < diagnostics.Count)
877+
{
878+
DiagnosticRecord record = diagnostics[recordIndex];
872879

873-
if (ruleSuppressionIndex == ruleSuppressions.Count)
880+
if (record.Extent.EndOffset > ruleSuppression.EndOffset)
874881
{
875882
break;
876883
}
877884

878-
ruleSuppression = ruleSuppressions[ruleSuppressionIndex];
879-
suppressionCount = 0;
880-
881-
continue;
882-
}
883-
884-
// if the record precedes the rule suppression then we don't apply the suppression
885-
// so we check that start of record is greater than start of suppression
886-
if (record.Extent.StartOffset >= ruleSuppression.StartOffset)
887-
{
888-
// end of the rule suppression is less than the record start offset so move on to next rule suppression
889-
if (ruleSuppression.EndOffset < record.Extent.StartOffset)
885+
if (string.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID))
890886
{
891-
ruleSuppressionIndex += 1;
892-
893-
// If we cannot found any error but the rulesuppression has a rulesuppressionid then it must be used wrongly
894-
if (!String.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID) && suppressionCount == 0)
895-
{
896-
ruleSuppression.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSuppression.StartAttributeLine,
897-
System.IO.Path.GetFileName(record.Extent.File), String.Format(Strings.RuleSuppressionIDError, ruleSuppression.RuleSuppressionID));
898-
Helper.Instance.MyCmdlet.WriteError(new ErrorRecord(new ArgumentException(ruleSuppression.Error), ruleSuppression.Error, ErrorCategory.InvalidArgument, ruleSuppression));
899-
}
900-
901-
if (ruleSuppressionIndex == ruleSuppressions.Count)
902-
{
903-
break;
904-
}
905-
906-
ruleSuppression = ruleSuppressions[ruleSuppressionIndex];
907-
suppressionCount = 0;
908-
909-
continue;
887+
suppressed[recordIndex] = true;
888+
suppressionCount += 1;
910889
}
911-
// at this point, the record is inside the interval
912890
else
913891
{
914-
// if the rule suppression id from the rule suppression is not null and the one from diagnostic record is not null
915-
// and they are they are not the same then we cannot ignore the record
916-
if (!string.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID) && !string.IsNullOrWhiteSpace(record.RuleSuppressionID)
917-
&& !string.Equals(ruleSuppression.RuleSuppressionID, record.RuleSuppressionID, StringComparison.OrdinalIgnoreCase))
918-
{
919-
suppressionCount -= 1;
920-
unSuppressedRecords.Add(record);
921-
}
922-
// otherwise, we suppress the record, move on to the next.
923-
else
892+
//if there is a rule suppression id, we only suppressed if it matches
893+
if (!String.IsNullOrWhiteSpace(record.RuleSuppressionID) &&
894+
string.Equals(ruleSuppression.RuleSuppressionID, record.RuleSuppressionID, StringComparison.OrdinalIgnoreCase))
924895
{
896+
suppressed[recordIndex] = true;
925897
suppressedRecords.Add(new SuppressedRecord(record, ruleSuppression));
898+
suppressionCount += 1;
926899
}
927900
}
928-
}
929-
else
930-
{
931-
unSuppressedRecords.Add(record);
932-
}
933901

934-
// important assumption: this point is reached only if we want to move to the next record
935-
recordIndex += 1;
936-
suppressionCount += 1;
902+
recordIndex += 1;
903+
}
937904

938-
if (recordIndex == diagnostics.Count)
905+
// If we cannot found any error but the rulesuppression has a rulesuppressionid then it must be used wrongly
906+
if (!String.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID) && suppressionCount == 0)
939907
{
940-
// If we cannot found any error but the rulesuppression has a rulesuppressionid then it must be used wrongly
941-
if (!String.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID) && suppressionCount == 0)
942-
{
943-
ruleSuppression.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSuppression.StartAttributeLine,
944-
System.IO.Path.GetFileName(record.Extent.File), String.Format(Strings.RuleSuppressionIDError, ruleSuppression.RuleSuppressionID));
945-
Helper.Instance.MyCmdlet.WriteError(new ErrorRecord(new ArgumentException(ruleSuppression.Error), ruleSuppression.Error, ErrorCategory.InvalidArgument, ruleSuppression));
946-
}
947-
948-
break;
908+
ruleSuppression.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSuppression.StartAttributeLine,
909+
System.IO.Path.GetFileName(diagnostics.First().Extent.File), String.Format(Strings.RuleSuppressionIDError, ruleSuppression.RuleSuppressionID));
910+
Helper.Instance.MyCmdlet.WriteError(new ErrorRecord(new ArgumentException(ruleSuppression.Error), ruleSuppression.Error, ErrorCategory.InvalidArgument, ruleSuppression));
949911
}
950-
951-
record = diagnostics[recordIndex];
952912
}
953913

954-
while (recordIndex < diagnostics.Count)
914+
for (int i = 0; i < suppressed.Length; i += 1)
955915
{
956-
unSuppressedRecords.Add(diagnostics[recordIndex]);
957-
recordIndex += 1;
916+
if (!suppressed[i])
917+
{
918+
unSuppressedRecords.Add(diagnostics[i]);
919+
}
958920
}
959921

960922
return result;

Tests/Engine/RuleSuppression.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ function SuppressMe ()
1414

1515
}
1616

17+
function SuppressTwoVariables()
18+
{
19+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUninitializedVariable", "b")]
20+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUninitializedVariable", "a")]
21+
Param([string]$a, [int]$b)
22+
{
23+
}
24+
}
25+
1726
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Scope="Class")]
1827
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("pSAvoidUsingInvokeExpression", "")]
1928
class TestClass

0 commit comments

Comments
 (0)