diff --git a/src/Analyzers/CSharp/Analyzers/NewLines/ArrowExpressionClausePlacement/ArrowExpressionClausePlacementDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/NewLines/ArrowExpressionClausePlacement/ArrowExpressionClausePlacementDiagnosticAnalyzer.cs index 8972833f5689d..d7aec18cecdf8 100644 --- a/src/Analyzers/CSharp/Analyzers/NewLines/ArrowExpressionClausePlacement/ArrowExpressionClausePlacementDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/NewLines/ArrowExpressionClausePlacement/ArrowExpressionClausePlacementDiagnosticAnalyzer.cs @@ -9,6 +9,7 @@ using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Shared.Extensions; namespace Microsoft.CodeAnalysis.CSharp.NewLines.ArrowExpressionClausePlacement; @@ -52,8 +53,8 @@ private void Recurse(SyntaxTreeAnalysisContext context, NotificationOption2 noti if (!context.ShouldAnalyzeSpan(child.Span)) continue; - if (child.IsNode) - Recurse(context, notificationOption, child.AsNode()!); + if (child.AsNode(out var childNode)) + Recurse(context, notificationOption, childNode); } } diff --git a/src/Analyzers/CSharp/Analyzers/NewLines/ConstructorInitializerPlacement/ConstructorInitializerPlacementDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/NewLines/ConstructorInitializerPlacement/ConstructorInitializerPlacementDiagnosticAnalyzer.cs index d43ac7418c66e..ac3e534a24d81 100644 --- a/src/Analyzers/CSharp/Analyzers/NewLines/ConstructorInitializerPlacement/ConstructorInitializerPlacementDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/NewLines/ConstructorInitializerPlacement/ConstructorInitializerPlacementDiagnosticAnalyzer.cs @@ -7,10 +7,10 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeStyle; using Microsoft.CodeAnalysis.CSharp.CodeStyle; -using Microsoft.CodeAnalysis.CSharp.Diagnostics; using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Shared.Extensions; namespace Microsoft.CodeAnalysis.CSharp.NewLines.ConstructorInitializerPlacement; @@ -58,8 +58,8 @@ private void Recurse(SyntaxTreeAnalysisContext context, NotificationOption2 noti if (!context.ShouldAnalyzeSpan(child.Span)) continue; - if (child.IsNode) - Recurse(context, notificationOption, child.AsNode()!); + if (child.AsNode(out var childNode)) + Recurse(context, notificationOption, childNode); } } diff --git a/src/Analyzers/CSharp/Analyzers/NewLines/EmbeddedStatementPlacement/EmbeddedStatementPlacementDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/NewLines/EmbeddedStatementPlacement/EmbeddedStatementPlacementDiagnosticAnalyzer.cs index 8aa7f141c6568..a7af57f730c9f 100644 --- a/src/Analyzers/CSharp/Analyzers/NewLines/EmbeddedStatementPlacement/EmbeddedStatementPlacementDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/NewLines/EmbeddedStatementPlacement/EmbeddedStatementPlacementDiagnosticAnalyzer.cs @@ -7,9 +7,9 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeStyle; using Microsoft.CodeAnalysis.CSharp.CodeStyle; -using Microsoft.CodeAnalysis.CSharp.Diagnostics; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Shared.Extensions; namespace Microsoft.CodeAnalysis.CSharp.NewLines.EmbeddedStatementPlacement; @@ -63,8 +63,8 @@ private void Recurse(SyntaxTreeAnalysisContext context, NotificationOption2 noti if (!context.ShouldAnalyzeSpan(child.Span)) continue; - if (child.IsNode) - Recurse(context, notificationOption, child.AsNode()!); + if (child.AsNode(out var childNode)) + Recurse(context, notificationOption, childNode); } } diff --git a/src/Analyzers/CSharp/Analyzers/RemoveUnnecessaryNullableDirective/CSharpRemoveRedundantNullableDirectiveDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/RemoveUnnecessaryNullableDirective/CSharpRemoveRedundantNullableDirectiveDiagnosticAnalyzer.cs index e7f238402a12d..f1ed887255e6a 100644 --- a/src/Analyzers/CSharp/Analyzers/RemoveUnnecessaryNullableDirective/CSharpRemoveRedundantNullableDirectiveDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/RemoveUnnecessaryNullableDirective/CSharpRemoveRedundantNullableDirectiveDiagnosticAnalyzer.cs @@ -72,10 +72,10 @@ private void ProcessSyntaxTree( if (!current.ContainsDirectives) continue; - if (current.IsNode) + if (current.AsNode(out var childNode)) { // Add the nodes in reverse so we continue walking in a depth-first fashion. - foreach (var child in current.AsNode()!.ChildNodesAndTokens().Reverse()) + foreach (var child in childNode.ChildNodesAndTokens().Reverse()) stack.Push(child); } else if (current.IsToken) diff --git a/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpIsAndCastCheckDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpIsAndCastCheckDiagnosticAnalyzer.cs index 6c1872cccbd0e..3d490403c7d1e 100644 --- a/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpIsAndCastCheckDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UsePatternMatching/CSharpIsAndCastCheckDiagnosticAnalyzer.cs @@ -233,8 +233,8 @@ private static bool ContainsVariableDeclaration( foreach (var child in current.ChildNodesAndTokens()) { - if (child.IsNode) - stack.Push(child.AsNode()!); + if (child.AsNode(out var childNode)) + stack.Push(childNode); } } diff --git a/src/Analyzers/Core/Analyzers/NewLines/ConsecutiveStatementPlacement/AbstractConsecutiveStatementPlacementDiagnosticAnalyzer.cs b/src/Analyzers/Core/Analyzers/NewLines/ConsecutiveStatementPlacement/AbstractConsecutiveStatementPlacementDiagnosticAnalyzer.cs index 6f2572311e6d6..5af03023ac6ab 100644 --- a/src/Analyzers/Core/Analyzers/NewLines/ConsecutiveStatementPlacement/AbstractConsecutiveStatementPlacementDiagnosticAnalyzer.cs +++ b/src/Analyzers/Core/Analyzers/NewLines/ConsecutiveStatementPlacement/AbstractConsecutiveStatementPlacementDiagnosticAnalyzer.cs @@ -8,6 +8,7 @@ using Microsoft.CodeAnalysis.CodeStyle; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.LanguageService; +using Microsoft.CodeAnalysis.Shared.Extensions; namespace Microsoft.CodeAnalysis.NewLines.ConsecutiveStatementPlacement; @@ -59,8 +60,8 @@ private void Recurse(SyntaxTreeAnalysisContext context, NotificationOption2 noti if (!context.ShouldAnalyzeSpan(child.FullSpan)) continue; - if (child.IsNode) - Recurse(context, notificationOption, child.AsNode()!, cancellationToken); + if (child.AsNode(out var childNode)) + Recurse(context, notificationOption, childNode, cancellationToken); } } diff --git a/src/Analyzers/Core/Analyzers/NewLines/MultipleBlankLines/AbstractMultipleBlankLinesDiagnosticAnalyzer.cs b/src/Analyzers/Core/Analyzers/NewLines/MultipleBlankLines/AbstractMultipleBlankLinesDiagnosticAnalyzer.cs index 45b7cf0ac6921..c69ddbcdd7de6 100644 --- a/src/Analyzers/Core/Analyzers/NewLines/MultipleBlankLines/AbstractMultipleBlankLinesDiagnosticAnalyzer.cs +++ b/src/Analyzers/Core/Analyzers/NewLines/MultipleBlankLines/AbstractMultipleBlankLinesDiagnosticAnalyzer.cs @@ -4,10 +4,10 @@ using System.Collections.Immutable; using System.Threading; -using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeStyle; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.LanguageService; +using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Text; namespace Microsoft.CodeAnalysis.NewLines.MultipleBlankLines; @@ -59,8 +59,8 @@ private void Recurse( if (!context.ShouldAnalyzeSpan(child.FullSpan)) continue; - if (child.IsNode) - Recurse(context, notificationOption, child.AsNode()!, cancellationToken); + if (child.AsNode(out var childNode)) + Recurse(context, notificationOption, childNode, cancellationToken); else if (child.IsToken) CheckToken(context, notificationOption, child.AsToken()); } diff --git a/src/Analyzers/Core/Analyzers/UseObjectInitializer/UseNamedMemberInitializerAnalyzer.cs b/src/Analyzers/Core/Analyzers/UseObjectInitializer/UseNamedMemberInitializerAnalyzer.cs index 8f0fb7a0a3c33..b9ad90458a74b 100644 --- a/src/Analyzers/Core/Analyzers/UseObjectInitializer/UseNamedMemberInitializerAnalyzer.cs +++ b/src/Analyzers/Core/Analyzers/UseObjectInitializer/UseNamedMemberInitializerAnalyzer.cs @@ -195,8 +195,8 @@ private bool ImplicitMemberAccessWouldBeAffected(SyntaxNode node) { foreach (var child in node.ChildNodesAndTokens()) { - if (child.IsNode && - ImplicitMemberAccessWouldBeAffected(child.AsNode()!)) + if (child.AsNode(out var childNode) && + ImplicitMemberAccessWouldBeAffected(childNode)) { return true; } diff --git a/src/Features/CSharp/Portable/StringIndentation/CSharpStringIndentationService.cs b/src/Features/CSharp/Portable/StringIndentation/CSharpStringIndentationService.cs index fd5d41cbb8f57..9f3e527a33d04 100644 --- a/src/Features/CSharp/Portable/StringIndentation/CSharpStringIndentationService.cs +++ b/src/Features/CSharp/Portable/StringIndentation/CSharpStringIndentationService.cs @@ -67,9 +67,9 @@ private static void Recurse( foreach (var child in node.ChildNodesAndTokens().Reverse()) { - if (child.IsNode) + if (child.AsNode(out var childNode)) { - nodeStack.Add(child.AsNode()!); + nodeStack.Add(childNode); } else if (child.IsToken) { diff --git a/src/Features/CSharp/Portable/UsePatternMatching/CSharpIsAndCastCheckWithoutNameDiagnosticAnalyzer.cs b/src/Features/CSharp/Portable/UsePatternMatching/CSharpIsAndCastCheckWithoutNameDiagnosticAnalyzer.cs index 52b7f4ad02c69..49566f73aa17c 100644 --- a/src/Features/CSharp/Portable/UsePatternMatching/CSharpIsAndCastCheckWithoutNameDiagnosticAnalyzer.cs +++ b/src/Features/CSharp/Portable/UsePatternMatching/CSharpIsAndCastCheckWithoutNameDiagnosticAnalyzer.cs @@ -261,10 +261,8 @@ private static void AddMatches( foreach (var child in node.ChildNodesAndTokens()) { - if (child.IsNode) - { - AddMatches(child.AsNode()!, expr, type, matches); - } + if (child.AsNode(out var childNode)) + AddMatches(childNode, expr, type, matches); } } } diff --git a/src/Features/Core/Portable/ConvertForToForEach/AbstractConvertForToForEachCodeRefactoringProvider.cs b/src/Features/Core/Portable/ConvertForToForEach/AbstractConvertForToForEachCodeRefactoringProvider.cs index 2f01d31dcacb6..2a45f5d02ccfc 100644 --- a/src/Features/Core/Portable/ConvertForToForEach/AbstractConvertForToForEachCodeRefactoringProvider.cs +++ b/src/Features/Core/Portable/ConvertForToForEach/AbstractConvertForToForEachCodeRefactoringProvider.cs @@ -181,10 +181,10 @@ bool IterationVariableIsUsedForMoreThanCollectionIndex(SyntaxNode current) foreach (var child in current.ChildNodesAndTokens()) { - if (child.IsNode) + if (child.AsNode(out var childNode) && + IterationVariableIsUsedForMoreThanCollectionIndex(childNode)) { - if (IterationVariableIsUsedForMoreThanCollectionIndex(child.AsNode()!)) - return true; + return true; } } @@ -458,8 +458,8 @@ void FindAndReplaceMatches(SyntaxNode current) foreach (var child in current.ChildNodesAndTokens()) { - if (child.IsNode) - FindAndReplaceMatches(child.AsNode()!); + if (child.AsNode(out var childNode)) + FindAndReplaceMatches(childNode); } } diff --git a/src/Features/Core/Portable/EmbeddedLanguages/Json/LanguageServices/AbstractJsonDetectionAnalyzer.cs b/src/Features/Core/Portable/EmbeddedLanguages/Json/LanguageServices/AbstractJsonDetectionAnalyzer.cs index ec05f4ab08f77..b8b5a698d232c 100644 --- a/src/Features/Core/Portable/EmbeddedLanguages/Json/LanguageServices/AbstractJsonDetectionAnalyzer.cs +++ b/src/Features/Core/Portable/EmbeddedLanguages/Json/LanguageServices/AbstractJsonDetectionAnalyzer.cs @@ -7,6 +7,7 @@ using Microsoft.CodeAnalysis.CodeStyle; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.EmbeddedLanguages; +using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Simplification; namespace Microsoft.CodeAnalysis.Features.EmbeddedLanguages.Json.LanguageServices; @@ -69,9 +70,9 @@ private void Analyze( if (!context.ShouldAnalyzeSpan(child.FullSpan)) continue; - if (child.IsNode) + if (child.AsNode(out var childNode)) { - Analyze(context, detector, child.AsNode()!, cancellationToken); + Analyze(context, detector, childNode, cancellationToken); } else { diff --git a/src/Features/Core/Portable/EmbeddedLanguages/Json/LanguageServices/AbstractJsonDiagnosticAnalyzer.cs b/src/Features/Core/Portable/EmbeddedLanguages/Json/LanguageServices/AbstractJsonDiagnosticAnalyzer.cs index 102c6c69088ef..1aee56cdf14fc 100644 --- a/src/Features/Core/Portable/EmbeddedLanguages/Json/LanguageServices/AbstractJsonDiagnosticAnalyzer.cs +++ b/src/Features/Core/Portable/EmbeddedLanguages/Json/LanguageServices/AbstractJsonDiagnosticAnalyzer.cs @@ -6,6 +6,7 @@ using Microsoft.CodeAnalysis.CodeStyle; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.EmbeddedLanguages; +using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Simplification; namespace Microsoft.CodeAnalysis.Features.EmbeddedLanguages.Json.LanguageServices; @@ -63,9 +64,9 @@ private void Analyze( if (!context.ShouldAnalyzeSpan(child.FullSpan)) continue; - if (child.IsNode) + if (child.AsNode(out var childNode)) { - Analyze(context, detector, child.AsNode()!, cancellationToken); + Analyze(context, detector, childNode, cancellationToken); } else { diff --git a/src/Features/Core/Portable/Structure/Syntax/BlockSpanCollector.cs b/src/Features/Core/Portable/Structure/Syntax/BlockSpanCollector.cs index 87e15f926b802..6e5ac0636cf34 100644 --- a/src/Features/Core/Portable/Structure/Syntax/BlockSpanCollector.cs +++ b/src/Features/Core/Portable/Structure/Syntax/BlockSpanCollector.cs @@ -6,6 +6,7 @@ using System.Collections.Immutable; using System.Threading; using Microsoft.CodeAnalysis.Shared.Collections; +using Microsoft.CodeAnalysis.Shared.Extensions; namespace Microsoft.CodeAnalysis.Structure; @@ -47,9 +48,9 @@ private void Collect(SyntaxNode root, ref TemporaryArray spans) SyntaxToken previousToken = default; foreach (var nodeOrToken in root.DescendantNodesAndTokensAndSelf(descendIntoTrivia: true)) { - if (nodeOrToken.IsNode) + if (nodeOrToken.AsNode(out var childNode)) { - GetBlockSpans(previousToken, nodeOrToken.AsNode()!, ref spans); + GetBlockSpans(previousToken, childNode, ref spans); } else { diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindLiterals/FindLiteralsSearchEngine.cs b/src/Workspaces/Core/Portable/FindSymbols/FindLiterals/FindLiteralsSearchEngine.cs index edc98a9a92721..c993c80dd9373 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindLiterals/FindLiteralsSearchEngine.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindLiterals/FindLiteralsSearchEngine.cs @@ -154,9 +154,9 @@ private void ProcessNode( cancellationToken.ThrowIfCancellationRequested(); foreach (var child in node.ChildNodesAndTokens()) { - if (child.IsNode) + if (child.AsNode(out var childNode)) { - ProcessNode(syntaxFacts, child.AsNode()!, matches, cancellationToken); + ProcessNode(syntaxFacts, childNode, matches, cancellationToken); } else { diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferenceCache.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferenceCache.cs index f76b366efde47..040c92bbedb57 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferenceCache.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferenceCache.cs @@ -161,9 +161,9 @@ private ImmutableArray FindMatchingIdentifierTokensFromTree( while (stack.TryPop(out var current)) { cancellationToken.ThrowIfCancellationRequested(); - if (current.IsNode) + if (current.AsNode(out var currentNode)) { - foreach (var child in current.AsNode()!.ChildNodesAndTokens().Reverse()) + foreach (var child in currentNode.ChildNodesAndTokens().Reverse()) stack.Push(child); } else if (current.IsToken) diff --git a/src/Workspaces/Core/Portable/FindSymbols/Shared/AbstractSyntaxIndex.cs b/src/Workspaces/Core/Portable/FindSymbols/Shared/AbstractSyntaxIndex.cs index f4c68ab40e9c5..e7fa239714abf 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/Shared/AbstractSyntaxIndex.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/Shared/AbstractSyntaxIndex.cs @@ -6,6 +6,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.LanguageService; +using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Storage; using Roslyn.Utilities; @@ -141,9 +142,9 @@ private static bool ContainsIfDirective(SyntaxNode node, int ifDirectiveKind) if (!child.ContainsDirectives) continue; - if (child.IsNode) + if (child.AsNode(out var childNode)) { - if (ContainsIfDirective(child.AsNode()!, ifDirectiveKind)) + if (ContainsIfDirective(childNode, ifDirectiveKind)) return true; } else diff --git a/src/Workspaces/Core/Portable/ObsoleteSymbol/AbstractObsoleteSymbolService.cs b/src/Workspaces/Core/Portable/ObsoleteSymbol/AbstractObsoleteSymbolService.cs index 675a1bb6056d3..b5a946e686453 100644 --- a/src/Workspaces/Core/Portable/ObsoleteSymbol/AbstractObsoleteSymbolService.cs +++ b/src/Workspaces/Core/Portable/ObsoleteSymbol/AbstractObsoleteSymbolService.cs @@ -75,10 +75,8 @@ void Recurse(TextSpan span, SemanticModel semanticModel) foreach (var child in current.ChildNodesAndTokens()) { - if (child.IsNode) - { - stack.Add(child.AsNode()!); - } + if (child.AsNode(out var childNode)) + stack.Add(childNode); var token = child.AsToken(); if (token != tokenFromNode) diff --git a/src/Workspaces/Core/Portable/ReassignedVariable/AbstractReassignedVariableService.cs b/src/Workspaces/Core/Portable/ReassignedVariable/AbstractReassignedVariableService.cs index 2393e5dff2ef2..4bf8380866395 100644 --- a/src/Workspaces/Core/Portable/ReassignedVariable/AbstractReassignedVariableService.cs +++ b/src/Workspaces/Core/Portable/ReassignedVariable/AbstractReassignedVariableService.cs @@ -80,8 +80,8 @@ void Recurse(TextSpan span, SemanticModel semanticModel) foreach (var child in current.ChildNodesAndTokens()) { - if (child.IsNode) - stack.Add(child.AsNode()!); + if (child.AsNode(out var childNode)) + stack.Add(childNode); } } } @@ -305,8 +305,8 @@ bool AnalyzePotentialMatches( foreach (var child in current.ChildNodesAndTokens()) { - if (child.IsNode) - stack.Add(child.AsNode()!); + if (child.AsNode(out var childNode)) + stack.Add(childNode); } // Ignore any nodes before the decl. diff --git a/src/Workspaces/Core/Portable/Shared/Utilities/SemanticMap.Walker.cs b/src/Workspaces/Core/Portable/Shared/Utilities/SemanticMap.Walker.cs index 8e7096e38ade4..9527e11960ee5 100644 --- a/src/Workspaces/Core/Portable/Shared/Utilities/SemanticMap.Walker.cs +++ b/src/Workspaces/Core/Portable/Shared/Utilities/SemanticMap.Walker.cs @@ -16,9 +16,8 @@ public void Visit(SyntaxNode node) { foreach (var child in node.DescendantNodesAndTokensAndSelf()) { - if (child.IsNode) + if (child.AsNode(out var childNode)) { - var childNode = child.AsNode()!; var info = semanticModel.GetSymbolInfo(childNode); if (!IsNone(info)) { diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/ParenthesizedExpressionSyntaxExtensions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/ParenthesizedExpressionSyntaxExtensions.cs index 0eb50260e6875..09054f97791ee 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/ParenthesizedExpressionSyntaxExtensions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/ParenthesizedExpressionSyntaxExtensions.cs @@ -348,9 +348,10 @@ private static bool RemovalMayIntroduceInterpolationAmbiguity(ParenthesizedExpre foreach (var nodeOrToken in expression.ChildNodesAndTokens()) { // Note: There's no need drill into other parenthesized expressions, since any colons in them would be unambiguous. - if (nodeOrToken.IsNode && !nodeOrToken.IsKind(SyntaxKind.ParenthesizedExpression)) + if (nodeOrToken.AsNode(out var childNode)) { - stack.Push(nodeOrToken.AsNode()!); + if (!childNode.IsKind(SyntaxKind.ParenthesizedExpression)) + stack.Push(childNode); } else if (nodeOrToken.IsToken) { diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/SemanticModelExtensions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/SemanticModelExtensions.cs index f48b8c7386d28..22d9ed8f0b157 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/SemanticModelExtensions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/SemanticModelExtensions.cs @@ -137,13 +137,10 @@ private static void GetAllDeclaredSymbols( foreach (var child in node.ChildNodesAndTokens()) { - if (child.IsNode) + if (child.AsNode(out var childNode) && + ShouldDescendInto(childNode, descendInto)) { - var childNode = child.AsNode()!; - if (ShouldDescendInto(childNode, descendInto)) - { - GetAllDeclaredSymbols(semanticModel, childNode, symbols, cancellationToken, descendInto); - } + GetAllDeclaredSymbols(semanticModel, childNode, symbols, cancellationToken, descendInto); } } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/SyntaxNodeOrTokenExtensions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/SyntaxNodeOrTokenExtensions.cs index bb0878db8dd8e..bf41872059309 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/SyntaxNodeOrTokenExtensions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/SyntaxNodeOrTokenExtensions.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using Roslyn.Utilities; @@ -10,6 +11,18 @@ namespace Microsoft.CodeAnalysis.Shared.Extensions; internal static class SyntaxNodeOrTokenExtensions { + public static bool AsNode(this SyntaxNodeOrToken nodeOrToken, [NotNullWhen(true)] out SyntaxNode? node) + { + if (nodeOrToken.IsNode) + { + node = nodeOrToken.AsNode(); + return node != null; + } + + node = null; + return false; + } + public static IEnumerable DepthFirstTraversal(this SyntaxNodeOrToken node) { using var pooledStack = SharedPools.Default>().GetPooledObject(); @@ -32,8 +45,8 @@ public static IEnumerable DepthFirstTraversalNodes(this SyntaxNodeOr { foreach (var t in node.DepthFirstTraversal()) { - if (t.IsNode) - yield return t.AsNode()!; + if (t.AsNode(out var childNode)) + yield return childNode; } } @@ -44,5 +57,5 @@ public static SyntaxNodeOrToken WithAppendedTrailingTrivia(this SyntaxNodeOrToke => WithAppendedTrailingTrivia(nodeOrToken, (IEnumerable)trivia); public static SyntaxNodeOrToken WithAppendedTrailingTrivia(this SyntaxNodeOrToken nodeOrToken, IEnumerable trivia) - => nodeOrToken.IsNode ? nodeOrToken.AsNode()!.WithAppendedTrailingTrivia(trivia) : nodeOrToken.AsToken().WithAppendedTrailingTrivia(trivia); + => nodeOrToken.AsNode(out var node) ? node.WithAppendedTrailingTrivia(trivia) : nodeOrToken.AsToken().WithAppendedTrailingTrivia(trivia); } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Formatting/FormattingExtensions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Formatting/FormattingExtensions.cs index 0ab7048ad4140..2f56cc8771715 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Formatting/FormattingExtensions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Formatting/FormattingExtensions.cs @@ -279,8 +279,9 @@ static IEnumerable EnumerateAnnotatedSpans(SyntaxNode node, SyntaxAnno { foreach (var nodeOrToken in node.GetAnnotatedNodesAndTokens(annotation)) { - var firstToken = nodeOrToken.IsNode ? nodeOrToken.AsNode()!.GetFirstToken(includeZeroWidth: true) : nodeOrToken.AsToken(); - var lastToken = nodeOrToken.IsNode ? nodeOrToken.AsNode()!.GetLastToken(includeZeroWidth: true) : nodeOrToken.AsToken(); + var (firstToken, lastToken) = nodeOrToken.AsNode(out var childNode) + ? (childNode.GetFirstToken(includeZeroWidth: true), childNode.GetLastToken(includeZeroWidth: true)) + : (nodeOrToken.AsToken(), nodeOrToken.AsToken()); yield return GetSpan(firstToken, lastToken); } } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/SymbolKey/SymbolKey.AliasSymbolKey.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/SymbolKey/SymbolKey.AliasSymbolKey.cs index b14c174d20501..b1e8faec2503d 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/SymbolKey/SymbolKey.AliasSymbolKey.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/SymbolKey/SymbolKey.AliasSymbolKey.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading; +using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Shared.Utilities; namespace Microsoft.CodeAnalysis; @@ -80,9 +81,9 @@ protected sealed override SymbolKeyResolution Resolve( foreach (var child in syntaxNode.ChildNodesAndTokens()) { - if (child.IsNode) + if (child.AsNode(out var childNode)) { - var result = Resolve(semanticModel, child.AsNode()!, name, target, cancellationToken); + var result = Resolve(semanticModel, childNode, name, target, cancellationToken); if (result.HasValue) { return result;