diff --git a/src/Features/Core/Portable/UseThrowExpression/AbstractUseThrowExpressionDiagnosticAnalyzer.cs b/src/Features/Core/Portable/UseThrowExpression/AbstractUseThrowExpressionDiagnosticAnalyzer.cs
index 9ac2c029e99a0..2a4e212954cb9 100644
--- a/src/Features/Core/Portable/UseThrowExpression/AbstractUseThrowExpressionDiagnosticAnalyzer.cs
+++ b/src/Features/Core/Portable/UseThrowExpression/AbstractUseThrowExpressionDiagnosticAnalyzer.cs
@@ -18,17 +18,17 @@ namespace Microsoft.CodeAnalysis.UseThrowExpression
/// if (a == null) {
/// throw SomeException();
/// }
- ///
+ ///
/// x = a;
///
- ///
+ ///
/// and offers to change it to
- ///
+ ///
///
/// x = a ?? throw SomeException();
///
- ///
- /// Note: this analyzer can be updated to run on VB once VB supports 'throw'
+ ///
+ /// Note: this analyzer can be updated to run on VB once VB supports 'throw'
/// expressions as well.
///
internal abstract class AbstractUseThrowExpressionDiagnosticAnalyzer :
@@ -62,7 +62,7 @@ protected override void InitializeWorker(AnalysisContext context)
s_registerOperationActionInfo.Invoke(startContext, new object[]
{
new Action(operationContext => AnalyzeOperation(operationContext, expressionTypeOpt)),
- ImmutableArray.Create(OperationKind.ExpressionStatement)
+ ImmutableArray.Create(OperationKind.ThrowExpression)
});
});
}
@@ -77,20 +77,21 @@ private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol
var cancellationToken = context.CancellationToken;
- var throwOperation = (IExpressionStatement)context.Operation;
- if (throwOperation.Expression.Kind != OperationKind.ThrowExpression)
+ var throwExpression = (IThrowExpression)context.Operation;
+ var throwStatementOperation = throwExpression.Parent;
+ if (throwStatementOperation.Kind != OperationKind.ExpressionStatement)
{
return;
}
- var throwStatement = throwOperation.Syntax;
+ var throwStatement = throwExpression.Syntax;
var options = context.Options;
var optionSet = options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}
-
+
var option = optionSet.GetOption(CodeStyleOptions.PreferThrowExpression, throwStatement.Language);
if (!option.Value)
{
@@ -106,7 +107,7 @@ private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol
}
var ifOperation = GetContainingIfOperation(
- semanticModel, throwOperation, cancellationToken);
+ semanticModel, (IExpressionStatement)throwStatementOperation, cancellationToken);
// This throw statement isn't parented by an if-statement. Nothing to
// do here.
@@ -164,7 +165,7 @@ private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol
var allLocations = ImmutableArray.Create(
ifOperation.Syntax.GetLocation(),
- ((IThrowExpression)throwOperation.Expression).Expression.Syntax.GetLocation(),
+ throwExpression.Expression.Syntax.GetLocation(),
assignmentExpression.Value.Syntax.GetLocation());
var descriptor = GetDescriptorWithSeverity(option.Notification.Value);