@@ -15,7 +15,16 @@ public class RequireFieldsInitAnalyzer : DiagnosticAnalyzer
1515 {
1616 private const string RequireFieldsInitAttributeName = "RequireFieldsInitAttribute" ;
1717
18- private static readonly DiagnosticDescriptor Rule = new (
18+ private static readonly DiagnosticDescriptor UnhandledErrorRule = new (
19+ id : "RequireFieldsInit_000" ,
20+ title : "Internal error" ,
21+ messageFormat : "Internal error occured. Please open a bug report. Message: '{0}'" ,
22+ category : "Usage" ,
23+ defaultSeverity : DiagnosticSeverity . Error ,
24+ isEnabledByDefault : true
25+ ) ;
26+
27+ private static readonly DiagnosticDescriptor FieldNotInitializedRule = new (
1928 id : "RequireFieldsInit_001" ,
2029 title : "Required field not initialized" ,
2130 messageFormat : "Required field '{0}' not initialized" ,
@@ -24,7 +33,8 @@ public class RequireFieldsInitAnalyzer : DiagnosticAnalyzer
2433 isEnabledByDefault : true
2534 ) ;
2635
27- public override ImmutableArray < DiagnosticDescriptor > SupportedDiagnostics => ImmutableArray . Create ( Rule ) ;
36+ public override ImmutableArray < DiagnosticDescriptor > SupportedDiagnostics
37+ => ImmutableArray . Create ( UnhandledErrorRule , FieldNotInitializedRule ) ;
2838
2939 public override void Initialize ( AnalysisContext context )
3040 {
@@ -50,7 +60,18 @@ private void OnCompilationStart(CompilationStartAnalysisContext context)
5060 } ;
5161
5262
53- context . RegisterSyntaxNodeAction ( ctx => CheckObjectCreation ( ctx , cache ) ,
63+ context . RegisterSyntaxNodeAction ( ctx =>
64+ {
65+ try
66+ {
67+ CheckObjectCreation ( ctx , cache ) ;
68+ }
69+ catch ( Exception ex )
70+ {
71+ ctx . ReportDiagnostic (
72+ Diagnostic . Create ( UnhandledErrorRule , ctx . Node . GetLocation ( ) , ex . Message ) ) ;
73+ }
74+ } ,
5475 SyntaxKind . ObjectCreationExpression ) ;
5576 context . RegisterCompilationEndAction ( _ => cache . RequiredFieldsCache . Clear ( ) ) ;
5677 }
@@ -108,7 +129,8 @@ private void Analyze(
108129 continue ;
109130 }
110131
111- context . ReportDiagnostic ( Diagnostic . Create ( Rule , creationSyntax . Type . GetLocation ( ) , fieldName ) ) ;
132+ context . ReportDiagnostic (
133+ Diagnostic . Create ( FieldNotInitializedRule , creationSyntax . Type . GetLocation ( ) , fieldName ) ) ;
112134 }
113135 }
114136
0 commit comments