未验证 提交 45436bd5 编写于 作者: M msftbot[bot] 提交者: GitHub

Merge pull request #49793 from CyrusNajmabadi/switchParens

Remove unnecessary parens when converting switch statement ot expression
......@@ -2,18 +2,19 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable disable
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.ConvertSwitchStatementToExpression;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
using VerifyCS = Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions.CSharpCodeFixVerifier<
Microsoft.CodeAnalysis.CSharp.ConvertSwitchStatementToExpression.ConvertSwitchStatementToExpressionDiagnosticAnalyzer,
Microsoft.CodeAnalysis.CSharp.ConvertSwitchStatementToExpression.ConvertSwitchStatementToExpressionCodeFixProvider>;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.ConvertSwitchStatementToExpression
{
using VerifyCS = CSharpCodeFixVerifier<
ConvertSwitchStatementToExpressionDiagnosticAnalyzer,
ConvertSwitchStatementToExpressionCodeFixProvider>;
public class ConvertSwitchStatementToExpressionFixAllTests
{
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertSwitchStatementToExpression)]
......@@ -258,7 +259,7 @@ class Program
public short Value => 0;
public bool ValueBoolean()
{
var value = (StatusValue()) switch
var value = StatusValue() switch
{
DayOfWeek.Monday => Value switch
{
......@@ -306,7 +307,7 @@ class C
public bool method(C c)
{
return ((string)c) switch
return (string)c switch
{
""A"" => true,
_ => false,
......
......@@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable disable
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeStyle;
......@@ -2009,5 +2007,59 @@ bool M(string s)
LanguageVersion = CSharp9,
}.RunAsync();
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertSwitchStatementToExpression)]
[WorkItem(49788, "https://github.com/dotnet/roslyn/issues/49788")]
public async Task TestParenthesizedExpression1()
{
await VerifyCS.VerifyCodeFixAsync(
@"class Program
{
int M(object i)
{
[|switch|] (i.GetType())
{
default: return 0;
}
}
}",
@"class Program
{
int M(object i)
{
return i.GetType() switch
{
_ => 0,
};
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertSwitchStatementToExpression)]
[WorkItem(49788, "https://github.com/dotnet/roslyn/issues/49788")]
public async Task TestParenthesizedExpression2()
{
await VerifyCS.VerifyCodeFixAsync(
@"class Program
{
int M()
{
[|switch|] (1 + 1)
{
default: return 0;
}
}
}",
@"class Program
{
int M()
{
return (1 + 1) switch
{
_ => 0,
};
}
}");
}
}
}
......@@ -809,6 +809,9 @@ public static OperatorPrecedence GetOperatorPrecedence(this ExpressionSyntax exp
return OperatorPrecedence.AssignmentAndLambdaExpression;
case SyntaxKind.SwitchExpression:
return OperatorPrecedence.Switch;
default:
return OperatorPrecedence.None;
}
......
......@@ -25,6 +25,7 @@ internal enum OperatorPrecedence
Shift,
Additive,
Multiplicative,
Switch,
Range,
Unary,
Primary
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册