public static void ReportLSPViolatingExceptionIfThrown(SyntaxNodeAnalysisContext context,
INamedTypeSymbol exceptionType,
DiagnosticDescriptor rule)
{
var throwStatement = context.Node as ThrowStatementSyntax;
throwStatement.Expression.DescendantNodesAndTokens()
.Where(t => t.IsKind(SyntaxKind.IdentifierName) || t.IsKind(SyntaxKind.IdentifierToken))
.TryFirst()
.Match()
.Some().Where(t => t.IsNode).Do(t =>
{
var identifier = t.AsNode() as IdentifierNameSyntax;
var identifierType = context.SemanticModel.GetSymbolInfo(identifier);
if (identifierType.Symbol.Equals(exceptionType))
{
context.ReportDiagnostic(Diagnostic.Create(rule, identifier.GetLocation()));
}
})
.Some().Do(t =>
{
var identifier = t.Parent as IdentifierNameSyntax;
var identiferType = context.SemanticModel.GetTypeInfo(identifier).Type;
if (identiferType.Equals(exceptionType))
{
context.ReportDiagnostic(Diagnostic.Create(rule, identifier.GetLocation()));
}
})
.None().Do(() => { })
.Exec();
}
}