diff --git a/src/EditorFeatures/CSharpTest/CodeActions/InvertIf/InvertIfTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/InvertIf/InvertIfTests.cs index 37cd5661014b153b605d42280ccead6f4bcffc8b..51491f0461717c15c86feaeecd74269a8fa64da6 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/InvertIf/InvertIfTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/InvertIf/InvertIfTests.cs @@ -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(); } } }"); + } } } diff --git a/src/Workspaces/Core/Portable/Shared/Extensions/SyntaxGeneratorExtensions_Negate.cs b/src/Workspaces/Core/Portable/Shared/Extensions/SyntaxGeneratorExtensions_Negate.cs index 9da3674f8c2fd165f3f5679fd65ce531864756a6..325f5667f65c4a4061d74d34b1a6af37c73c49f1 100644 --- a/src/Workspaces/Core/Portable/Shared/Extensions/SyntaxGeneratorExtensions_Negate.cs +++ b/src/Workspaces/Core/Portable/Shared/Extensions/SyntaxGeneratorExtensions_Negate.cs @@ -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);