11using System ;
22using System . Collections . Immutable ;
33using System . Linq ;
4- using CSharpGuidelinesAnalyzer . Extensions ;
54using JetBrains . Annotations ;
65using Microsoft . CodeAnalysis ;
6+ using Microsoft . CodeAnalysis . CSharp ;
7+ using Microsoft . CodeAnalysis . CSharp . Syntax ;
78using Microsoft . CodeAnalysis . Diagnostics ;
8- using Microsoft . CodeAnalysis . Operations ;
99
1010namespace CSharpGuidelinesAnalyzer . Rules . MiscellaneousDesign
1111{
@@ -36,8 +36,8 @@ public sealed class CatchSpecificExceptionAnalyzer : DiagnosticAnalyzer
3636
3737#pragma warning disable RS1008 // Avoid storing per-compilation data into the fields of a diagnostic analyzer.
3838 [ NotNull ]
39- private static readonly Action < OperationAnalysisContext , ImmutableArray < INamedTypeSymbol > > AnalyzeCatchClauseAction =
40- ( context , types ) => context . SkipInvalid ( _ => AnalyzeCatchClause ( context , types ) ) ;
39+ private static readonly Action < SyntaxNodeAnalysisContext , ImmutableArray < INamedTypeSymbol > > AnalyzeCatchClauseAction =
40+ AnalyzeCatchClause ;
4141#pragma warning restore RS1008 // Avoid storing per-compilation data into the fields of a diagnostic analyzer.
4242
4343 public override void Initialize ( [ NotNull ] AnalysisContext context )
@@ -54,8 +54,8 @@ private static void RegisterCompilationStart([NotNull] CompilationStartAnalysisC
5454
5555 if ( types . Any ( ) )
5656 {
57- startContext . RegisterOperationAction ( context => AnalyzeCatchClauseAction ( context , types ) ,
58- OperationKind . CatchClause ) ;
57+ startContext . RegisterSyntaxNodeAction ( context => AnalyzeCatchClauseAction ( context , types ) ,
58+ SyntaxKind . CatchClause ) ;
5959 }
6060 }
6161
@@ -80,24 +80,25 @@ private static void AddTypeToBuilder([CanBeNull] INamedTypeSymbol type,
8080 }
8181 }
8282
83- private static void AnalyzeCatchClause ( OperationAnalysisContext context ,
83+ private static void AnalyzeCatchClause ( SyntaxNodeAnalysisContext context ,
8484 [ ItemNotNull ] ImmutableArray < INamedTypeSymbol > exceptionTypes )
8585 {
86- var catchClause = ( ICatchClauseOperation ) context . Operation ;
86+ var catchClause = ( CatchClauseSyntax ) context . Node ;
8787
88- if ( catchClause . Filter ! = null )
88+ if ( catchClause . Filter = = null )
8989 {
90- return ;
91- }
92-
93- if ( catchClause . ExceptionType == null || exceptionTypes . Contains ( catchClause . ExceptionType ) )
94- {
95- Location location = catchClause . TryGetLocationForKeyword ( ) ;
96- if ( location != null )
90+ ISymbol exceptionType = TryGetExceptionType ( catchClause . Declaration , context . SemanticModel ) ;
91+ if ( exceptionType == null || exceptionTypes . Contains ( exceptionType ) )
9792 {
98- context . ReportDiagnostic ( Diagnostic . Create ( Rule , location ) ) ;
93+ context . ReportDiagnostic ( Diagnostic . Create ( Rule , catchClause . CatchKeyword . GetLocation ( ) ) ) ;
9994 }
10095 }
10196 }
97+
98+ [ CanBeNull ]
99+ private static ISymbol TryGetExceptionType ( [ CanBeNull ] CatchDeclarationSyntax declaration , [ NotNull ] SemanticModel model )
100+ {
101+ return declaration != null ? model . GetSymbolInfo ( declaration . Type ) . Symbol : null ;
102+ }
102103 }
103104}
0 commit comments