提交 e65ebdcd 编写于 作者: S Sam Harwell

Enable nullable reference types for symbol extensions

上级 55b2e2fb
// 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 Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
......
// 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.Linq;
using Microsoft.CodeAnalysis.CSharp.Syntax;
......
// 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.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
......
// 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.Linq;
......@@ -97,7 +99,7 @@ public static bool ContainingTypesOrSelfHasUnsafeKeyword(this ITypeSymbol contai
return false;
}
public static async Task<ISymbol> FindApplicableAlias(this ITypeSymbol type, int position, SemanticModel semanticModel, CancellationToken cancellationToken)
public static async Task<ISymbol?> FindApplicableAlias(this ITypeSymbol type, int position, SemanticModel semanticModel, CancellationToken cancellationToken)
{
try
{
......@@ -109,7 +111,7 @@ public static async Task<ISymbol> FindApplicableAlias(this ITypeSymbol type, int
var root = await semanticModel.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);
var applicableUsings = GetApplicableUsings(position, root as CompilationUnitSyntax);
var applicableUsings = GetApplicableUsings(position, (CompilationUnitSyntax)root);
foreach (var applicableUsing in applicableUsings)
{
var alias = semanticModel.GetOriginalSemanticModel().GetDeclaredSymbol(applicableUsing, cancellationToken);
......
// 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 Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Symbols;
......@@ -10,7 +12,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Extensions
{
internal static class SymbolDisplayPartExtensions
{
public static SymbolDisplayPart MassageErrorTypeNames(this SymbolDisplayPart part, string replacement = null)
public static SymbolDisplayPart MassageErrorTypeNames(this SymbolDisplayPart part, string? replacement = null)
{
if (part.Kind == SymbolDisplayPartKind.ErrorTypeName)
{
......
// 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;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
......@@ -367,7 +370,7 @@ public static PredefinedOperator GetPredefinedOperator(this IMethodSymbol symbol
/// the first parameter is of <see cref="object"/> type and the second
/// parameter inherits from or equals <see cref="EventArgs"/> type.
/// </summary>
public static bool HasEventHandlerSignature(this IMethodSymbol method, INamedTypeSymbol eventArgsType)
public static bool HasEventHandlerSignature(this IMethodSymbol method, [NotNullWhen(returnValue: true)] INamedTypeSymbol? eventArgsType)
=> eventArgsType != null &&
method.Parameters.Length == 2 &&
method.Parameters[0].Type.SpecialType == SpecialType.System_Object &&
......
// 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;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
......@@ -13,7 +16,7 @@ namespace Microsoft.CodeAnalysis.Shared.Extensions
{
internal static partial class INamedTypeSymbolExtensions
{
public static IEnumerable<INamedTypeSymbol> GetBaseTypesAndThis(this INamedTypeSymbol namedType)
public static IEnumerable<INamedTypeSymbol> GetBaseTypesAndThis(this INamedTypeSymbol? namedType)
{
var current = namedType;
while (current != null)
......@@ -23,19 +26,19 @@ public static IEnumerable<INamedTypeSymbol> GetBaseTypesAndThis(this INamedTypeS
}
}
public static IEnumerable<ITypeParameterSymbol> GetAllTypeParameters(this INamedTypeSymbol symbol)
public static IEnumerable<ITypeParameterSymbol> GetAllTypeParameters(this INamedTypeSymbol? symbol)
{
var stack = GetContainmentStack(symbol);
return stack.SelectMany(n => n.TypeParameters);
}
public static IEnumerable<ITypeSymbol> GetAllTypeArguments(this INamedTypeSymbol symbol)
public static IEnumerable<ITypeSymbol> GetAllTypeArguments(this INamedTypeSymbol? symbol)
{
var stack = GetContainmentStack(symbol);
return stack.SelectMany(n => n.TypeArguments);
}
private static Stack<INamedTypeSymbol> GetContainmentStack(INamedTypeSymbol symbol)
private static Stack<INamedTypeSymbol> GetContainmentStack(INamedTypeSymbol? symbol)
{
var stack = new Stack<INamedTypeSymbol>();
for (var current = symbol; current != null; current = current.ContainingType)
......@@ -46,7 +49,7 @@ private static Stack<INamedTypeSymbol> GetContainmentStack(INamedTypeSymbol symb
return stack;
}
public static bool IsContainedWithin(this INamedTypeSymbol symbol, INamedTypeSymbol outer)
public static bool IsContainedWithin([NotNullWhen(returnValue: true)] this INamedTypeSymbol? symbol, INamedTypeSymbol outer)
{
// TODO(cyrusn): Should we be using OriginalSymbol here?
for (var current = symbol; current != null; current = current.ContainingType)
......@@ -60,7 +63,7 @@ public static bool IsContainedWithin(this INamedTypeSymbol symbol, INamedTypeSym
return false;
}
public static ISymbol FindImplementationForAbstractMember(this INamedTypeSymbol type, ISymbol symbol)
public static ISymbol? FindImplementationForAbstractMember(this INamedTypeSymbol? type, ISymbol symbol)
{
if (symbol.IsAbstract)
{
......@@ -71,7 +74,7 @@ public static ISymbol FindImplementationForAbstractMember(this INamedTypeSymbol
return null;
}
internal static ISymbol GetOverriddenMember(this ISymbol symbol)
internal static ISymbol? GetOverriddenMember(this ISymbol? symbol)
{
switch (symbol)
{
......@@ -129,7 +132,7 @@ private static bool IsInterfacePropertyImplemented(INamedTypeSymbol classOrStruc
// local functions
static bool IsAccessorImplemented(IMethodSymbol accessor, INamedTypeSymbol classOrStructType)
static bool IsAccessorImplemented(IMethodSymbol? accessor, INamedTypeSymbol classOrStructType)
{
return accessor == null || !IsImplementable(accessor) || classOrStructType.FindImplementationForInterfaceMember(accessor) != null;
}
......@@ -305,7 +308,7 @@ static bool IsPropertyWithInaccessibleImplementableAccessor(ISymbol member, ISym
return IsInaccessibleImplementableAccessor(property.GetMethod, within) || IsInaccessibleImplementableAccessor(property.SetMethod, within);
}
static bool IsInaccessibleImplementableAccessor(IMethodSymbol accessor, ISymbol within)
static bool IsInaccessibleImplementableAccessor(IMethodSymbol? accessor, ISymbol within)
{
return accessor != null && IsImplementable(accessor) && !accessor.IsAccessibleWithin(within);
}
......@@ -434,7 +437,7 @@ static bool IsInaccessibleImplementableAccessor(IMethodSymbol accessor, ISymbol
}
}
private static ISymbol IsAttributeNamedParameter(
private static ISymbol? IsAttributeNamedParameter(
ISymbol symbol,
ISymbol within)
{
......
// 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.Linq;
......@@ -29,7 +31,7 @@ public static string GetShortName(this INamespaceOrTypeSymbol symbol)
return symbol.ToDisplayString(s_shortNameFormat);
}
public static IEnumerable<IPropertySymbol> GetIndexers(this INamespaceOrTypeSymbol symbol)
public static IEnumerable<IPropertySymbol> GetIndexers(this INamespaceOrTypeSymbol? symbol)
{
return symbol == null
? SpecializedCollections.EmptyEnumerable<IPropertySymbol>()
......@@ -73,7 +75,7 @@ public static IReadOnlyList<string> GetNameParts(this INamespaceOrTypeSymbol sym
return names1.Count - names2.Count;
}
private static void GetNameParts(INamespaceOrTypeSymbol namespaceOrTypeSymbol, List<string> result)
private static void GetNameParts(INamespaceOrTypeSymbol? namespaceOrTypeSymbol, List<string> result)
{
if (namespaceOrTypeSymbol == null || (namespaceOrTypeSymbol.IsNamespace && ((INamespaceSymbol)namespaceOrTypeSymbol).IsGlobalNamespace))
{
......
// 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.Collections.Generic;
using System.Linq;
using Roslyn.Utilities;
......@@ -8,14 +10,14 @@ namespace Microsoft.CodeAnalysis.Shared.Extensions
{
internal partial class INamespaceSymbolExtensions
{
private class Comparer : IEqualityComparer<INamespaceSymbol>
private class Comparer : IEqualityComparer<INamespaceSymbol?>
{
public bool Equals(INamespaceSymbol x, INamespaceSymbol y)
public bool Equals(INamespaceSymbol? x, INamespaceSymbol? y)
{
return GetNameParts(x).SequenceEqual(GetNameParts(y));
}
public int GetHashCode(INamespaceSymbol obj)
public int GetHashCode(INamespaceSymbol? obj)
{
return GetNameParts(obj).Aggregate(0, (a, v) => Hash.Combine(v, a));
}
......
// 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;
......@@ -19,14 +21,14 @@ internal static partial class INamespaceSymbolExtensions
public static readonly Comparison<INamespaceSymbol> CompareNamespaces = CompareTo;
public static readonly IEqualityComparer<INamespaceSymbol> EqualityComparer = new Comparer();
private static List<string> GetNameParts(INamespaceSymbol namespaceSymbol)
private static List<string> GetNameParts(INamespaceSymbol? namespaceSymbol)
{
var result = new List<string>();
GetNameParts(namespaceSymbol, result);
return result;
}
private static void GetNameParts(INamespaceSymbol namespaceSymbol, List<string> result)
private static void GetNameParts(INamespaceSymbol? namespaceSymbol, List<string> result)
{
if (namespaceSymbol == null || namespaceSymbol.IsGlobalNamespace)
{
......@@ -143,11 +145,11 @@ public static int CompareTo(this INamespaceSymbol n1, INamespaceSymbol n2)
}
}
public static INamespaceSymbol GetQualifiedNamespace(
public static INamespaceSymbol? GetQualifiedNamespace(
this INamespaceSymbol globalNamespace,
string namespaceName)
{
var namespaceSymbol = globalNamespace;
INamespaceSymbol? namespaceSymbol = globalNamespace;
foreach (var name in namespaceName.Split('.'))
{
var members = namespaceSymbol.GetMembers(name);
......@@ -155,7 +157,7 @@ public static int CompareTo(this INamespaceSymbol n1, INamespaceSymbol n2)
? members.First() as INamespaceSymbol
: null;
if ((object)namespaceSymbol == null)
if (namespaceSymbol is null)
{
break;
}
......
// 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.Linq;
......@@ -36,7 +38,7 @@ public static IPropertySymbol RenameParameters(this IPropertySymbol property, IL
}
public static IPropertySymbol RemoveAttributeFromParameters(
this IPropertySymbol property, INamedTypeSymbol[] attributesToRemove)
this IPropertySymbol property, INamedTypeSymbol?[]? attributesToRemove)
{
if (attributesToRemove == null)
{
......@@ -44,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.Where(attr => attr != null).Any(attr => attr!.Equals(a.AttributeClass));
var someParameterHasAttribute = property.Parameters
.Any(p => p.GetAttributes().Any(shouldRemoveAttribute));
......@@ -75,7 +77,7 @@ public static IPropertySymbol RenameParameters(this IPropertySymbol property, IL
public static bool IsWritableInConstructor(this IPropertySymbol property)
=> (property.SetMethod != null || ContainsBackingField(property));
public static IFieldSymbol GetBackingFieldIfAny(this IPropertySymbol property)
public static IFieldSymbol? GetBackingFieldIfAny(this IPropertySymbol property)
=> property.ContainingType.GetMembers()
.OfType<IFieldSymbol>()
.FirstOrDefault(f => property.Equals(f.AssociatedSymbol));
......
// 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.Linq;
using Roslyn.Utilities;
......@@ -7,12 +9,12 @@ namespace Microsoft.CodeAnalysis.Shared.Extensions
{
internal static class ITypeParameterSymbolExtensions
{
public static INamedTypeSymbol GetNamedTypeSymbolConstraint(this ITypeParameterSymbol typeParameter)
public static INamedTypeSymbol? GetNamedTypeSymbolConstraint(this ITypeParameterSymbol typeParameter)
{
return typeParameter.ConstraintTypes.Select(GetNamedTypeSymbol).WhereNotNull().FirstOrDefault();
}
private static INamedTypeSymbol GetNamedTypeSymbol(ITypeSymbol type)
private static INamedTypeSymbol? GetNamedTypeSymbol(ITypeSymbol type)
{
return type is INamedTypeSymbol
? (INamedTypeSymbol)type
......
// 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;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
......@@ -22,7 +25,7 @@ internal static partial class ITypeSymbolExtensions
private const string DefaultParameterName = "p";
private const string DefaultBuiltInParameterName = "v";
public static bool CanAddNullCheck(this ITypeSymbol type)
public static bool CanAddNullCheck([NotNullWhen(returnValue: true)] this ITypeSymbol? type)
=> type != null && (type.IsReferenceType || type.IsNullable());
public static IList<INamedTypeSymbol> GetAllInterfacesIncludingThis(this ITypeSymbol type)
......@@ -39,45 +42,46 @@ public static IList<INamedTypeSymbol> GetAllInterfacesIncludingThis(this ITypeSy
return allInterfaces;
}
public static bool IsAbstractClass(this ITypeSymbol symbol)
public static bool IsAbstractClass([NotNullWhen(returnValue: true)] this ITypeSymbol? symbol)
{
return symbol?.TypeKind == TypeKind.Class && symbol.IsAbstract;
}
public static bool IsSystemVoid(this ITypeSymbol symbol)
public static bool IsSystemVoid([NotNullWhen(returnValue: true)] this ITypeSymbol? symbol)
{
return symbol?.SpecialType == SpecialType.System_Void;
}
public static bool IsNullable(this ITypeSymbol symbol)
public static bool IsNullable([NotNullWhen(returnValue: true)] this ITypeSymbol? symbol)
=> symbol?.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T;
public static bool IsModuleType(this ITypeSymbol symbol)
public static bool IsModuleType([NotNullWhen(returnValue: true)] this ITypeSymbol? symbol)
{
return symbol?.TypeKind == TypeKind.Module;
}
public static bool IsInterfaceType(this ITypeSymbol symbol)
public static bool IsInterfaceType([NotNullWhen(returnValue: true)] this ITypeSymbol? symbol)
{
return symbol?.TypeKind == TypeKind.Interface;
}
public static bool IsDelegateType(this ITypeSymbol symbol)
public static bool IsDelegateType([NotNullWhen(returnValue: true)] this ITypeSymbol? symbol)
{
return symbol?.TypeKind == TypeKind.Delegate;
}
public static bool IsStructType(this ITypeSymbol symbol)
public static bool IsStructType([NotNullWhen(returnValue: true)] this ITypeSymbol? symbol)
{
return symbol?.TypeKind == TypeKind.Struct;
}
public static bool IsAnonymousType(this INamedTypeSymbol symbol)
public static bool IsAnonymousType([NotNullWhen(returnValue: true)] this INamedTypeSymbol? symbol)
{
return symbol?.IsAnonymousType == true;
}
public static ITypeSymbol RemoveNullableIfPresent(this ITypeSymbol symbol)
[return: NotNullIfNotNull(parameterName: "symbol")]
public static ITypeSymbol? RemoveNullableIfPresent([NotNullWhen(returnValue: true)] this ITypeSymbol? symbol)
{
if (symbol.IsNullable())
{
......@@ -216,14 +220,14 @@ public static ITypeSymbol RemoveNullableIfPresent(this ITypeSymbol symbol)
// local functions
static Task<Compilation> GetCompilationOrNullAsync(Project project, CancellationToken cancellationToken)
static Task<Compilation> GetCompilationOrNullAsync(Project? project, CancellationToken cancellationToken)
=> project?.GetCompilationAsync(cancellationToken) ?? SpecializedTasks.Default<Compilation>();
}
private static HashSet<INamedTypeSymbol> GetOriginalInterfacesAndTheirBaseInterfaces(
this ITypeSymbol type,
HashSet<INamedTypeSymbol> symbols = null)
HashSet<INamedTypeSymbol>? symbols = null)
{
symbols ??= new HashSet<INamedTypeSymbol>(SymbolEquivalenceComparer.Instance);
......@@ -236,7 +240,7 @@ static Task<Compilation> GetCompilationOrNullAsync(Project project, Cancellation
return symbols;
}
public static ISymbol FindImplementations(
public static ISymbol? FindImplementations(
this ITypeSymbol typeSymbol,
ISymbol constructedInterfaceMember,
Workspace workspace)
......@@ -265,7 +269,7 @@ where SymbolEquivalenceComparer.Instance.Equals(explicitInterfaceMethod, constru
select member;
var provider = workspace.Services.GetLanguageServices(typeSymbol.Language);
var semanticFacts = provider.GetService<ISemanticFactsService>();
var semanticFacts = provider.GetRequiredService<ISemanticFactsService>();
// Even if a language only supports explicit interface implementation, we
// can't enforce it for types from metadata. For example, a VB symbol
......@@ -279,7 +283,7 @@ where SymbolEquivalenceComparer.Instance.Equals(explicitInterfaceMethod, constru
return explicitMatches.FirstOrDefault();
}
var syntaxFacts = provider.GetService<ISyntaxFactsService>();
var syntaxFacts = provider.GetRequiredService<ISyntaxFactsService>();
var implicitMatches =
from baseType in typeSymbol.GetBaseTypesAndThis()
from member in baseType.GetMembers(constructedInterfaceMember.Name).OfType<TSymbol>()
......@@ -291,7 +295,7 @@ where SymbolEquivalenceComparer.Instance.Equals(explicitInterfaceMethod, constru
return explicitMatches.FirstOrDefault() ?? implicitMatches.FirstOrDefault();
}
public static IEnumerable<ITypeSymbol> GetBaseTypesAndThis(this ITypeSymbol type)
public static IEnumerable<ITypeSymbol> GetBaseTypesAndThis(this ITypeSymbol? type)
{
var current = type;
while (current != null)
......@@ -311,7 +315,7 @@ public static IEnumerable<INamedTypeSymbol> GetBaseTypes(this ITypeSymbol type)
}
}
public static IEnumerable<ITypeSymbol> GetContainingTypesAndThis(this ITypeSymbol type)
public static IEnumerable<ITypeSymbol> GetContainingTypesAndThis(this ITypeSymbol? type)
{
var current = type;
while (current != null)
......@@ -424,7 +428,7 @@ public static bool IsAttribute(this ITypeSymbol symbol)
return false;
}
public static bool IsFormattableString(this ITypeSymbol symbol)
public static bool IsFormattableString([NotNullWhen(returnValue: true)] this ITypeSymbol? symbol)
{
return symbol?.MetadataName == "FormattableString"
&& symbol.ContainingType == null
......@@ -432,31 +436,31 @@ public static bool IsFormattableString(this ITypeSymbol symbol)
&& symbol.ContainingNamespace.ContainingNamespace?.IsGlobalNamespace == true;
}
public static ITypeSymbol RemoveUnavailableTypeParameters(
this ITypeSymbol type,
public static ITypeSymbol? RemoveUnavailableTypeParameters(
this ITypeSymbol? type,
Compilation compilation,
IEnumerable<ITypeParameterSymbol> availableTypeParameters)
{
return type?.RemoveUnavailableTypeParameters(compilation, availableTypeParameters.Select(t => t.Name).ToSet());
}
private static ITypeSymbol RemoveUnavailableTypeParameters(
this ITypeSymbol type,
private static ITypeSymbol? RemoveUnavailableTypeParameters(
this ITypeSymbol? type,
Compilation compilation,
ISet<string> availableTypeParameterNames)
{
return type?.Accept(new UnavailableTypeParameterRemover(compilation, availableTypeParameterNames));
}
public static ITypeSymbol RemoveAnonymousTypes(
this ITypeSymbol type,
public static ITypeSymbol? RemoveAnonymousTypes(
this ITypeSymbol? type,
Compilation compilation)
{
return type?.Accept(new AnonymousTypeRemover(compilation));
}
public static ITypeSymbol ReplaceTypeParametersBasedOnTypeConstraints(
this ITypeSymbol type,
public static ITypeSymbol? ReplaceTypeParametersBasedOnTypeConstraints(
this ITypeSymbol? type,
Compilation compilation,
IEnumerable<ITypeParameterSymbol> availableTypeParameters,
Solution solution,
......@@ -465,15 +469,15 @@ public static bool IsFormattableString(this ITypeSymbol symbol)
return type?.Accept(new ReplaceTypeParameterBasedOnTypeConstraintVisitor(compilation, availableTypeParameters.Select(t => t.Name).ToSet(), solution, cancellationToken));
}
public static ITypeSymbol RemoveUnnamedErrorTypes(
this ITypeSymbol type,
public static ITypeSymbol? RemoveUnnamedErrorTypes(
this ITypeSymbol? type,
Compilation compilation)
{
return type?.Accept(new UnnamedErrorTypeRemover(compilation));
}
public static IList<ITypeParameterSymbol> GetReferencedMethodTypeParameters(
this ITypeSymbol type, IList<ITypeParameterSymbol> result = null)
this ITypeSymbol? type, IList<ITypeParameterSymbol>? result = null)
{
result ??= new List<ITypeParameterSymbol>();
type?.Accept(new CollectTypeParameterSymbolsVisitor(result, onlyMethodTypeParameters: true));
......@@ -481,15 +485,15 @@ public static bool IsFormattableString(this ITypeSymbol symbol)
}
public static IList<ITypeParameterSymbol> GetReferencedTypeParameters(
this ITypeSymbol type, IList<ITypeParameterSymbol> result = null)
this ITypeSymbol? type, IList<ITypeParameterSymbol>? result = null)
{
result ??= new List<ITypeParameterSymbol>();
type?.Accept(new CollectTypeParameterSymbolsVisitor(result, onlyMethodTypeParameters: false));
return result;
}
public static ITypeSymbol SubstituteTypes<TType1, TType2>(
this ITypeSymbol type,
public static ITypeSymbol? SubstituteTypes<TType1, TType2>(
this ITypeSymbol? type,
IDictionary<TType1, TType2> mapping,
Compilation compilation)
where TType1 : ITypeSymbol
......@@ -498,8 +502,8 @@ public static bool IsFormattableString(this ITypeSymbol symbol)
return type.SubstituteTypes(mapping, new CompilationTypeGenerator(compilation));
}
public static ITypeSymbol SubstituteTypes<TType1, TType2>(
this ITypeSymbol type,
public static ITypeSymbol? SubstituteTypes<TType1, TType2>(
this ITypeSymbol? type,
IDictionary<TType1, TType2> mapping,
ITypeGenerator typeGenerator)
where TType1 : ITypeSymbol
......@@ -535,7 +539,7 @@ public static bool IsUnexpressibleTypeParameterConstraint(this ITypeSymbol typeS
return false;
}
public static bool IsNumericType(this ITypeSymbol type)
public static bool IsNumericType([NotNullWhen(returnValue: true)] this ITypeSymbol? type)
{
if (type != null)
{
......@@ -564,7 +568,7 @@ public static Accessibility DetermineMinimalAccessibility(this ITypeSymbol typeS
return typeSymbol.Accept(MinimalAccessibilityVisitor.Instance);
}
public static bool ContainsAnonymousType(this ITypeSymbol symbol)
public static bool ContainsAnonymousType([NotNullWhen(returnValue: true)] this ITypeSymbol? symbol)
{
switch (symbol)
{
......@@ -614,7 +618,7 @@ public static string CreateParameterName(this ITypeSymbol type, bool capitalize
return capitalize ? shortName.ToPascalCase() : shortName.ToCamelCase();
}
private static string GetParameterName(ITypeSymbol type)
private static string GetParameterName(ITypeSymbol? type)
{
if (type == null || type.IsAnonymousType() || type.IsTupleType)
{
......@@ -632,7 +636,7 @@ private static string GetParameterName(ITypeSymbol type)
: shortName;
}
public static bool IsSpecialType(this ITypeSymbol symbol)
public static bool IsSpecialType([NotNullWhen(returnValue: true)] this ITypeSymbol? symbol)
{
if (symbol != null)
{
......@@ -672,7 +676,7 @@ public static bool CanSupportCollectionInitializer(this ITypeSymbol typeSymbol,
.Any(m => m.Parameters.Any());
}
public static INamedTypeSymbol GetDelegateType(this ITypeSymbol typeSymbol, Compilation compilation)
public static INamedTypeSymbol? GetDelegateType(this ITypeSymbol? typeSymbol, Compilation compilation)
{
if (typeSymbol != null)
{
......@@ -703,7 +707,7 @@ public static INamedTypeSymbol GetDelegateType(this ITypeSymbol typeSymbol, Comp
return types.SelectMany(x => x.GetMembers().OfType<T>().Where(m => m.IsAccessibleWithin(within)));
}
public static ImmutableArray<T> GetAccessibleMembersInThisAndBaseTypes<T>(this ITypeSymbol containingType, ISymbol within) where T : class, ISymbol
public static ImmutableArray<T> GetAccessibleMembersInThisAndBaseTypes<T>(this ITypeSymbol? containingType, ISymbol within) where T : class, ISymbol
{
if (containingType == null)
{
......@@ -748,7 +752,7 @@ public static INamedTypeSymbol GetDelegateType(this ITypeSymbol typeSymbol, Comp
return result;
}
private static IEnumerable<T> SelectAccessibleMembers<T>(this IEnumerable<ITypeSymbol> types, ISymbol within) where T : class, ISymbol
private static IEnumerable<T> SelectAccessibleMembers<T>(this IEnumerable<ITypeSymbol>? types, ISymbol within) where T : class, ISymbol
{
if (types == null)
{
......@@ -758,7 +762,7 @@ public static INamedTypeSymbol GetDelegateType(this ITypeSymbol typeSymbol, Comp
return types.SelectMany(x => x.GetMembers().OfType<T>().Where(m => m.IsAccessibleWithin(within)));
}
private static IEnumerable<T> SelectAccessibleMembers<T>(this IEnumerable<ITypeSymbol> types, string memberName, ISymbol within) where T : class, ISymbol
private static IEnumerable<T> SelectAccessibleMembers<T>(this IEnumerable<ITypeSymbol>? types, string memberName, ISymbol within) where T : class, ISymbol
{
if (types == null)
{
......@@ -848,7 +852,7 @@ public static INamedTypeSymbol GetDelegateType(this ITypeSymbol typeSymbol, Comp
return allTypeArgs1.AreMoreSpecificThan(allTypeArgs2);
}
public static bool IsOrDerivesFromExceptionType(this ITypeSymbol type, Compilation compilation)
public static bool IsOrDerivesFromExceptionType([NotNullWhen(returnValue: true)] this ITypeSymbol? type, Compilation compilation)
{
if (type != null)
{
......@@ -965,7 +969,7 @@ public static bool IsEnumType(this ITypeSymbol type)
return false;
}
public static bool IsDisposable(this ITypeSymbol type, ITypeSymbol iDisposableType)
public static bool IsDisposable([NotNullWhen(returnValue: true)] this ITypeSymbol? type, [NotNullWhen(returnValue: true)] ITypeSymbol? iDisposableType)
=> iDisposableType != null &&
(Equals(iDisposableType, type) ||
type?.AllInterfaces.Contains(iDisposableType) == true);
......
// 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.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.PooledObjects;
......@@ -29,7 +31,7 @@ private static ImmutableArray<ISymbol> GetAllSymbolsWorker(this SymbolInfo info)
}
}
public static ISymbol GetAnySymbol(this SymbolInfo info)
public static ISymbol? GetAnySymbol(this SymbolInfo info)
{
return info.Symbol != null
? info.Symbol
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册