@@ -108,21 +108,18 @@ public override void Initialize(AnalysisContext context)
108108 private static void HandleSyntaxTree ( SyntaxTreeAnalysisContext context )
109109 {
110110 var syntaxRoot = context . Tree . GetRoot ( context . CancellationToken ) ;
111- var previousCommentNotOnOwnLine = false ;
112111
113112 foreach ( var trivia in syntaxRoot . DescendantTrivia ( ) . Where ( trivia => trivia . IsKind ( SyntaxKind . SingleLineCommentTrivia ) ) )
114113 {
115114 if ( trivia . FullSpan . Start == 0 )
116115 {
117116 // skip the trivia if it is at the start of the file
118- previousCommentNotOnOwnLine = false ;
119117 continue ;
120118 }
121119
122120 if ( trivia . ToString ( ) . StartsWith ( "////" , StringComparison . Ordinal ) )
123121 {
124122 // ignore commented out code
125- previousCommentNotOnOwnLine = false ;
126123 continue ;
127124 }
128125
@@ -132,26 +129,21 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
132129 if ( ! IsOnOwnLine ( triviaList , triviaIndex ) )
133130 {
134131 // ignore comments after other code elements.
135- previousCommentNotOnOwnLine = true ;
136132 continue ;
137133 }
138134
139135 if ( IsPrecededByBlankLine ( triviaList , triviaIndex ) )
140136 {
141137 // allow properly formatted blank line comments.
142- previousCommentNotOnOwnLine = false ;
143138 continue ;
144139 }
145140
146- if ( ! previousCommentNotOnOwnLine && IsPrecededBySingleLineCommentOrDocumentation ( triviaList , triviaIndex ) )
141+ if ( IsPrecededBySingleLineCommentOnOwnLineOrDocumentation ( triviaList , triviaIndex ) )
147142 {
148143 // allow consecutive single line comments.
149- previousCommentNotOnOwnLine = false ;
150144 continue ;
151145 }
152146
153- previousCommentNotOnOwnLine = false ;
154-
155147 if ( IsAtStartOfScope ( trivia ) )
156148 {
157149 // allow single line comment at scope start.
@@ -174,7 +166,8 @@ private static bool IsOnOwnLine<T>(T triviaList, int triviaIndex)
174166 {
175167 while ( triviaIndex >= 0 )
176168 {
177- if ( triviaList [ triviaIndex ] . IsKind ( SyntaxKind . EndOfLineTrivia ) )
169+ var currentTrivia = triviaList [ triviaIndex ] ;
170+ if ( currentTrivia . IsKind ( SyntaxKind . EndOfLineTrivia ) )
178171 {
179172 return true ;
180173 }
@@ -185,7 +178,7 @@ private static bool IsOnOwnLine<T>(T triviaList, int triviaIndex)
185178 return false ;
186179 }
187180
188- private static bool IsPrecededBySingleLineCommentOrDocumentation < T > ( T triviaList , int triviaIndex )
181+ private static bool IsPrecededBySingleLineCommentOnOwnLineOrDocumentation < T > ( T triviaList , int triviaIndex )
189182 where T : IReadOnlyList < SyntaxTrivia >
190183 {
191184 var eolCount = 0 ;
@@ -206,6 +199,8 @@ private static bool IsPrecededBySingleLineCommentOrDocumentation<T>(T triviaList
206199 break ;
207200
208201 case SyntaxKind . SingleLineCommentTrivia :
202+ return IsOnOwnLine ( triviaList , triviaIndex ) ;
203+
209204 case SyntaxKind . SingleLineDocumentationCommentTrivia :
210205 return true ;
211206
0 commit comments