提交 494fcc5f 编写于 作者: S Sam Harwell

Add annotations for extension methods classes

上级 a1d4335a
......@@ -11,5 +11,11 @@ internal static class RoslynString
/// <inheritdoc cref="string.IsNullOrEmpty(string)"/>
public static bool IsNullOrEmpty([NotNullWhen(returnValue: false)] string? value)
=> string.IsNullOrEmpty(value);
#if !NET20
/// <inheritdoc cref="string.IsNullOrWhiteSpace(string)"/>
public static bool IsNullOrWhiteSpace([NotNullWhen(returnValue: false)] string? value)
=> string.IsNullOrWhiteSpace(value);
#endif
}
}
......@@ -36,7 +36,7 @@ public override bool ContainingScopeHasAsyncKeyword()
return false;
}
public override SyntaxNode GetContainingScope()
public override SyntaxNode? GetContainingScope()
{
Contract.ThrowIfNull(this.SemanticDocument);
Contract.ThrowIfFalse(this.SelectionInExpression);
......
// 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.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.ExtractMethod;
using Microsoft.CodeAnalysis.Shared.Extensions;
......@@ -16,7 +18,7 @@ namespace Microsoft.CodeAnalysis.CSharp.ExtractMethod
{
internal static class Extensions
{
public static ExpressionSyntax GetUnparenthesizedExpression(this SyntaxNode node)
public static ExpressionSyntax? GetUnparenthesizedExpression(this SyntaxNode? node)
{
if (!(node is ParenthesizedExpressionSyntax parenthesizedExpression))
{
......@@ -26,19 +28,17 @@ public static ExpressionSyntax GetUnparenthesizedExpression(this SyntaxNode node
return GetUnparenthesizedExpression(parenthesizedExpression.Expression);
}
public static StatementSyntax GetStatementUnderContainer(this SyntaxNode node)
public static StatementSyntax? GetStatementUnderContainer(this SyntaxNode node)
{
Contract.ThrowIfNull(node);
while (node != null)
for (SyntaxNode? current = node; current is object; current = current.Parent)
{
if (node.Parent != null &&
node.Parent.IsStatementContainerNode())
if (current.Parent != null &&
current.Parent.IsStatementContainerNode())
{
return node as StatementSyntax;
return current as StatementSyntax;
}
node = node.Parent;
}
return null;
......@@ -49,12 +49,12 @@ public static StatementSyntax GetParentLabeledStatementIfPossible(this SyntaxNod
return (StatementSyntax)((node.Parent is LabeledStatementSyntax) ? node.Parent : node);
}
public static bool IsStatementContainerNode(this SyntaxNode node)
public static bool IsStatementContainerNode([NotNullWhen(returnValue: true)] this SyntaxNode? node)
{
return node is BlockSyntax || node is SwitchSectionSyntax;
}
public static BlockSyntax GetBlockBody(this SyntaxNode node)
public static BlockSyntax? GetBlockBody(this SyntaxNode? node)
{
switch (node)
{
......@@ -246,22 +246,22 @@ public static bool HasHybridTriviaBetween(this SyntaxToken token1, SyntaxToken t
return false;
}
public static bool IsArrayInitializer(this SyntaxNode node)
public static bool IsArrayInitializer([NotNullWhen(returnValue: true)] this SyntaxNode? node)
{
return node is InitializerExpressionSyntax && node.Parent is EqualsValueClauseSyntax;
}
public static bool IsExpressionInCast(this SyntaxNode node)
public static bool IsExpressionInCast([NotNullWhen(returnValue: true)] this SyntaxNode? node)
{
return node is ExpressionSyntax && node.Parent is CastExpressionSyntax;
}
public static bool IsExpression(this SyntaxNode node)
public static bool IsExpression([NotNullWhen(returnValue: true)] this SyntaxNode? node)
{
return node is ExpressionSyntax;
}
public static bool IsObjectType(this ITypeSymbol type)
public static bool IsObjectType(this ITypeSymbol? type)
{
return type == null || type.SpecialType == SpecialType.System_Object;
}
......
......@@ -48,7 +48,7 @@ protected override ITypeSymbol DetermineReturnTypeWorker(CancellationToken cance
var typeInference = this.Document.Document.GetLanguageService<ITypeInferenceService>();
var inferredType = typeInference.InferType(
this.Document.SemanticModel, _invocationExpression, objectAsDefault: true,
nameOpt: this.State.IdentifierToken.ValueText, cancellationToken: cancellationToken);
name: this.State.IdentifierToken.ValueText, cancellationToken: cancellationToken);
return inferredType;
}
......
......@@ -330,6 +330,10 @@ internal ChangeSignatureOptionsResult GetChangeSignatureOptions(ChangeSignatureA
var doc = originalSolution.GetDocument(docId)!;
var updater = doc.Project.LanguageServices.GetRequiredService<AbstractChangeSignatureService>();
var root = doc.GetSyntaxRootSynchronously(CancellationToken.None);
if (root is null)
{
throw new NotSupportedException(WorkspacesResources.Document_does_not_support_syntax_trees);
}
var nodes = nodesToUpdate[docId];
......
......@@ -378,7 +378,7 @@ private int GetStatementIndex(ChildSyntaxList children, SyntaxNode statement)
var typeInference = semanticDocument.Document.GetLanguageService<ITypeInferenceService>();
var inferredType = typeInference.InferType(
semanticDocument.SemanticModel, SimpleNameOrMemberAccessExpressionOpt, objectAsDefault: true,
nameOpt: IdentifierToken.ValueText, cancellationToken: cancellationToken);
name: IdentifierToken.ValueText, cancellationToken: cancellationToken);
var compilation = semanticDocument.SemanticModel.Compilation;
inferredType = inferredType.SpecialType == SpecialType.System_Void
......
......@@ -55,7 +55,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.GenerateMember.GenerateMethod
Dim typeInference = Document.Project.LanguageServices.GetService(Of ITypeInferenceService)()
Dim inferredType = typeInference.InferType(
Document.SemanticModel, Me.InvocationExpression, objectAsDefault:=True,
nameOpt:=Me.State.IdentifierToken.ValueText, cancellationToken:=cancellationToken)
name:=Me.State.IdentifierToken.ValueText, cancellationToken:=cancellationToken)
Return inferredType
End Function
......
......@@ -165,7 +165,7 @@ private void GetContainers(SyntaxNode root, SyntaxNode contextLocation, out Synt
aliasContainer = contextSpine.FirstOrDefault(HasAliases) ?? fallbackNode;
}
private static SyntaxNode GetFirstApplicableContainer(SyntaxNode contextNode)
private static SyntaxNode? GetFirstApplicableContainer(SyntaxNode contextNode)
{
var usingDirective = contextNode.GetAncestor<TUsingOrAliasSyntax>();
if (usingDirective != null)
......@@ -174,7 +174,7 @@ private static SyntaxNode GetFirstApplicableContainer(SyntaxNode contextNode)
}
return contextNode.GetAncestor<TNamespaceDeclarationSyntax>() ??
(SyntaxNode)contextNode.GetAncestorOrThis<TCompilationUnitSyntax>();
(SyntaxNode?)contextNode.GetAncestorOrThis<TCompilationUnitSyntax>();
}
}
}
......@@ -212,8 +212,7 @@ CancellationToken cancellationToken
SyntaxGenerator generator,
CancellationToken cancellationToken)
{
SyntaxNode? first = null;
SyntaxNode? last = null;
(SyntaxNode first, SyntaxNode last)? nodes = null;
var importsToAdd = ArrayBuilder<SyntaxNode>.GetInstance();
var annotatedNodes = syntaxNodes.Where(x => x.HasAnnotations(SymbolAnnotation.Kind));
......@@ -246,8 +245,7 @@ CancellationToken cancellationToken
continue;
}
first ??= annotatedNode;
last = annotatedNode;
nodes = (first: nodes?.first ?? annotatedNode, last: annotatedNode);
if (addedSymbols.Contains(namespaceSymbol))
{
......@@ -276,7 +274,7 @@ CancellationToken cancellationToken
// since whatever added the symbol annotation probably also added simplifier annotations,
// and if not they probably didn't for a reason
return (importsToAdd.ToImmutableAndFree(), addedSymbols, first?.GetCommonRoot(last));
return (importsToAdd.ToImmutableAndFree(), addedSymbols, nodes is var (first, last) ? first.GetCommonRoot(last) : null);
}
/// <summary>
......
// 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.Concurrent;
using System.Collections.Generic;
......@@ -61,7 +63,7 @@ public static void PerformAction(this IExtensionManager extensionManager, object
public static async Task PerformActionAsync(
this IExtensionManager extensionManager,
object extension,
Func<Task> function)
Func<Task?> function)
{
try
{
......@@ -84,7 +86,7 @@ public static void PerformAction(this IExtensionManager extensionManager, object
public static async Task<T> PerformFunctionAsync<T>(
this IExtensionManager extensionManager,
object extension,
Func<Task<T>> function,
Func<Task<T>?> function,
T defaultValue)
{
try
......
// 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
namespace Microsoft.CodeAnalysis.Shared.Extensions
{
internal static class FileLinePositionSpanExtensions
......@@ -7,7 +9,7 @@ internal static class FileLinePositionSpanExtensions
/// <summary>
/// Get mapped file path if exist, otherwise return null.
/// </summary>
public static string GetMappedFilePathIfExist(this FileLinePositionSpan fileLinePositionSpan)
public static string? GetMappedFilePathIfExist(this FileLinePositionSpan fileLinePositionSpan)
{
return fileLinePositionSpan.HasMappedPath ? fileLinePositionSpan.Path : null;
}
......
// 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;
......@@ -87,15 +89,15 @@ internal static partial class IFindReferencesResultExtensions
public static IEnumerable<ReferencedSymbol> FilterToAliasMatches(
this IEnumerable<ReferencedSymbol> result,
IAliasSymbol aliasSymbolOpt)
IAliasSymbol? aliasSymbol)
{
if (aliasSymbolOpt == null)
if (aliasSymbol == null)
{
return result;
}
return from r in result
let aliasLocations = r.Locations.Where(loc => SymbolEquivalenceComparer.Instance.Equals(loc.Alias, aliasSymbolOpt))
let aliasLocations = r.Locations.Where(loc => SymbolEquivalenceComparer.Instance.Equals(loc.Alias, aliasSymbol))
where aliasLocations.Any()
select new ReferencedSymbol(r.DefinitionAndProjectId, aliasLocations);
}
......@@ -117,7 +119,7 @@ where aliasLocations.Any()
{
foreach (var reference in result)
{
var isCaseSensitive = solution.Workspace.Services.GetLanguageServices(reference.Definition.Language).GetService<ISyntaxFactsService>().IsCaseSensitive;
var isCaseSensitive = solution.Workspace.Services.GetLanguageServices(reference.Definition.Language).GetRequiredService<ISyntaxFactsService>().IsCaseSensitive;
var comparer = isCaseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase;
if (reference.Definition.IsOrdinaryMethod() &&
!comparer.Equals(reference.Definition.Name, symbol.Name))
......
// 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.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CodeAnalysis.Host.Mef;
using Roslyn.Utilities;
......@@ -10,6 +13,7 @@ namespace Microsoft.CodeAnalysis.Shared.Extensions
{
internal static class ILanguageMetadataExtensions
{
[return: MaybeNull]
public static TInterface ToSpecificLanguage<TInterface, TMetadata>(this IEnumerable<Lazy<TInterface, TMetadata>> services, string languageName)
where TMetadata : ILanguageMetadata
{
......
// 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;
......@@ -13,7 +15,7 @@ internal static class ILanguageServiceProviderExtensions
{
public static IEnumerable<Lazy<T, TMetadata>> SelectMatchingExtensions<T, TMetadata>(
this HostLanguageServices serviceProvider,
IEnumerable<Lazy<T, TMetadata>> items)
IEnumerable<Lazy<T, TMetadata>>? items)
where TMetadata : ILanguageMetadata
{
if (items == null)
......
// 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.Threading;
......@@ -22,18 +25,20 @@ internal static partial class ISolutionExtensions
foreach (var projectId in solution.ProjectIds)
{
var project = solution.GetProject(projectId);
var project = solution.GetProject(projectId)!;
if (project.SupportsCompilation)
{
var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
#nullable disable // Can 'compilation' be null here?
results.Add(compilation.Assembly.GlobalNamespace);
#nullable enable
}
}
return results.ToImmutableAndFree();
}
public static IEnumerable<DocumentId> GetChangedDocuments(this Solution newSolution, Solution oldSolution)
public static IEnumerable<DocumentId> GetChangedDocuments(this Solution? newSolution, Solution oldSolution)
{
if (newSolution != null)
{
......@@ -49,14 +54,14 @@ public static IEnumerable<DocumentId> GetChangedDocuments(this Solution newSolut
}
}
public static TextDocument GetTextDocument(this Solution solution, DocumentId documentId)
public static TextDocument? GetTextDocument(this Solution solution, DocumentId? documentId)
{
return solution.GetDocument(documentId) ?? solution.GetAdditionalDocument(documentId) ?? solution.GetAnalyzerConfigDocument(documentId);
}
public static TextDocumentKind GetDocumentKind(this Solution solution, DocumentId documentId)
public static TextDocumentKind? GetDocumentKind(this Solution solution, DocumentId documentId)
{
return solution.GetTextDocument(documentId).Kind;
return solution.GetTextDocument(documentId)?.Kind;
}
public static Solution WithTextDocumentText(this Solution solution, DocumentId documentId, SourceText text, PreservationMode mode = PreservationMode.PreserveIdentity)
......@@ -73,6 +78,9 @@ public static Solution WithTextDocumentText(this Solution solution, DocumentId d
case TextDocumentKind.AdditionalDocument:
return solution.WithAdditionalDocumentText(documentId, text, mode);
case null:
throw new InvalidOperationException(WorkspacesResources.The_solution_does_not_contain_the_specified_document);
default:
throw ExceptionUtilities.UnexpectedValue(documentKind);
}
......
......@@ -511,7 +511,7 @@ public static bool IsUnsafe([NotNullWhen(returnValue: true)] this ISymbol? membe
}
public static ITypeSymbol ConvertToType(
this ISymbol symbol,
this ISymbol? symbol,
Compilation compilation,
bool extensionUsedAsInstance = false)
{
......
// 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 System.Threading;
......@@ -22,7 +24,7 @@ public static ImmutableArray<TypeInferenceInfo> GetTypeInferenceInfo(this ITypeI
public static ImmutableArray<TypeInferenceInfo> GetTypeInferenceInfo(this ITypeInferenceService service, SemanticModel semanticModel, SyntaxNode expression, CancellationToken cancellationToken)
=> service.GetTypeInferenceInfo(semanticModel, expression, nameOpt: null, cancellationToken: cancellationToken);
public static INamedTypeSymbol InferDelegateType(
public static INamedTypeSymbol? InferDelegateType(
this ITypeInferenceService typeInferenceService,
SemanticModel semanticModel,
SyntaxNode expression,
......@@ -32,7 +34,7 @@ public static ImmutableArray<TypeInferenceInfo> GetTypeInferenceInfo(this ITypeI
return GetFirstDelegateType(semanticModel, types);
}
public static INamedTypeSymbol InferDelegateType(
public static INamedTypeSymbol? InferDelegateType(
this ITypeInferenceService typeInferenceService,
SemanticModel semanticModel,
int position,
......@@ -42,13 +44,13 @@ public static ImmutableArray<TypeInferenceInfo> GetTypeInferenceInfo(this ITypeI
return GetFirstDelegateType(semanticModel, types);
}
private static INamedTypeSymbol GetFirstDelegateType(SemanticModel semanticModel, ImmutableArray<ITypeSymbol> types)
private static INamedTypeSymbol? GetFirstDelegateType(SemanticModel semanticModel, ImmutableArray<ITypeSymbol> types)
{
var delegateTypes = types.Select(t => t.GetDelegateType(semanticModel.Compilation));
return delegateTypes.WhereNotNull().FirstOrDefault();
}
public static ITypeSymbol InferType(
public static ITypeSymbol? InferType(
this ITypeInferenceService typeInferenceService,
SemanticModel semanticModel,
SyntaxNode expression,
......@@ -57,28 +59,28 @@ private static INamedTypeSymbol GetFirstDelegateType(SemanticModel semanticModel
{
return InferType(
typeInferenceService, semanticModel, expression, objectAsDefault,
nameOpt: null, cancellationToken: cancellationToken);
name: null, cancellationToken: cancellationToken);
}
public static ITypeSymbol InferType(
public static ITypeSymbol? InferType(
this ITypeInferenceService typeInferenceService,
SemanticModel semanticModel,
SyntaxNode expression,
bool objectAsDefault,
string nameOpt,
string? name,
CancellationToken cancellationToken)
{
var types = typeInferenceService.InferTypes(semanticModel, expression, nameOpt, cancellationToken);
var types = typeInferenceService.InferTypes(semanticModel, expression, name, cancellationToken);
if (types.Length == 0)
{
return objectAsDefault ? semanticModel.Compilation.ObjectType : null;
}
return types.FirstOrDefault();
return types.First();
}
public static ITypeSymbol InferType(
public static ITypeSymbol? InferType(
this ITypeInferenceService typeInferenceService,
SemanticModel semanticModel,
int position,
......@@ -87,18 +89,18 @@ private static INamedTypeSymbol GetFirstDelegateType(SemanticModel semanticModel
{
return InferType(
typeInferenceService, semanticModel, position, objectAsDefault,
nameOpt: null, cancellationToken: cancellationToken);
name: null, cancellationToken: cancellationToken);
}
public static ITypeSymbol InferType(
public static ITypeSymbol? InferType(
this ITypeInferenceService typeInferenceService,
SemanticModel semanticModel,
int position,
bool objectAsDefault,
string nameOpt,
string? name,
CancellationToken cancellationToken)
{
var types = typeInferenceService.InferTypes(semanticModel, position, nameOpt, cancellationToken);
var types = typeInferenceService.InferTypes(semanticModel, position, name, cancellationToken);
if (types.Length == 0)
{
......
// 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.Diagnostics.CodeAnalysis;
namespace Microsoft.CodeAnalysis.Shared.Extensions
{
internal static partial class ObjectExtensions
{
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TBaseType, TResult> defaultFunc = null)
[return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType
......@@ -29,11 +33,12 @@ internal static partial class ObjectExtensions
}
else
{
return default;
return default!;
}
}
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TBaseType, TResult> defaultFunc = null)
[return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType
......@@ -61,11 +66,12 @@ internal static partial class ObjectExtensions
}
else
{
return default;
return default!;
}
}
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TBaseType, TResult> defaultFunc = null)
[return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType
......@@ -98,11 +104,12 @@ internal static partial class ObjectExtensions
}
else
{
return default;
return default!;
}
}
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TBaseType, TResult> defaultFunc = null)
[return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType
......@@ -140,11 +147,12 @@ internal static partial class ObjectExtensions
}
else
{
return default;
return default!;
}
}
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TBaseType, TResult> defaultFunc = null)
[return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType
......@@ -232,11 +240,12 @@ internal static partial class ObjectExtensions
}
else
{
return default;
return default!;
}
}
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TDerivedType17, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TDerivedType17, TResult> matchFunc17, Func<TBaseType, TResult> defaultFunc = null)
[return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TDerivedType17, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TDerivedType17, TResult> matchFunc17, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType
......@@ -329,11 +338,12 @@ internal static partial class ObjectExtensions
}
else
{
return default;
return default!;
}
}
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TDerivedType17, TDerivedType18, TDerivedType19, TDerivedType20, TDerivedType21, TDerivedType22, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TDerivedType17, TResult> matchFunc17, Func<TDerivedType18, TResult> matchFunc18, Func<TDerivedType19, TResult> matchFunc19, Func<TDerivedType20, TResult> matchFunc20, Func<TDerivedType21, TResult> matchFunc21, Func<TDerivedType22, TResult> matchFunc22, Func<TBaseType, TResult> defaultFunc = null)
[return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TDerivedType17, TDerivedType18, TDerivedType19, TDerivedType20, TDerivedType21, TDerivedType22, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TDerivedType17, TResult> matchFunc17, Func<TDerivedType18, TResult> matchFunc18, Func<TDerivedType19, TResult> matchFunc19, Func<TDerivedType20, TResult> matchFunc20, Func<TDerivedType21, TResult> matchFunc21, Func<TDerivedType22, TResult> matchFunc22, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType
......@@ -451,11 +461,12 @@ internal static partial class ObjectExtensions
}
else
{
return default;
return default!;
}
}
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TDerivedType17, TDerivedType18, TDerivedType19, TDerivedType20, TDerivedType21, TDerivedType22, TDerivedType23, TDerivedType24, TDerivedType25, TDerivedType26, TDerivedType27, TDerivedType28, TDerivedType29, TDerivedType30, TDerivedType31, TDerivedType32, TDerivedType33, TDerivedType34, TDerivedType35, TDerivedType36, TDerivedType37, TDerivedType38, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TDerivedType17, TResult> matchFunc17, Func<TDerivedType18, TResult> matchFunc18, Func<TDerivedType19, TResult> matchFunc19, Func<TDerivedType20, TResult> matchFunc20, Func<TDerivedType21, TResult> matchFunc21, Func<TDerivedType22, TResult> matchFunc22, Func<TDerivedType23, TResult> matchFunc23, Func<TDerivedType24, TResult> matchFunc24, Func<TDerivedType25, TResult> matchFunc25, Func<TDerivedType26, TResult> matchFunc26, Func<TDerivedType27, TResult> matchFunc27, Func<TDerivedType28, TResult> matchFunc28, Func<TDerivedType29, TResult> matchFunc29, Func<TDerivedType30, TResult> matchFunc30, Func<TDerivedType31, TResult> matchFunc31, Func<TDerivedType32, TResult> matchFunc32, Func<TDerivedType33, TResult> matchFunc33, Func<TDerivedType34, TResult> matchFunc34, Func<TDerivedType35, TResult> matchFunc35, Func<TDerivedType36, TResult> matchFunc36, Func<TDerivedType37, TResult> matchFunc37, Func<TDerivedType38, TResult> matchFunc38, Func<TBaseType, TResult> defaultFunc = null)
[return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TDerivedType17, TDerivedType18, TDerivedType19, TDerivedType20, TDerivedType21, TDerivedType22, TDerivedType23, TDerivedType24, TDerivedType25, TDerivedType26, TDerivedType27, TDerivedType28, TDerivedType29, TDerivedType30, TDerivedType31, TDerivedType32, TDerivedType33, TDerivedType34, TDerivedType35, TDerivedType36, TDerivedType37, TDerivedType38, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TDerivedType17, TResult> matchFunc17, Func<TDerivedType18, TResult> matchFunc18, Func<TDerivedType19, TResult> matchFunc19, Func<TDerivedType20, TResult> matchFunc20, Func<TDerivedType21, TResult> matchFunc21, Func<TDerivedType22, TResult> matchFunc22, Func<TDerivedType23, TResult> matchFunc23, Func<TDerivedType24, TResult> matchFunc24, Func<TDerivedType25, TResult> matchFunc25, Func<TDerivedType26, TResult> matchFunc26, Func<TDerivedType27, TResult> matchFunc27, Func<TDerivedType28, TResult> matchFunc28, Func<TDerivedType29, TResult> matchFunc29, Func<TDerivedType30, TResult> matchFunc30, Func<TDerivedType31, TResult> matchFunc31, Func<TDerivedType32, TResult> matchFunc32, Func<TDerivedType33, TResult> matchFunc33, Func<TDerivedType34, TResult> matchFunc34, Func<TDerivedType35, TResult> matchFunc35, Func<TDerivedType36, TResult> matchFunc36, Func<TDerivedType37, TResult> matchFunc37, Func<TDerivedType38, TResult> matchFunc38, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType
......@@ -653,7 +664,7 @@ internal static partial class ObjectExtensions
}
else
{
return default;
return default!;
}
}
}
......
// 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
namespace Microsoft.CodeAnalysis.Shared.Extensions
{
internal static partial class ObjectExtensions
{
public static string GetTypeDisplayName(this object obj)
public static string GetTypeDisplayName(this object? obj)
{
return obj == null ? "null" : obj.GetType().Name;
}
......
// 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.Diagnostics.CodeAnalysis;
namespace Microsoft.CodeAnalysis.Extensions
{
internal static class CommonParenthesizedExpressionSyntaxExtensions
......@@ -81,14 +85,14 @@ private static bool AnySymbolIsUserDefinedOperator(SymbolInfo symbolInfo)
return false;
}
private static bool IsUserDefinedOperator(ISymbol symbol)
private static bool IsUserDefinedOperator([NotNullWhen(returnValue: true)] ISymbol? symbol)
=> symbol is IMethodSymbol methodSymbol &&
methodSymbol.MethodKind == MethodKind.UserDefinedOperator;
private static bool IsFloatingPoint(TypeInfo typeInfo)
=> IsFloatingPoint(typeInfo.Type) || IsFloatingPoint(typeInfo.ConvertedType);
private static bool IsFloatingPoint(ITypeSymbol type)
private static bool IsFloatingPoint([NotNullWhen(returnValue: true)] ITypeSymbol? type)
=> type?.SpecialType == SpecialType.System_Single || type?.SpecialType == SpecialType.System_Double;
}
}
// 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.LanguageServices;
......@@ -32,8 +35,8 @@ public static SymbolInfo GetSymbolInfo(this SemanticModel semanticModel, SyntaxT
return semanticModel.GetSymbolInfo(token.Parent, cancellationToken);
}
public static TSymbol GetEnclosingSymbol<TSymbol>(this SemanticModel semanticModel, int position, CancellationToken cancellationToken)
where TSymbol : ISymbol
public static TSymbol? GetEnclosingSymbol<TSymbol>(this SemanticModel semanticModel, int position, CancellationToken cancellationToken)
where TSymbol : class, ISymbol
{
for (var symbol = semanticModel.GetEnclosingSymbol(position, cancellationToken);
symbol != null;
......@@ -54,12 +57,12 @@ public static ISymbol GetEnclosingNamedTypeOrAssembly(this SemanticModel semanti
(ISymbol)semanticModel.Compilation.Assembly;
}
public static INamedTypeSymbol GetEnclosingNamedType(this SemanticModel semanticModel, int position, CancellationToken cancellationToken)
public static INamedTypeSymbol? GetEnclosingNamedType(this SemanticModel semanticModel, int position, CancellationToken cancellationToken)
{
return semanticModel.GetEnclosingSymbol<INamedTypeSymbol>(position, cancellationToken);
}
public static INamespaceSymbol GetEnclosingNamespace(this SemanticModel semanticModel, int position, CancellationToken cancellationToken)
public static INamespaceSymbol? GetEnclosingNamespace(this SemanticModel semanticModel, int position, CancellationToken cancellationToken)
{
return semanticModel.GetEnclosingSymbol<INamespaceSymbol>(position, cancellationToken);
}
......@@ -84,7 +87,7 @@ public static INamespaceSymbol GetEnclosingNamespace(this SemanticModel semantic
return symbolInfo.GetAnySymbol().ConvertToType(semanticModel.Compilation);
}
private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type)
private static ISymbol? MapSymbol(ISymbol symbol, ITypeSymbol? type)
{
if (symbol.IsConstructor() && symbol.ContainingType.IsAnonymousType)
{
......@@ -139,19 +142,19 @@ private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type)
CancellationToken cancellationToken)
{
var languageServices = workspace.Services.GetLanguageServices(token.Language);
var syntaxFacts = languageServices.GetService<ISyntaxFactsService>();
var syntaxFacts = languageServices.GetRequiredService<ISyntaxFactsService>();
if (!syntaxFacts.IsBindableToken(token))
{
return TokenSemanticInfo.Empty;
}
var semanticFacts = languageServices.GetService<ISemanticFactsService>();
var semanticFacts = languageServices.GetRequiredService<ISemanticFactsService>();
IAliasSymbol aliasSymbol;
ITypeSymbol type;
ITypeSymbol convertedType;
ISymbol declaredSymbol;
ImmutableArray<ISymbol> allSymbols;
IAliasSymbol? aliasSymbol;
ITypeSymbol? type;
ITypeSymbol? convertedType;
ISymbol? declaredSymbol;
ImmutableArray<ISymbol?> allSymbols;
var overriddingIdentifier = syntaxFacts.GetDeclarationIdentifierIfOverride(token);
if (overriddingIdentifier.HasValue)
......@@ -165,7 +168,7 @@ private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type)
type = null;
convertedType = null;
declaredSymbol = null;
allSymbols = overriddenSymbol is null ? ImmutableArray<ISymbol>.Empty : ImmutableArray.Create(overriddenSymbol);
allSymbols = overriddenSymbol is null ? ImmutableArray<ISymbol?>.Empty : ImmutableArray.Create<ISymbol?>(overriddenSymbol);
}
else
{
......@@ -178,7 +181,7 @@ private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type)
var skipSymbolInfoLookup = declaredSymbol.IsKind(SymbolKind.RangeVariable);
allSymbols = skipSymbolInfoLookup
? ImmutableArray<ISymbol>.Empty
? ImmutableArray<ISymbol?>.Empty
: semanticFacts
.GetBestOrAllSymbols(semanticModel, bindableParent, token, cancellationToken)
.WhereAsArray(s => !s.Equals(declaredSymbol))
......@@ -203,7 +206,7 @@ private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type)
if (namedType.TypeKind == TypeKind.Delegate ||
namedType.AssociatedSymbol != null)
{
allSymbols = ImmutableArray.Create<ISymbol>(type);
allSymbols = ImmutableArray.Create<ISymbol?>(type);
type = null;
}
}
......@@ -232,7 +235,7 @@ public static SemanticModel GetOriginalSemanticModel(this SemanticModel semantic
}
public static HashSet<ISymbol> GetAllDeclaredSymbols(
this SemanticModel semanticModel, SyntaxNode container, CancellationToken cancellationToken, Func<SyntaxNode, bool> filter = null)
this SemanticModel semanticModel, SyntaxNode? container, CancellationToken cancellationToken, Func<SyntaxNode, bool>? filter = null)
{
var symbols = new HashSet<ISymbol>();
if (container != null)
......@@ -244,7 +247,7 @@ public static SemanticModel GetOriginalSemanticModel(this SemanticModel semantic
}
public static IEnumerable<ISymbol> GetExistingSymbols(
this SemanticModel semanticModel, SyntaxNode container, CancellationToken cancellationToken, Func<SyntaxNode, bool> descendInto = null)
this SemanticModel semanticModel, SyntaxNode? container, CancellationToken cancellationToken, Func<SyntaxNode, bool>? descendInto = null)
{
// Ignore an anonymous type property or tuple field. It's ok if they have a name that
// matches the name of the local we're introducing.
......@@ -254,7 +257,7 @@ public static SemanticModel GetOriginalSemanticModel(this SemanticModel semantic
private static void GetAllDeclaredSymbols(
SemanticModel semanticModel, SyntaxNode node,
HashSet<ISymbol> symbols, CancellationToken cancellationToken, Func<SyntaxNode, bool> descendInto = null)
HashSet<ISymbol> symbols, CancellationToken cancellationToken, Func<SyntaxNode, bool>? descendInto = null)
{
var symbol = semanticModel.GetDeclaredSymbol(node, cancellationToken);
......@@ -275,7 +278,7 @@ public static SemanticModel GetOriginalSemanticModel(this SemanticModel semantic
}
}
static bool ShouldDescendInto(SyntaxNode node, Func<SyntaxNode, bool> filter)
static bool ShouldDescendInto(SyntaxNode node, Func<SyntaxNode, bool>? filter)
=> filter != null ? filter(node) : 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;
using System.Collections.Immutable;
using System.Diagnostics;
......@@ -69,7 +71,7 @@ public static int ConvertTabToSpace(this string textSnippet, int tabSize, int in
return column - initialColumn;
}
public static int IndexOf(this string text, Func<char, bool> predicate)
public static int IndexOf(this string? text, Func<char, bool> predicate)
{
if (text == null)
{
......@@ -215,9 +217,9 @@ public static int GetLineOffsetFromColumn(this string line, int column, int tabS
return line.Length;
}
public static void AppendToAliasNameSet(this string alias, ImmutableHashSet<string>.Builder builder)
public static void AppendToAliasNameSet(this string? alias, ImmutableHashSet<string>.Builder builder)
{
if (string.IsNullOrWhiteSpace(alias))
if (RoslynString.IsNullOrWhiteSpace(alias))
{
return;
}
......
// 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.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
......@@ -19,7 +22,7 @@ public static IEnumerable<SyntaxNodeOrToken> DepthFirstTraversal(this SyntaxNode
public static IEnumerable<SyntaxNode> GetAncestors(this SyntaxNode node)
{
var current = node.Parent;
SyntaxNode? current = node.Parent;
while (current != null)
{
......@@ -32,7 +35,7 @@ public static IEnumerable<SyntaxNode> GetAncestors(this SyntaxNode node)
public static IEnumerable<TNode> GetAncestors<TNode>(this SyntaxNode node)
where TNode : SyntaxNode
{
var current = node.Parent;
SyntaxNode? current = node.Parent;
while (current != null)
{
if (current is TNode tNode)
......@@ -44,10 +47,10 @@ public static IEnumerable<TNode> GetAncestors<TNode>(this SyntaxNode node)
}
}
public static TNode GetAncestor<TNode>(this SyntaxNode node)
public static TNode? GetAncestor<TNode>(this SyntaxNode node)
where TNode : SyntaxNode
{
var current = node.Parent;
SyntaxNode? current = node.Parent;
while (current != null)
{
if (current is TNode tNode)
......@@ -61,13 +64,13 @@ public static TNode GetAncestor<TNode>(this SyntaxNode node)
return null;
}
public static TNode GetAncestorOrThis<TNode>(this SyntaxNode node)
public static TNode? GetAncestorOrThis<TNode>(this SyntaxNode? node)
where TNode : SyntaxNode
{
return node?.GetAncestorsOrThis<TNode>().FirstOrDefault();
}
public static IEnumerable<TNode> GetAncestorsOrThis<TNode>(this SyntaxNode node)
public static IEnumerable<TNode> GetAncestorsOrThis<TNode>(this SyntaxNode? node)
where TNode : SyntaxNode
{
var current = node;
......@@ -114,7 +117,7 @@ public static bool HasAncestor<TNode>(this SyntaxNode node)
}
}
public static bool CheckParent<T>(this SyntaxNode node, Func<T, bool> valueChecker) where T : SyntaxNode
public static bool CheckParent<T>([NotNullWhen(returnValue: true)] this SyntaxNode? node, Func<T, bool> valueChecker) where T : SyntaxNode
{
if (!(node?.Parent is T parentNode))
{
......@@ -184,11 +187,11 @@ public static int FullWidth(this SyntaxNode node)
return node.FullSpan.Length;
}
public static SyntaxNode FindInnermostCommonNode(
public static SyntaxNode? FindInnermostCommonNode(
this IEnumerable<SyntaxNode> nodes,
Func<SyntaxNode, bool> predicate)
{
IEnumerable<SyntaxNode> blocks = null;
IEnumerable<SyntaxNode>? blocks = null;
foreach (var node in nodes)
{
blocks = blocks == null
......@@ -199,10 +202,10 @@ public static int FullWidth(this SyntaxNode node)
return blocks?.First();
}
public static TSyntaxNode FindInnermostCommonNode<TSyntaxNode>(this IEnumerable<SyntaxNode> nodes)
public static TSyntaxNode? FindInnermostCommonNode<TSyntaxNode>(this IEnumerable<SyntaxNode> nodes)
where TSyntaxNode : SyntaxNode
{
return (TSyntaxNode)nodes.FindInnermostCommonNode(n => n is TSyntaxNode);
return (TSyntaxNode?)nodes.FindInnermostCommonNode(n => n is TSyntaxNode);
}
/// <summary>
......@@ -250,9 +253,9 @@ public static TextSpan GetContainedSpan(this IEnumerable<SyntaxNode> nodes)
}
public static IEnumerable<TextSpan> GetContiguousSpans(
this IEnumerable<SyntaxNode> nodes, Func<SyntaxNode, SyntaxToken> getLastToken = null)
this IEnumerable<SyntaxNode> nodes, Func<SyntaxNode, SyntaxToken>? getLastToken = null)
{
SyntaxNode lastNode = null;
SyntaxNode? lastNode = null;
TextSpan? textSpan = null;
// Sort the nodes in source location order.
......@@ -268,12 +271,12 @@ public static TextSpan GetContainedSpan(this IEnumerable<SyntaxNode> nodes)
if (lastToken.GetNextToken(includeDirectives: true) == node.GetFirstToken())
{
// Expand the span
textSpan = TextSpan.FromBounds(textSpan.Value.Start, node.Span.End);
textSpan = TextSpan.FromBounds(textSpan!.Value.Start, node.Span.End);
}
else
{
// Return the last span, and start a new one
yield return textSpan.Value;
yield return textSpan!.Value;
textSpan = node.Span;
}
}
......@@ -370,12 +373,12 @@ public static bool OverlapsHiddenPosition(this SyntaxNode declaration, SyntaxNod
public static async Task<TRoot> ReplaceSyntaxAsync<TRoot>(
this TRoot root,
IEnumerable<SyntaxNode> nodes,
Func<SyntaxNode, SyntaxNode, CancellationToken, Task<SyntaxNode>> computeReplacementNodeAsync,
IEnumerable<SyntaxToken> tokens,
Func<SyntaxToken, SyntaxToken, CancellationToken, Task<SyntaxToken>> computeReplacementTokenAsync,
IEnumerable<SyntaxTrivia> trivia,
Func<SyntaxTrivia, SyntaxTrivia, CancellationToken, Task<SyntaxTrivia>> computeReplacementTriviaAsync,
IEnumerable<SyntaxNode>? nodes,
Func<SyntaxNode, SyntaxNode, CancellationToken, Task<SyntaxNode>>? computeReplacementNodeAsync,
IEnumerable<SyntaxToken>? tokens,
Func<SyntaxToken, SyntaxToken, CancellationToken, Task<SyntaxToken>>? computeReplacementTokenAsync,
IEnumerable<SyntaxTrivia>? trivia,
Func<SyntaxTrivia, SyntaxTrivia, CancellationToken, Task<SyntaxTrivia>>? computeReplacementTriviaAsync,
CancellationToken cancellationToken)
where TRoot : SyntaxNode
{
......@@ -423,7 +426,7 @@ public static bool OverlapsHiddenPosition(this SyntaxNode declaration, SyntaxNod
if (nodesToReplace.TryGetValue(span, out var currentNode))
{
var original = (SyntaxNode)retryAnnotations.GetAnnotations(currentNode).SingleOrDefault() ?? currentNode;
var newNode = await computeReplacementNodeAsync(original, currentNode, cancellationToken).ConfigureAwait(false);
var newNode = await computeReplacementNodeAsync!(original, currentNode, cancellationToken).ConfigureAwait(false);
nodeReplacements[currentNode] = newNode;
}
else if (tokensToReplace.TryGetValue(span, out var currentToken))
......@@ -434,7 +437,7 @@ public static bool OverlapsHiddenPosition(this SyntaxNode declaration, SyntaxNod
original = currentToken;
}
var newToken = await computeReplacementTokenAsync(original, currentToken, cancellationToken).ConfigureAwait(false);
var newToken = await computeReplacementTokenAsync!(original, currentToken, cancellationToken).ConfigureAwait(false);
tokenReplacements[currentToken] = newToken;
}
else if (triviaToReplace.TryGetValue(span, out var currentTrivia))
......@@ -445,7 +448,7 @@ public static bool OverlapsHiddenPosition(this SyntaxNode declaration, SyntaxNod
original = currentTrivia;
}
var newTrivia = await computeReplacementTriviaAsync(original, currentTrivia, cancellationToken).ConfigureAwait(false);
var newTrivia = await computeReplacementTriviaAsync!(original, currentTrivia, cancellationToken).ConfigureAwait(false);
triviaReplacements[currentTrivia] = newTrivia;
}
}
......@@ -756,7 +759,7 @@ private static SyntaxToken FindSkippedTokenBackward(SyntaxTriviaList triviaList,
}
// Copy of the same function in SyntaxNode.cs
public static SyntaxNode GetParent(this SyntaxNode node, bool ascendOutOfTrivia)
public static SyntaxNode? GetParent(this SyntaxNode node, bool ascendOutOfTrivia)
{
var parent = node.Parent;
if (parent == null && ascendOutOfTrivia)
......@@ -770,7 +773,7 @@ public static SyntaxNode GetParent(this SyntaxNode node, bool ascendOutOfTrivia)
return parent;
}
public static TNode FirstAncestorOrSelfUntil<TNode>(this SyntaxNode node, Func<SyntaxNode, bool> predicate)
public static TNode? FirstAncestorOrSelfUntil<TNode>(this SyntaxNode? node, Func<SyntaxNode, bool> predicate)
where TNode : SyntaxNode
{
for (var current = node; current != null; current = current.GetParent(ascendOutOfTrivia: 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;
using System.Collections.Generic;
using System.Linq;
......@@ -10,12 +12,12 @@ namespace Microsoft.CodeAnalysis.Shared.Extensions
{
internal static class SyntaxTokenExtensions
{
public static SyntaxNode GetAncestor(this SyntaxToken token, Func<SyntaxNode, bool> predicate)
public static SyntaxNode? GetAncestor(this SyntaxToken token, Func<SyntaxNode, bool>? predicate)
{
return token.GetAncestor<SyntaxNode>(predicate);
}
public static T GetAncestor<T>(this SyntaxToken token, Func<T, bool> predicate = null)
public static T? GetAncestor<T>(this SyntaxToken token, Func<T, bool>? predicate = null)
where T : SyntaxNode
{
return token.Parent != null
......@@ -38,7 +40,7 @@ public static IEnumerable<SyntaxNode> GetAncestors(this SyntaxToken token, Func<
: SpecializedCollections.EmptyEnumerable<SyntaxNode>();
}
public static SyntaxNode GetCommonRoot(this SyntaxToken token1, SyntaxToken token2)
public static SyntaxNode? GetCommonRoot(this SyntaxToken token1, SyntaxToken token2)
{
Contract.ThrowIfTrue(token1.RawKind == 0 || token2.RawKind == 0);
......
// 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.Text;
using Roslyn.Utilities;
......@@ -34,7 +36,7 @@ public static TextDocument WithText(this TextDocument textDocument, SourceText t
public static TextDocument WithAdditionalDocumentText(this TextDocument textDocument, SourceText text)
{
Contract.ThrowIfFalse(textDocument is AdditionalDocument);
return textDocument.Project.Solution.WithAdditionalDocumentText(textDocument.Id, text, PreservationMode.PreserveIdentity).GetTextDocument(textDocument.Id);
return textDocument.Project.Solution.WithAdditionalDocumentText(textDocument.Id, text, PreservationMode.PreserveIdentity).GetTextDocument(textDocument.Id)!;
}
/// <summary>
......@@ -43,7 +45,7 @@ public static TextDocument WithAdditionalDocumentText(this TextDocument textDocu
public static TextDocument WithAnalyzerConfigDocumentText(this TextDocument textDocument, SourceText text)
{
Contract.ThrowIfFalse(textDocument is AnalyzerConfigDocument);
return textDocument.Project.Solution.WithAnalyzerConfigDocumentText(textDocument.Id, text, PreservationMode.PreserveIdentity).GetTextDocument(textDocument.Id);
return textDocument.Project.Solution.WithAnalyzerConfigDocumentText(textDocument.Id, text, PreservationMode.PreserveIdentity).GetTextDocument(textDocument.Id)!;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册