Skip to content

Commit

Permalink
Made UseThrowExpression analyzer look for ThrowExpressions and use Pa…
Browse files Browse the repository at this point in the history
…rent, instead of traversing the other direction.
  • Loading branch information
333fred committed Aug 10, 2017
1 parent ac280f3 commit d18797b
Showing 1 changed file with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ namespace Microsoft.CodeAnalysis.UseThrowExpression
/// if (a == null) {
/// throw SomeException();
/// }
///
///
/// x = a;
/// </code>
///
///
/// and offers to change it to
///
///
/// <code>
/// x = a ?? throw SomeException();
/// </code>
///
/// 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.
/// </summary>
internal abstract class AbstractUseThrowExpressionDiagnosticAnalyzer :
Expand Down Expand Up @@ -62,7 +62,7 @@ protected override void InitializeWorker(AnalysisContext context)
s_registerOperationActionInfo.Invoke(startContext, new object[]
{
new Action<OperationAnalysisContext>(operationContext => AnalyzeOperation(operationContext, expressionTypeOpt)),
ImmutableArray.Create(OperationKind.ExpressionStatement)
ImmutableArray.Create(OperationKind.ThrowExpression)
});
});
}
Expand All @@ -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)
{
Expand All @@ -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.
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d18797b

Please sign in to comment.