@@ -716,25 +716,23 @@ internal List<RuleSuppression> GetSuppressionsClass(TypeDefinitionAst typeAst)
716716 }
717717
718718 /// <summary>
719- /// Suppress the rules from the diagnostic records list and return the result
719+ /// Suppress the rules from the diagnostic records list by attaching rule suppression to the record that is suppressed
720720 /// </summary>
721721 /// <param name="ruleSuppressions"></param>
722722 /// <param name="diagnostics"></param>
723- public List < DiagnosticRecord > SuppressRule ( string ruleName , Dictionary < string , List < RuleSuppression > > ruleSuppressionsDict , List < DiagnosticRecord > diagnostics )
723+ public void SuppressRule ( string ruleName , Dictionary < string , List < RuleSuppression > > ruleSuppressionsDict , List < DiagnosticRecord > diagnostics )
724724 {
725- List < DiagnosticRecord > results = new List < DiagnosticRecord > ( ) ;
726-
727725 if ( ruleSuppressionsDict == null || ! ruleSuppressionsDict . ContainsKey ( ruleName )
728726 || diagnostics == null || diagnostics . Count == 0 )
729727 {
730- return diagnostics ;
728+ return ;
731729 }
732730
733731 List < RuleSuppression > ruleSuppressions = ruleSuppressionsDict [ ruleName ] ;
734732
735733 if ( ruleSuppressions . Count == 0 )
736734 {
737- return diagnostics ;
735+ return ;
738736 }
739737
740738 int recordIndex = 0 ;
@@ -760,46 +758,49 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
760758 continue ;
761759 }
762760
763- // the record precedes the rule suppression so don't apply the suppression
764- if ( record . Extent . StartOffset < ruleSuppression . StartOffset )
761+ // if the record precedes the rule suppression then we don't apply the suppression
762+ // so we check that start of record is greater than start of suppression
763+ if ( record . Extent . StartOffset >= ruleSuppression . StartOffset )
765764 {
766- results . Add ( record ) ;
767- }
768- // end of the rule suppression is less than the record start offset so move on to next rule suppression
769- else if ( ruleSuppression . EndOffset < record . Extent . StartOffset )
770- {
771- ruleSuppressionIndex += 1 ;
772-
773- // If we cannot found any error but the rulesuppression has a rulesuppressionid then it must be used wrongly
774- if ( ! String . IsNullOrWhiteSpace ( ruleSuppression . RuleSuppressionID ) && suppressionCount == 0 )
765+ // end of the rule suppression is less than the record start offset so move on to next rule suppression
766+ if ( ruleSuppression . EndOffset < record . Extent . StartOffset )
775767 {
776- ruleSuppression . Error = String . Format ( CultureInfo . CurrentCulture , Strings . RuleSuppressionErrorFormat , ruleSuppression . StartAttributeLine ,
777- System . IO . Path . GetFileName ( record . Extent . File ) , String . Format ( Strings . RuleSuppressionIDError , ruleSuppression . RuleSuppressionID ) ) ;
778- Helper . Instance . MyCmdlet . WriteError ( new ErrorRecord ( new ArgumentException ( ruleSuppression . Error ) , ruleSuppression . Error , ErrorCategory . InvalidArgument , ruleSuppression ) ) ;
779- }
768+ ruleSuppressionIndex += 1 ;
780769
781- if ( ruleSuppressionIndex == ruleSuppressions . Count )
782- {
783- break ;
784- }
770+ // If we cannot found any error but the rulesuppression has a rulesuppressionid then it must be used wrongly
771+ if ( ! String . IsNullOrWhiteSpace ( ruleSuppression . RuleSuppressionID ) && suppressionCount == 0 )
772+ {
773+ ruleSuppression . Error = String . Format ( CultureInfo . CurrentCulture , Strings . RuleSuppressionErrorFormat , ruleSuppression . StartAttributeLine ,
774+ System . IO . Path . GetFileName ( record . Extent . File ) , String . Format ( Strings . RuleSuppressionIDError , ruleSuppression . RuleSuppressionID ) ) ;
775+ Helper . Instance . MyCmdlet . WriteError ( new ErrorRecord ( new ArgumentException ( ruleSuppression . Error ) , ruleSuppression . Error , ErrorCategory . InvalidArgument , ruleSuppression ) ) ;
776+ }
785777
786- ruleSuppression = ruleSuppressions [ ruleSuppressionIndex ] ;
787- suppressionCount = 0 ;
778+ if ( ruleSuppressionIndex == ruleSuppressions . Count )
779+ {
780+ break ;
781+ }
788782
789- continue ;
790- }
791- // at this point, the record is inside the interval
792- else
793- {
794- // if the rule suppression id from the rule suppression is not null and the one from diagnostic record is not null
795- // and they are they are not the same then we cannot ignore the record
796- if ( ! string . IsNullOrWhiteSpace ( ruleSuppression . RuleSuppressionID ) && ! string . IsNullOrWhiteSpace ( record . RuleSuppressionID )
797- && ! string . Equals ( ruleSuppression . RuleSuppressionID , record . RuleSuppressionID , StringComparison . OrdinalIgnoreCase ) )
783+ ruleSuppression = ruleSuppressions [ ruleSuppressionIndex ] ;
784+ suppressionCount = 0 ;
785+
786+ continue ;
787+ }
788+ // at this point, the record is inside the interval
789+ else
798790 {
799- results . Add ( record ) ;
800- suppressionCount -= 1 ;
791+ // if the rule suppression id from the rule suppression is not null and the one from diagnostic record is not null
792+ // and they are they are not the same then we cannot ignore the record
793+ if ( ! string . IsNullOrWhiteSpace ( ruleSuppression . RuleSuppressionID ) && ! string . IsNullOrWhiteSpace ( record . RuleSuppressionID )
794+ && ! string . Equals ( ruleSuppression . RuleSuppressionID , record . RuleSuppressionID , StringComparison . OrdinalIgnoreCase ) )
795+ {
796+ suppressionCount -= 1 ;
797+ }
798+ // otherwise, we suppress the record, move on to the next.
799+ else
800+ {
801+ record . Suppression = ruleSuppression ;
802+ }
801803 }
802- // otherwise, we ignore the record, move on to the next.
803804 }
804805
805806 // important assumption: this point is reached only if we want to move to the next record
@@ -821,15 +822,6 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
821822
822823 record = diagnostics [ recordIndex ] ;
823824 }
824-
825- // Add all unprocessed records to results
826- while ( recordIndex < diagnostics . Count )
827- {
828- results . Add ( diagnostics [ recordIndex ] ) ;
829- recordIndex += 1 ;
830- }
831-
832- return results ;
833825 }
834826
835827 #endregion
0 commit comments