From a8c08249c0c1ffb282ef2b8d1afb156b55ab8973 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 14 Aug 2019 17:49:12 -0700 Subject: [PATCH] Fix issues with nullable annotations found during review --- .../Portable/AddImports/AbstractAddImportsService.cs | 10 ++++++---- .../Core/Portable/AddImports/IAddImportsService.cs | 6 +++--- .../Shared/Extensions/IPropertySymbolExtensions.cs | 2 +- .../Shared/Extensions/ITypeSymbolExtensions.cs | 11 +++++++++-- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Workspaces/Core/Portable/AddImports/AbstractAddImportsService.cs b/src/Workspaces/Core/Portable/AddImports/AbstractAddImportsService.cs index dded9e5bfa1..7dcd321ceb0 100644 --- a/src/Workspaces/Core/Portable/AddImports/AbstractAddImportsService.cs +++ b/src/Workspaces/Core/Portable/AddImports/AbstractAddImportsService.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + using System; using System.Collections.Generic; using System.Collections.Immutable; @@ -35,14 +37,14 @@ protected AbstractAddImportsService() public bool HasExistingImport( Compilation compilation, SyntaxNode root, - SyntaxNode contextLocation, SyntaxNode import) + SyntaxNode? contextLocation, SyntaxNode import) { var globalImports = GetGlobalImports(compilation); var containers = GetAllContainers(root, contextLocation); return HasExistingImport(import, containers, globalImports); } - private static ImmutableArray GetAllContainers(SyntaxNode root, SyntaxNode contextLocation) + private static ImmutableArray GetAllContainers(SyntaxNode root, SyntaxNode? contextLocation) { contextLocation ??= root; @@ -77,7 +79,7 @@ private static ImmutableArray GetAllContainers(SyntaxNode root, Synt return false; } - public SyntaxNode GetImportContainer(SyntaxNode root, SyntaxNode contextLocation, SyntaxNode import) + public SyntaxNode GetImportContainer(SyntaxNode root, SyntaxNode? contextLocation, SyntaxNode import) { contextLocation ??= root; GetContainers(root, contextLocation, @@ -106,7 +108,7 @@ public SyntaxNode GetImportContainer(SyntaxNode root, SyntaxNode contextLocation public SyntaxNode AddImports( Compilation compilation, SyntaxNode root, - SyntaxNode contextLocation, + SyntaxNode? contextLocation, IEnumerable newImports, bool placeSystemNamespaceFirst) { diff --git a/src/Workspaces/Core/Portable/AddImports/IAddImportsService.cs b/src/Workspaces/Core/Portable/AddImports/IAddImportsService.cs index 5b40dc0837c..93bb8c99bb5 100644 --- a/src/Workspaces/Core/Portable/AddImports/IAddImportsService.cs +++ b/src/Workspaces/Core/Portable/AddImports/IAddImportsService.cs @@ -15,16 +15,16 @@ internal interface IAddImportsService : ILanguageService /// in scope at . This includes /// global imports for VB. /// - bool HasExistingImport(Compilation compilation, SyntaxNode root, SyntaxNode contextLocation, SyntaxNode import); + bool HasExistingImport(Compilation compilation, SyntaxNode root, SyntaxNode? contextLocation, SyntaxNode import); /// /// Given a context location in a provided syntax tree, returns the appropriate container /// that should be added to. /// - SyntaxNode GetImportContainer(SyntaxNode root, SyntaxNode contextLocation, SyntaxNode import); + SyntaxNode GetImportContainer(SyntaxNode root, SyntaxNode? contextLocation, SyntaxNode import); SyntaxNode AddImports( - Compilation compilation, SyntaxNode root, SyntaxNode contextLocation, + Compilation compilation, SyntaxNode root, SyntaxNode? contextLocation, IEnumerable newImports, bool placeSystemNamespaceFirst); } diff --git a/src/Workspaces/Core/Portable/Shared/Extensions/IPropertySymbolExtensions.cs b/src/Workspaces/Core/Portable/Shared/Extensions/IPropertySymbolExtensions.cs index d7e590b37e7..68f30407390 100644 --- a/src/Workspaces/Core/Portable/Shared/Extensions/IPropertySymbolExtensions.cs +++ b/src/Workspaces/Core/Portable/Shared/Extensions/IPropertySymbolExtensions.cs @@ -46,7 +46,7 @@ public static IPropertySymbol RenameParameters(this IPropertySymbol property, IL } bool shouldRemoveAttribute(AttributeData a) => - attributesToRemove.Where(attr => attr != null).Any(attr => attr!.Equals(a.AttributeClass)); + attributesToRemove.Any(attr => attr?.Equals(a.AttributeClass) ?? false); var someParameterHasAttribute = property.Parameters .Any(p => p.GetAttributes().Any(shouldRemoveAttribute)); diff --git a/src/Workspaces/Core/Portable/Shared/Extensions/ITypeSymbolExtensions.cs b/src/Workspaces/Core/Portable/Shared/Extensions/ITypeSymbolExtensions.cs index c5dff351ee6..69fcc887ad9 100644 --- a/src/Workspaces/Core/Portable/Shared/Extensions/ITypeSymbolExtensions.cs +++ b/src/Workspaces/Core/Portable/Shared/Extensions/ITypeSymbolExtensions.cs @@ -81,7 +81,7 @@ public static bool IsAnonymousType([NotNullWhen(returnValue: true)] this INamedT } [return: NotNullIfNotNull(parameterName: "symbol")] - public static ITypeSymbol? RemoveNullableIfPresent([NotNullWhen(returnValue: true)] this ITypeSymbol? symbol) + public static ITypeSymbol? RemoveNullableIfPresent(this ITypeSymbol? symbol) { if (symbol.IsNullable()) { @@ -255,7 +255,7 @@ public static bool IsAnonymousType([NotNullWhen(returnValue: true)] this INamedT return null; } - private static ISymbol FindImplementations( + private static ISymbol? FindImplementations( this ITypeSymbol typeSymbol, TSymbol constructedInterfaceMember, Workspace workspace) where TSymbol : class, ISymbol @@ -436,6 +436,7 @@ public static bool IsFormattableString([NotNullWhen(returnValue: true)] this ITy && symbol.ContainingNamespace.ContainingNamespace?.IsGlobalNamespace == true; } + [return: NotNullIfNotNull(parameterName: "type")] public static ITypeSymbol? RemoveUnavailableTypeParameters( this ITypeSymbol? type, Compilation compilation, @@ -444,6 +445,7 @@ public static bool IsFormattableString([NotNullWhen(returnValue: true)] this ITy return type?.RemoveUnavailableTypeParameters(compilation, availableTypeParameters.Select(t => t.Name).ToSet()); } + [return: NotNullIfNotNull(parameterName: "type")] private static ITypeSymbol? RemoveUnavailableTypeParameters( this ITypeSymbol? type, Compilation compilation, @@ -452,6 +454,7 @@ public static bool IsFormattableString([NotNullWhen(returnValue: true)] this ITy return type?.Accept(new UnavailableTypeParameterRemover(compilation, availableTypeParameterNames)); } + [return: NotNullIfNotNull(parameterName: "type")] public static ITypeSymbol? RemoveAnonymousTypes( this ITypeSymbol? type, Compilation compilation) @@ -459,6 +462,7 @@ public static bool IsFormattableString([NotNullWhen(returnValue: true)] this ITy return type?.Accept(new AnonymousTypeRemover(compilation)); } + [return: NotNullIfNotNull(parameterName: "type")] public static ITypeSymbol? ReplaceTypeParametersBasedOnTypeConstraints( this ITypeSymbol? type, Compilation compilation, @@ -469,6 +473,7 @@ public static bool IsFormattableString([NotNullWhen(returnValue: true)] this ITy return type?.Accept(new ReplaceTypeParameterBasedOnTypeConstraintVisitor(compilation, availableTypeParameters.Select(t => t.Name).ToSet(), solution, cancellationToken)); } + [return: NotNullIfNotNull(parameterName: "type")] public static ITypeSymbol? RemoveUnnamedErrorTypes( this ITypeSymbol? type, Compilation compilation) @@ -492,6 +497,7 @@ public static bool IsFormattableString([NotNullWhen(returnValue: true)] this ITy return result; } + [return: NotNullIfNotNull(parameterName: "type")] public static ITypeSymbol? SubstituteTypes( this ITypeSymbol? type, IDictionary mapping, @@ -502,6 +508,7 @@ public static bool IsFormattableString([NotNullWhen(returnValue: true)] this ITy return type.SubstituteTypes(mapping, new CompilationTypeGenerator(compilation)); } + [return: NotNullIfNotNull(parameterName: "type")] public static ITypeSymbol? SubstituteTypes( this ITypeSymbol? type, IDictionary mapping, -- GitLab