diff --git a/src/EditorFeatures/CSharpTest/Completion/CompletionProviders/OverrideCompletionProviderTests.cs b/src/EditorFeatures/CSharpTest/Completion/CompletionProviders/OverrideCompletionProviderTests.cs index 1d98b29d14097..e2dfbc22ef3c7 100644 --- a/src/EditorFeatures/CSharpTest/Completion/CompletionProviders/OverrideCompletionProviderTests.cs +++ b/src/EditorFeatures/CSharpTest/Completion/CompletionProviders/OverrideCompletionProviderTests.cs @@ -3508,6 +3508,27 @@ class D : C """, "M()"); } + [WpfFact, WorkItem("https://github.com/dotnet/vscode-csharp/issues/6308")] + public async Task NoOverrideItemsWhenNotInTypeDeclaration() + { + await VerifyNoItemsExistAsync(""" + namespace NS + { + override $$ + } + """); + } + + [WpfFact, WorkItem("https://github.com/dotnet/vscode-csharp/issues/6308")] + public async Task NoOverrideItemsAtTopLevel() + { + await VerifyNoItemsExistAsync(""" + System.Console.WriteLine(); + + override $$ + """); + } + private Task VerifyItemExistsAsync(string markup, string expectedItem) { return VerifyItemExistsAsync(markup, expectedItem, isComplexTextEdit: true); diff --git a/src/Features/Core/Portable/Completion/Providers/AbstractOverrideCompletionProvider.ItemGetter.cs b/src/Features/Core/Portable/Completion/Providers/AbstractOverrideCompletionProvider.ItemGetter.cs index 380088f0172ce..2545f398a74da 100644 --- a/src/Features/Core/Portable/Completion/Providers/AbstractOverrideCompletionProvider.ItemGetter.cs +++ b/src/Features/Core/Portable/Completion/Providers/AbstractOverrideCompletionProvider.ItemGetter.cs @@ -8,7 +8,6 @@ using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Text; -using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.Completion.Providers { @@ -111,7 +110,11 @@ private bool TryDetermineOverridableMembers( out ImmutableArray overridableMembers) { var containingType = semanticModel.GetEnclosingSymbol(startToken.SpanStart, _cancellationToken); - Contract.ThrowIfNull(containingType); + if (containingType is null) + { + overridableMembers = default; + return false; + } var result = containingType.GetOverridableMembers(_cancellationToken);