未验证 提交 00cbaf02 编写于 作者: S Shen Chen 提交者: GitHub

Fix remove parentheses bugs (#32284)

* Add tests to catch bugs

* Change analyzer to fix two bugs

* Add test to make sure parentheses will be remove when conditional expression used as rhs, change the loop in ParenthesesExpressionExtension to break properly
上级 3bc2bdea
......@@ -2227,6 +2227,57 @@ void M(int x)
{
var v = x+$$(+x);
}
}", new TestParameters(options: RemoveAllUnnecessaryParentheses));
}
[WorkItem(31103, "https://github.com/dotnet/roslyn/issues/31103")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryParentheses)]
public async Task TestMissingForConditionalRefAsLeftHandSideValue()
{
await TestMissingAsync(
@"class Bar
{
void Foo(bool cond, double a, double b)
{
[||](cond ? ref a : ref b) = 6.67e-11;
}
}", new TestParameters(options: RemoveAllUnnecessaryParentheses));
}
[WorkItem(31103, "https://github.com/dotnet/roslyn/issues/31103")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryParentheses)]
public async Task TestConditionalExpressionAsRightHandSideValue()
{
await TestInRegularAndScript1Async(
@"class Bar
{
void Foo(bool cond, double a, double b)
{
double c = $$(cond ? a : b);
}
}",
@"class Bar
{
void Foo(bool cond, double a, double b)
{
double c = cond ? a : b;
}
}",
parameters: new TestParameters(options: RemoveAllUnnecessaryParentheses));
}
[WorkItem(32085, "https://github.com/dotnet/roslyn/issues/32085")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryParentheses)]
public async Task TestMissingForNestedConditionalExpressionInLambda()
{
await TestMissingAsync(
@"class Bar
{
void Test(bool a)
{
Func<int, string> lambda =
number => (number + $""{ ($$a ? ""foo"" : ""bar"") }"");
}
}", new TestParameters(options: RemoveAllUnnecessaryParentheses));
}
}
......
......@@ -255,6 +255,14 @@ public static bool CanRemoveParentheses(this ParenthesizedExpressionSyntax node,
}
}
// (condition ? ref a : ref b ) = SomeValue, parenthesis can't be removed for when conditional expression appears at left
// This syntax is only allowed since C# 7.2
if (expression.IsKind(SyntaxKind.ConditionalExpression) &&
node.IsLeftSideOfAnyAssignExpression())
{
return false;
}
// Operator precedence cases:
// - If the parent is not an expression, do not remove parentheses
// - Otherwise, parentheses may be removed if doing so does not change operator associations.
......@@ -270,13 +278,15 @@ private static bool RemovalMayIntroduceInterpolationAmbiguity(ParenthesizedExpre
InterpolationSyntax interpolation = null;
foreach (var ancestor in node.Parent.AncestorsAndSelf())
{
switch (ancestor.Kind())
if (ancestor.IsKind(SyntaxKind.ParenthesizedExpression))
{
case SyntaxKind.ParenthesizedExpression:
return false;
case SyntaxKind.Interpolation:
interpolation = (InterpolationSyntax)ancestor;
break;
return false;
}
if (ancestor.IsKind(SyntaxKind.Interpolation))
{
interpolation = (InterpolationSyntax)ancestor;
break;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册