diff --git a/src/Workspaces/Core/Portable/Editing/ImportAdder.cs b/src/Workspaces/Core/Portable/Editing/ImportAdder.cs index b4bf07978afee55c9ed6ef06bfa88e39e22b3c51..00a066bee4ee11854f7c5c337d1883e17f40c2f1 100644 --- a/src/Workspaces/Core/Portable/Editing/ImportAdder.cs +++ b/src/Workspaces/Core/Portable/Editing/ImportAdder.cs @@ -9,6 +9,7 @@ using Microsoft.CodeAnalysis.Options; using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Text; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.Editing { @@ -52,6 +53,7 @@ public static Task AddImportsAsync(Document document, IEnumerable AddImportsFromSyntaxesAsync(Document document, bool safe = true, OptionSet? options = null, CancellationToken cancellationToken = default) { var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); + Contract.ThrowIfNull(root); return await AddImportsFromSyntaxesAsync(document, root.FullSpan, safe, options, cancellationToken).ConfigureAwait(false); } @@ -69,6 +71,7 @@ internal static Task AddImportsFromSyntaxesAsync(Document document, Te internal static async Task AddImportsFromSyntaxesAsync(Document document, SyntaxAnnotation annotation, bool safe = true, OptionSet? options = null, CancellationToken cancellationToken = default) { var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); + Contract.ThrowIfNull(root); return await AddImportsFromSyntaxesAsync(document, root.GetAnnotatedNodesAndTokens(annotation).Select(t => t.FullSpan), safe, options, cancellationToken).ConfigureAwait(false); } @@ -94,6 +97,7 @@ internal static Task AddImportsFromSyntaxesAsync(Document document, IE internal static async Task AddImportsFromSymbolAnnotationAsync(Document document, bool safe = true, OptionSet? options = null, CancellationToken cancellationToken = default) { var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); + Contract.ThrowIfNull(root); return await AddImportsFromSymbolAnnotationAsync(document, root.FullSpan, safe, options, cancellationToken).ConfigureAwait(false); } @@ -111,6 +115,7 @@ internal static Task AddImportsFromSymbolAnnotationAsync(Document docu internal static async Task AddImportsFromSymbolAnnotationAsync(Document document, SyntaxAnnotation annotation, bool safe = true, OptionSet? options = null, CancellationToken cancellationToken = default) { var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); + Contract.ThrowIfNull(root); return await AddImportsFromSymbolAnnotationAsync(document, root.GetAnnotatedNodesAndTokens(annotation).Select(t => t.FullSpan), safe, options, cancellationToken).ConfigureAwait(false); } diff --git a/src/Workspaces/Core/Portable/Editing/ImportAdderService.cs b/src/Workspaces/Core/Portable/Editing/ImportAdderService.cs index bae8f7b41a0574c7d15379dc107b84065a7f3a82..caecc0e053fd75199ed59d882180f56a67428c62 100644 --- a/src/Workspaces/Core/Portable/Editing/ImportAdderService.cs +++ b/src/Workspaces/Core/Portable/Editing/ImportAdderService.cs @@ -40,6 +40,7 @@ public enum Strategy options ??= await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false); var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); + Contract.ThrowIfNull(model); var root = await model.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false); var addImportsService = document.GetLanguageService(); var generator = SyntaxGenerator.GetGenerator(document); @@ -68,14 +69,18 @@ public enum Strategy // This will allow us to find it after we have called MakeSafeToAddNamespaces. var annotation = new SyntaxAnnotation(); document = document.WithSyntaxRoot(root.ReplaceNode(context, context.WithAdditionalAnnotations(annotation))); - root = await document.GetSyntaxRootAsync().ConfigureAwait(false); + root = (await document.GetSyntaxRootAsync().ConfigureAwait(false))!; + model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); + Contract.ThrowIfNull(model); // Make Safe to add namespaces document = document.WithSyntaxRoot( MakeSafeToAddNamespaces(root, namespaceSymbols, model, document.Project.Solution.Workspace, cancellationToken)); - root = await document.GetSyntaxRootAsync().ConfigureAwait(false); + root = (await document.GetSyntaxRootAsync().ConfigureAwait(false))!; + model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); + Contract.ThrowIfNull(model); // Find the context. It might be null if we have removed the context in the process of complexifying the tree. context = root.DescendantNodesAndSelf().FirstOrDefault(x => x.HasAnnotation(annotation)) ?? root;