未验证 提交 af0af1b6 编写于 作者: M Manish Vasani 提交者: GitHub

Merge pull request #30173 from mavasani/Issue29434

Fix for InvalidCastException in "Invert If" refactoring
......@@ -927,5 +927,14 @@ public async Task TestSingleLine_DoesNotSimplifyToLengthEqualsZero2()
@"string x; [||]if (x.Length > 0.0f) { GreaterThanZero(); } else { EqualsZero(); } } } ",
@"string x; if (x.Length <= 0.0f) { EqualsZero(); } else { GreaterThanZero(); } } } ");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
[WorkItem(29434, "https://github.com/dotnet/roslyn/issues/29434")]
public async Task TestIsExpression()
{
await TestInRegularAndScriptAsync(
@"class C { void M(object o) { [||]if (o is C) { a(); } else { } } }",
@"class C { void M(object o) { if (!(o is C)) { } else { a(); } } }");
}
}
}
......@@ -69,14 +69,13 @@ internal static partial class SyntaxGeneratorExtensions
var syntaxFacts = generator.SyntaxFacts;
syntaxFacts.GetPartsOfBinaryExpression(expressionNode, out var leftOperand, out var operatorToken, out var rightOperand);
var operation = semanticModel.GetOperation(expressionNode, cancellationToken);
if (operation.Kind == OperationKind.IsPattern)
var binaryOperation = semanticModel.GetOperation(expressionNode, cancellationToken) as IBinaryOperation;
if (binaryOperation == null)
{
// Apply the logical not operator if it is not a binary operation.
return generator.LogicalNotExpression(expressionNode);
}
var binaryOperation = (IBinaryOperation)operation;
if (!s_negatedBinaryMap.TryGetValue(binaryOperation.OperatorKind, out var negatedKind))
{
return generator.LogicalNotExpression(expressionNode);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册