提交 0c730afa 编写于 作者: B Brett V. Forsgren

fix some incorrect assumptions about the structure of properties

e.g., a C# getter body doesn't necessarily have Statements, and if it
does, it may have zero or more than one
上级 3a405601
......@@ -7,6 +7,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.UseAutoProperty;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UseAutoProperty
{
......@@ -81,11 +82,16 @@ private bool CheckExpressionSyntactically(ExpressionSyntax expression)
protected override ExpressionSyntax GetGetterExpression(IMethodSymbol getMethod, CancellationToken cancellationToken)
{
var getAccessor = getMethod.DeclaringSyntaxReferences[0].GetSyntax(cancellationToken) as AccessorDeclarationSyntax;
var firstStatement = getAccessor?.Body?.Statements.SingleOrDefault();
if (firstStatement?.Kind() == SyntaxKind.ReturnStatement)
var statements = getAccessor?.Body?.Statements;
if (statements?.Count == 1)
{
var expr = ((ReturnStatementSyntax)firstStatement).Expression;
return CheckExpressionSyntactically(expr) ? expr : null;
// this only works with a getter body with exactly one statement
var firstStatement = statements.Value[0];
if (firstStatement.Kind() == SyntaxKind.ReturnStatement)
{
var expr = ((ReturnStatementSyntax)firstStatement).Expression;
return CheckExpressionSyntactically(expr) ? expr : null;
}
}
return null;
......
......@@ -46,10 +46,14 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UseAutoProperty
Protected Overrides Function GetGetterExpression(getMethod As IMethodSymbol, cancellationToken As CancellationToken) As ExpressionSyntax
Dim accessor = TryCast(TryCast(getMethod.DeclaringSyntaxReferences(0).GetSyntax(cancellationToken), AccessorStatementSyntax)?.Parent, AccessorBlockSyntax)
Dim firstStatement = accessor?.Statements.SingleOrDefault()
If firstStatement?.Kind() = SyntaxKind.ReturnStatement Then
Dim expr = DirectCast(firstStatement, ReturnStatementSyntax).Expression
Return If(CheckExpressionSyntactically(expr), expr, Nothing)
Dim statements = accessor?.Statements
If statements?.Count = 1 Then
' this only works with a getter body with exactly one statement
Dim firstStatement = statements.Value(0)
If firstStatement.Kind() = SyntaxKind.ReturnStatement Then
Dim expr = DirectCast(firstStatement, ReturnStatementSyntax).Expression
Return If(CheckExpressionSyntactically(expr), expr, Nothing)
End If
End If
Return Nothing
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册