@@ -105,44 +105,50 @@ public override void Initialize(AnalysisContext context)
105105 private static void HandleSyntaxTree ( SyntaxTreeAnalysisContext context )
106106 {
107107 var syntaxRoot = context . Tree . GetRoot ( context . CancellationToken ) ;
108+ var previousCommentNotOnOwnLine = false ;
108109
109110 foreach ( var trivia in syntaxRoot . DescendantTrivia ( ) . Where ( trivia => trivia . IsKind ( SyntaxKind . SingleLineCommentTrivia ) ) )
110111 {
111112 if ( trivia . FullSpan . Start == 0 )
112113 {
113114 // skip the trivia if it is at the start of the file
115+ previousCommentNotOnOwnLine = false ;
114116 continue ;
115117 }
116118
117119 if ( trivia . ToString ( ) . StartsWith ( "////" , StringComparison . Ordinal ) )
118120 {
119121 // ignore commented out code
122+ previousCommentNotOnOwnLine = false ;
120123 continue ;
121124 }
122125
123126 int triviaIndex ;
124-
125- // PERF: Explicitly cast to IReadOnlyList so we only box once.
126127 var triviaList = TriviaHelper . GetContainingTriviaList ( trivia , out triviaIndex ) ;
127128
128129 if ( ! IsOnOwnLine ( triviaList , triviaIndex ) )
129130 {
130131 // ignore comments after other code elements.
132+ previousCommentNotOnOwnLine = true ;
131133 continue ;
132134 }
133135
134136 if ( IsPrecededByBlankLine ( triviaList , triviaIndex ) )
135137 {
136138 // allow properly formatted blank line comments.
139+ previousCommentNotOnOwnLine = false ;
137140 continue ;
138141 }
139142
140- if ( IsPrecededBySingleLineCommentOrDocumentation ( triviaList , triviaIndex ) )
143+ if ( ! previousCommentNotOnOwnLine && IsPrecededBySingleLineCommentOrDocumentation ( triviaList , triviaIndex ) )
141144 {
142145 // allow consecutive single line comments.
146+ previousCommentNotOnOwnLine = false ;
143147 continue ;
144148 }
145149
150+ previousCommentNotOnOwnLine = false ;
151+
146152 if ( IsAtStartOfScope ( trivia ) )
147153 {
148154 // allow single line comment at scope start.
@@ -184,7 +190,8 @@ private static bool IsPrecededBySingleLineCommentOrDocumentation<T>(T triviaList
184190 triviaIndex -- ;
185191 while ( ( eolCount < 2 ) && ( triviaIndex >= 0 ) )
186192 {
187- switch ( triviaList [ triviaIndex ] . Kind ( ) )
193+ var currentTrivia = triviaList [ triviaIndex ] ;
194+ switch ( currentTrivia . Kind ( ) )
188195 {
189196 case SyntaxKind . WhitespaceTrivia :
190197 triviaIndex -- ;
0 commit comments