@@ -19,9 +19,10 @@ namespace StyleCop.Analyzers.SpacingRules
1919 /// <para>The following C# keywords must always be followed by a single space: <strong>catch</strong>,
2020 /// <strong>fixed</strong>, <strong>for</strong>, <strong>foreach</strong>, <strong>from</strong>,
2121 /// <strong>group</strong>, <strong>if</strong>, <strong>in</strong>, <strong>into</strong>, <strong>join</strong>,
22- /// <strong>let</strong>, <strong>lock</strong>, <strong>orderby</strong>, <strong>return</strong>,
23- /// <strong>select</strong>, <strong>stackalloc</strong>, <strong>switch</strong>, <strong>throw</strong>,
24- /// <strong>using</strong>, <strong>where</strong>, <strong>while</strong>, <strong>yield</strong>.</para>
22+ /// <strong>let</strong>, <strong>lock</strong>, <strong>orderby</strong>, <strong>out</strong>,
23+ /// <strong>ref</strong>, <strong>return</strong>, <strong>select</strong>, <strong>stackalloc</strong>,
24+ /// <strong>switch</strong>, <strong>throw</strong>, <strong>using</strong>, <strong>var</strong>,
25+ /// <strong>where</strong>, <strong>while</strong>, <strong>yield</strong>.</para>
2526 ///
2627 /// <para>The following keywords must not be followed by any space: <strong>checked</strong>,
2728 /// <strong>default</strong>, <strong>sizeof</strong>, <strong>typeof</strong>, <strong>unchecked</strong>.</para>
@@ -46,6 +47,7 @@ internal class SA1000KeywordsMustBeSpacedCorrectly : DiagnosticAnalyzer
4647
4748 private static readonly Action < SyntaxTreeAnalysisContext > SyntaxTreeAction = HandleSyntaxTree ;
4849 private static readonly Action < SyntaxNodeAnalysisContext > InvocationExpressionAction = HandleInvocationExpression ;
50+ private static readonly Action < SyntaxNodeAnalysisContext > IdentifierNameAction = HandleIdentifierName ;
4951 private static readonly ReportDiagnosticCallback < SyntaxTreeAnalysisContext > ReportSyntaxTreeDiagnostic =
5052 ( ref SyntaxTreeAnalysisContext context , Diagnostic diagnostic ) => context . ReportDiagnostic ( diagnostic ) ;
5153
@@ -69,6 +71,9 @@ public override void Initialize(AnalysisContext context)
6971
7072 // handle nameof (which appears as an invocation expression??)
7173 context . RegisterSyntaxNodeAction ( InvocationExpressionAction , SyntaxKind . InvocationExpression ) ;
74+
75+ // handle var (which appears as an identifier name??)
76+ context . RegisterSyntaxNodeAction ( IdentifierNameAction , SyntaxKind . IdentifierName ) ;
7277 }
7378
7479 private static void HandleSyntaxTree ( SyntaxTreeAnalysisContext context )
@@ -93,10 +98,13 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
9398 case SyntaxKind . LetKeyword :
9499 case SyntaxKind . LockKeyword :
95100 case SyntaxKind . OrderByKeyword :
101+ case SyntaxKind . OutKeyword :
102+ case SyntaxKind . RefKeyword :
96103 case SyntaxKind . SelectKeyword :
97104 case SyntaxKind . StackAllocKeyword :
98105 case SyntaxKind . SwitchKeyword :
99106 case SyntaxKind . UsingKeyword :
107+ case SyntaxKind . TypeVarKeyword :
100108 case SyntaxKind . WhereKeyword :
101109 case SyntaxKind . WhileKeyword :
102110 case SyntaxKind . YieldKeyword :
@@ -168,9 +176,21 @@ private static void HandleInvocationExpression(SyntaxNodeAnalysisContext context
168176 }
169177 }
170178
179+ private static void HandleIdentifierName ( SyntaxNodeAnalysisContext context )
180+ {
181+ IdentifierNameSyntax identifierNameSyntax = ( IdentifierNameSyntax ) context . Node ;
182+ if ( identifierNameSyntax . IsVar )
183+ {
184+ HandleRequiredSpaceToken ( ref context , identifierNameSyntax . Identifier ) ;
185+ }
186+ }
187+
171188 private static void HandleRequiredSpaceToken ( ref SyntaxTreeAnalysisContext context , SyntaxToken token )
172189 => HandleRequiredSpaceToken ( ReportSyntaxTreeDiagnostic , ref context , token ) ;
173190
191+ private static void HandleRequiredSpaceToken ( ref SyntaxNodeAnalysisContext context , SyntaxToken token )
192+ => HandleRequiredSpaceToken ( ReportSyntaxNodeDiagnostic , ref context , token ) ;
193+
174194 private static void HandleRequiredSpaceToken < TContext > ( ReportDiagnosticCallback < TContext > reportDiagnostic , ref TContext context , SyntaxToken token )
175195 {
176196 if ( token . IsMissing )
0 commit comments