@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.SpacingRules
66 using System . Collections . Immutable ;
77 using Microsoft . CodeAnalysis ;
88 using Microsoft . CodeAnalysis . CSharp ;
9+ using Microsoft . CodeAnalysis . CSharp . Syntax ;
910 using Microsoft . CodeAnalysis . Diagnostics ;
1011 using StyleCop . Analyzers . Helpers ;
1112
@@ -37,17 +38,9 @@ internal class SA1026CodeMustNotContainSpaceAfterNewKeywordInImplicitlyTypedArra
3738 private static readonly DiagnosticDescriptor Descriptor =
3839 new DiagnosticDescriptor ( DiagnosticId , Title , MessageFormat , AnalyzerCategory . SpacingRules , DiagnosticSeverity . Warning , AnalyzerConstants . EnabledByDefault , Description , HelpLink ) ;
3940
40- private static readonly ImmutableArray < DiagnosticDescriptor > SupportedDiagnosticsValue =
41- ImmutableArray . Create ( Descriptor ) ;
42-
4341 /// <inheritdoc/>
44- public override ImmutableArray < DiagnosticDescriptor > SupportedDiagnostics
45- {
46- get
47- {
48- return SupportedDiagnosticsValue ;
49- }
50- }
42+ public override ImmutableArray < DiagnosticDescriptor > SupportedDiagnostics { get ; }
43+ = ImmutableArray . Create ( Descriptor ) ;
5144
5245 /// <inheritdoc/>
5346 public override void Initialize ( AnalysisContext context )
@@ -57,28 +50,17 @@ public override void Initialize(AnalysisContext context)
5750
5851 private static void HandleCompilationStart ( CompilationStartAnalysisContext context )
5952 {
60- context . RegisterSyntaxTreeActionHonorExclusions ( HandleSyntaxTree ) ;
53+ context . RegisterSyntaxNodeActionHonorExclusions ( HandleImplicitArrayCreation , SyntaxKind . ImplicitArrayCreationExpression ) ;
6154 }
6255
63- private static void HandleSyntaxTree ( SyntaxTreeAnalysisContext context )
56+ private static void HandleImplicitArrayCreation ( SyntaxNodeAnalysisContext context )
6457 {
65- SyntaxNode root = context . Tree . GetCompilationUnitRoot ( context . CancellationToken ) ;
66- foreach ( var token in root . DescendantTokens ( ) )
67- {
68- if ( ! token . IsKind ( SyntaxKind . NewKeyword ) )
69- {
70- continue ;
71- }
58+ var arrayCreation = ( ImplicitArrayCreationExpressionSyntax ) context . Node ;
59+ var newKeywordToken = arrayCreation . NewKeyword ;
7260
73- if ( token . IsMissing || ! token . Parent . IsKind ( SyntaxKind . ImplicitArrayCreationExpression ) )
74- {
75- continue ;
76- }
77-
78- if ( token . IsFollowedByWhitespace ( ) || token . IsLastInLine ( ) )
79- {
80- context . ReportDiagnostic ( Diagnostic . Create ( Descriptor , token . GetLocation ( ) , TokenSpacingCodeFixProvider . RemoveFollowing ) ) ;
81- }
61+ if ( newKeywordToken . IsFollowedByWhitespace ( ) || newKeywordToken . IsLastInLine ( ) )
62+ {
63+ context . ReportDiagnostic ( Diagnostic . Create ( Descriptor , newKeywordToken . GetLocation ( ) , TokenSpacingCodeFixProvider . RemoveFollowing ) ) ;
8264 }
8365 }
8466 }
0 commit comments