44using System ;
55using System . Collections . Generic ;
66using System . Collections . Immutable ;
7+ using System . Diagnostics ;
78using System . Linq ;
89
910namespace FluentAssertions . Analyzers
@@ -33,7 +34,7 @@ private void AnalyzeCodeBlock(CodeBlockAnalysisContext context)
3334 {
3435 foreach ( var statement in method . Body . Statements . OfType < ExpressionStatementSyntax > ( ) )
3536 {
36- var diagnostic = AnalyzeExpression ( statement . Expression , context . SemanticModel ) ;
37+ var diagnostic = AnalyzeExpressionSafely ( statement . Expression , context . SemanticModel ) ;
3738 if ( diagnostic != null )
3839 {
3940 context . ReportDiagnostic ( diagnostic ) ;
@@ -43,7 +44,7 @@ private void AnalyzeCodeBlock(CodeBlockAnalysisContext context)
4344 }
4445 if ( method . ExpressionBody != null )
4546 {
46- var diagnostic = AnalyzeExpression ( method . ExpressionBody . Expression , context . SemanticModel ) ;
47+ var diagnostic = AnalyzeExpressionSafely ( method . ExpressionBody . Expression , context . SemanticModel ) ;
4748 if ( diagnostic != null )
4849 {
4950 context . ReportDiagnostic ( diagnostic ) ;
@@ -86,6 +87,19 @@ protected virtual Diagnostic CreateDiagnostic(TCSharpSyntaxVisitor visitor, Expr
8687 location : expression . GetLocation ( ) ,
8788 properties : properties ) ;
8889 }
90+
91+ private Diagnostic AnalyzeExpressionSafely ( ExpressionSyntax expression , SemanticModel semanticModel )
92+ {
93+ try
94+ {
95+ return AnalyzeExpression ( expression , semanticModel ) ;
96+ }
97+ catch ( Exception e )
98+ {
99+ Console . Error . WriteLine ( $ "Failed to analyze expression in { GetType ( ) . FullName } .\n { e } ") ;
100+ return null ;
101+ }
102+ }
89103 }
90104
91105 public abstract class FluentAssertionsAnalyzer : FluentAssertionsAnalyzer < FluentAssertionsCSharpSyntaxVisitor >
0 commit comments