diff --git a/src/EditorFeatures/CSharpTest/InvertIf/InvertIfTests.Elseless.cs b/src/EditorFeatures/CSharpTest/InvertIf/InvertIfTests.Elseless.cs index c0e1aaeec6d218ce32bd08852b6cbd92a79d6b13..106c03b2ab4533e10a0bddf65265683d42f06788 100644 --- a/src/EditorFeatures/CSharpTest/InvertIf/InvertIfTests.Elseless.cs +++ b/src/EditorFeatures/CSharpTest/InvertIf/InvertIfTests.Elseless.cs @@ -793,5 +793,49 @@ void M() } }"); } + + [Theory, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)] + [InlineData("get")] + [InlineData("set")] + [InlineData("init")] + public async Task IfWithoutElse_InPropertyAccessors(string accessor) + { + await TestInRegularAndScriptAsync( +$@"class C +{{ + private bool _b; + + public string Prop + {{ + {accessor} + {{ + [||]if (_b) + {{ + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); + }} + }} + }} +}}", +$@"class C +{{ + private bool _b; + + public string Prop + {{ + {accessor} + {{ + if (!_b) + {{ + return; + }} + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); + }} + }} +}}"); + } } } diff --git a/src/Features/CSharp/Portable/InvertIf/CSharpInvertIfCodeRefactoringProvider.cs b/src/Features/CSharp/Portable/InvertIf/CSharpInvertIfCodeRefactoringProvider.cs index 23941b82527bdf109344e344d29cd055f608be46..5bfa5c6d16756ad0121ac43a07c402728387d5c3 100644 --- a/src/Features/CSharp/Portable/InvertIf/CSharpInvertIfCodeRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/InvertIf/CSharpInvertIfCodeRefactoringProvider.cs @@ -66,27 +66,17 @@ protected override StatementSyntax GetElseBody(IfStatementSyntax ifNode) protected override bool CanControlFlowOut(SyntaxNode node) { - switch (node.Kind()) + switch (node) { - case SyntaxKind.SwitchSection: - case SyntaxKind.LocalFunctionStatement: - case SyntaxKind.SetAccessorDeclaration: - case SyntaxKind.GetAccessorDeclaration: - case SyntaxKind.AddAccessorDeclaration: - case SyntaxKind.RemoveAccessorDeclaration: - case SyntaxKind.MethodDeclaration: - case SyntaxKind.ConstructorDeclaration: - case SyntaxKind.DestructorDeclaration: - case SyntaxKind.OperatorDeclaration: - case SyntaxKind.ConversionOperatorDeclaration: - case SyntaxKind.AnonymousMethodExpression: - case SyntaxKind.SimpleLambdaExpression: - case SyntaxKind.ParenthesizedLambdaExpression: - case SyntaxKind.DoStatement: - case SyntaxKind.WhileStatement: - case SyntaxKind.ForStatement: - case SyntaxKind.ForEachStatement: - case SyntaxKind.ForEachVariableStatement: + case SwitchSectionSyntax: + case LocalFunctionStatementSyntax: + case AccessorDeclarationSyntax: + case MemberDeclarationSyntax: + case AnonymousFunctionExpressionSyntax: + case CommonForEachStatementSyntax: + case DoStatementSyntax: + case WhileStatementSyntax: + case ForStatementSyntax: return false; } @@ -108,31 +98,21 @@ protected override SyntaxList GetStatements(SyntaxNode node) protected override int GetJumpStatementRawKind(SyntaxNode node) { - switch (node.Kind()) + switch (node) { - case SyntaxKind.SwitchSection: + case SwitchSectionSyntax: return (int)SyntaxKind.BreakStatement; - case SyntaxKind.LocalFunctionStatement: - case SyntaxKind.SetAccessorDeclaration: - case SyntaxKind.GetAccessorDeclaration: - case SyntaxKind.AddAccessorDeclaration: - case SyntaxKind.RemoveAccessorDeclaration: - case SyntaxKind.MethodDeclaration: - case SyntaxKind.ConstructorDeclaration: - case SyntaxKind.DestructorDeclaration: - case SyntaxKind.OperatorDeclaration: - case SyntaxKind.ConversionOperatorDeclaration: - case SyntaxKind.AnonymousMethodExpression: - case SyntaxKind.SimpleLambdaExpression: - case SyntaxKind.ParenthesizedLambdaExpression: + case LocalFunctionStatementSyntax: + case AccessorDeclarationSyntax: + case MemberDeclarationSyntax: + case AnonymousFunctionExpressionSyntax: return (int)SyntaxKind.ReturnStatement; - case SyntaxKind.DoStatement: - case SyntaxKind.WhileStatement: - case SyntaxKind.ForStatement: - case SyntaxKind.ForEachStatement: - case SyntaxKind.ForEachVariableStatement: + case CommonForEachStatementSyntax: + case DoStatementSyntax: + case WhileStatementSyntax: + case ForStatementSyntax: return (int)SyntaxKind.ContinueStatement; }