Skip to content

Commit

Permalink
Merge pull request #17728 from CyrusNajmabadi/usePatterns
Browse files Browse the repository at this point in the history
Use pattern matching to help clean up a lot of IDE code.
  • Loading branch information
CyrusNajmabadi authored Mar 11, 2017
2 parents 58baf35 + 0768420 commit 5f014b5
Show file tree
Hide file tree
Showing 102 changed files with 556 additions and 691 deletions.
16 changes: 8 additions & 8 deletions src/EditorFeatures/CSharpTest/SymbolId/SymbolKeyTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ internal static List<BlockSyntax> GetBlockSyntaxList(MethodSymbol symbol)
foreach (var node in symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()))
{
BlockSyntax body = null;
if (node is BaseMethodDeclarationSyntax)
if (node is BaseMethodDeclarationSyntax baseMethod)
{
body = (node as BaseMethodDeclarationSyntax).Body;
body = baseMethod.Body;
}
else if (node is AccessorDeclarationSyntax)
else if (node is AccessorDeclarationSyntax accessor)
{
body = (node as AccessorDeclarationSyntax).Body;
body = accessor.Body;
}

if (body != null || body.Statements.Any())
Expand Down Expand Up @@ -300,13 +300,13 @@ public void GetLocalSymbols(MethodSymbol symbol, List<ISymbol> list)
foreach (var node in symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()))
{
BlockSyntax body = null;
if (node is BaseMethodDeclarationSyntax)
if (node is BaseMethodDeclarationSyntax baseMethod)
{
body = (node as BaseMethodDeclarationSyntax).Body;
body = baseMethod.Body;
}
else if (node is AccessorDeclarationSyntax)
else if (node is AccessorDeclarationSyntax accessor)
{
body = (node as AccessorDeclarationSyntax).Body;
body = accessor.Body;
}

var model = _compilation.GetSemanticModel(node.SyntaxTree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public static bool TryGoToDefinition(
// We can't go to the definition of the alias, so use the target type.

var solution = project.Solution;
if (symbol is IAliasSymbol)
if (alias != null)
{
var sourceLocations = NavigableItemFactory.GetPreferredSourceLocations(
solution, symbol, cancellationToken);

if (sourceLocations.All(l => project.Solution.GetDocument(l.SourceTree) == null))
{
symbol = ((IAliasSymbol)symbol).Target;
symbol = alias.Target;
}
}

Expand All @@ -63,9 +63,9 @@ public static bool TryGoToDefinition(

// If it is a partial method declaration with no body, choose to go to the implementation
// that has a method body.
if (symbol is IMethodSymbol)
if (symbol is IMethodSymbol method)
{
symbol = ((IMethodSymbol)symbol).PartialImplementationPart ?? symbol;
symbol = method.PartialImplementationPart ?? symbol;
}

var options = project.Solution.Options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ public async Task<ICallHierarchyMemberItem> CreateItem(ISymbol symbol,

private ISymbol GetTargetSymbol(ISymbol symbol)
{
if (symbol is IMethodSymbol)
if (symbol is IMethodSymbol methodSymbol)
{
var methodSymbol = (IMethodSymbol)symbol;
methodSymbol = methodSymbol.ReducedFrom != null ? methodSymbol.ReducedFrom : methodSymbol;
methodSymbol = methodSymbol.ConstructedFrom != null ? methodSymbol.ConstructedFrom : methodSymbol;
methodSymbol = methodSymbol.ReducedFrom ?? methodSymbol;
methodSymbol = methodSymbol.ConstructedFrom ?? methodSymbol;
return methodSymbol;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ public void ExecuteReturnOrTypeCommand(CommandArgs args, Action nextHandler, Can
return;
}
}
else if (args is TypeCharCommandArgs)
else if (args is TypeCharCommandArgs typeCharArgs)
{
var typedChar = ((TypeCharCommandArgs)args).TypedChar;
var typedChar = typeCharArgs.TypedChar;
if (!service.SupportsFormattingOnTypedCharacter(document, typedChar) ||
!TryFormat(textView, document, service, typedChar, caretPositionMarker, formatOnReturn: false, cancellationToken: cancellationToken))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ private static IEnumerable<Span> MapUpToSnapshotRecursive(SnapshotSpan start, IP
yield return result;
}
}
else if (source is IProjectionSnapshot)
else if (source is IProjectionSnapshot sourceProjection)
{
var sourceProjection = source as IProjectionSnapshot;
foreach (var span in MapUpToSnapshotRecursive(start, sourceProjection))
{
foreach (var result in target.MapFromSourceSnapshot(new SnapshotSpan(source, span)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public IEnumerable<TextSpan> GetHighlights(
{
for (var parent = token.Parent; parent != null; parent = parent.Parent)
{
if (parent is TNode)
if (parent is TNode parentTNode)
{
var highlights = GetHighlights((TNode)parent, cancellationToken);
var highlights = GetHighlights(parentTNode, cancellationToken);

// Only return them if any of them matched
if (highlights.Any(span => span.IntersectsWith(position)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,9 @@ protected Tuple<string, string>[] NoClassifications()

protected void WaitForDocumentationComment(object content)
{
if (content is QuickInfoDisplayDeferredContent)
if (content is QuickInfoDisplayDeferredContent deferredContent)
{
var docCommentDeferredContent = ((QuickInfoDisplayDeferredContent)content).Documentation as DocumentationCommentDeferredContent;
if (docCommentDeferredContent != null)
if (deferredContent.Documentation is DocumentationCommentDeferredContent docCommentDeferredContent)
{
docCommentDeferredContent.WaitForDocumentationCommentTask_ForTestingPurposesOnly();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ protected override bool CanAddImportForMethod(
{
break;
}
else if (node is MemberBindingExpressionSyntax)
else if (node is MemberBindingExpressionSyntax memberBindingExpr)
{
node = (node as MemberBindingExpressionSyntax).Name;
node = memberBindingExpr.Name;
}
break;
case CS1929:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,23 +259,22 @@ private static bool WouldCauseAmbiguity(
private static InvocationExpressionSyntax TryGetInvocationExpression(
SyntaxNode lambdaBody)
{
if (lambdaBody is ExpressionSyntax)
if (lambdaBody is ExpressionSyntax exprBody)
{
return ((ExpressionSyntax)lambdaBody).WalkDownParentheses() as InvocationExpressionSyntax;
return exprBody.WalkDownParentheses() as InvocationExpressionSyntax;
}
else if (lambdaBody is BlockSyntax)
else if (lambdaBody is BlockSyntax block)
{
var block = (BlockSyntax)lambdaBody;
if (block.Statements.Count == 1)
{
var statement = block.Statements.First();
if (statement is ReturnStatementSyntax)
if (statement is ReturnStatementSyntax returnStatement)
{
return ((ReturnStatementSyntax)statement).Expression.WalkDownParentheses() as InvocationExpressionSyntax;
return returnStatement.Expression.WalkDownParentheses() as InvocationExpressionSyntax;
}
else if (statement is ExpressionStatementSyntax)
else if (statement is ExpressionStatementSyntax exprStatement)
{
return ((ExpressionStatementSyntax)statement).Expression.WalkDownParentheses() as InvocationExpressionSyntax;
return exprStatement.Expression.WalkDownParentheses() as InvocationExpressionSyntax;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,19 @@ private void RemoveExistingTags(DocumentationCommentTriviaSyntax parentTrivia, I

private IEnumerable<CompletionItem> GetTagsForSymbol(ISymbol symbol, DocumentationCommentTriviaSyntax trivia)
{
if (symbol is IMethodSymbol)
if (symbol is IMethodSymbol method)
{
return GetTagsForMethod((IMethodSymbol)symbol, trivia);
return GetTagsForMethod(method, trivia);
}

if (symbol is IPropertySymbol)
if (symbol is IPropertySymbol property)
{
return GetTagsForProperty((IPropertySymbol)symbol, trivia);
return GetTagsForProperty(property, trivia);
}

if (symbol is INamedTypeSymbol)
if (symbol is INamedTypeSymbol namedType)
{
return GetTagsForType((INamedTypeSymbol)symbol, trivia);
return GetTagsForType(namedType, trivia);
}

return SpecializedCollections.EmptyEnumerable<CompletionItem>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ private bool IsInferredPredefinedType(SyntaxNode declarationStatement, SemanticM

private TypeSyntax GetTypeSyntaxFromDeclaration(SyntaxNode declarationStatement)
{
if (declarationStatement is VariableDeclarationSyntax)
if (declarationStatement is VariableDeclarationSyntax varDecl)
{
return ((VariableDeclarationSyntax)declarationStatement).Type;
return varDecl.Type;
}
else if (declarationStatement is ForEachStatementSyntax)
else if (declarationStatement is ForEachStatementSyntax forEach)
{
return ((ForEachStatementSyntax)declarationStatement).Type;
return forEach.Type;
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ private static string GetMethodNameBasedOnExpression(string methodName, SyntaxNo
return (name != null && name.Length > 0) ? MakeMethodName("Get", name) : methodName;
}

if (expression is MemberAccessExpressionSyntax)
if (expression is MemberAccessExpressionSyntax memberAccess)
{
expression = ((MemberAccessExpressionSyntax)expression).Name;
expression = memberAccess.Name;
}

if (expression is NameSyntax)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ private ITypeParameterSymbol GetMethodTypeParameter(TypeSyntax type, Cancellatio
if (type is IdentifierNameSyntax)
{
var info = this.Document.SemanticModel.GetTypeInfo(type, cancellationToken);
if (info.Type is ITypeParameterSymbol &&
((ITypeParameterSymbol)info.Type).TypeParameterKind == TypeParameterKind.Method)
if (info.Type is ITypeParameterSymbol typeParameter &&
typeParameter.TypeParameterKind == TypeParameterKind.Method)
{
return (ITypeParameterSymbol)info.Type;
return typeParameter;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,10 +677,10 @@ private bool IdentifierMatches(int indexDone, List<string> namespaceContainers,

private void GetNamespaceContainers(NameSyntax name, List<string> namespaceContainers)
{
if (name is QualifiedNameSyntax)
if (name is QualifiedNameSyntax qualifiedName)
{
GetNamespaceContainers(((QualifiedNameSyntax)name).Left, namespaceContainers);
namespaceContainers.Add(((QualifiedNameSyntax)name).Right.Identifier.ValueText);
GetNamespaceContainers(qualifiedName.Left, namespaceContainers);
namespaceContainers.Add(qualifiedName.Right.Identifier.ValueText);
}
else
{
Expand Down Expand Up @@ -833,9 +833,8 @@ internal override async Task<Solution> TryAddUsingsOrImportToDocumentAsync(Solut
root = modifiedRoot;
}

if (root is CompilationUnitSyntax)
if (root is CompilationUnitSyntax compilationRoot)
{
var compilationRoot = (CompilationUnitSyntax)root;
var usingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(includeUsingsOrImports));

// Check if the usings is already present
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,12 @@ private Document RewriteExpressionBodiedMemberAndIntroduceLocalDeclaration(
.WithAdditionalAnnotations(Formatter.Annotation);

SyntaxNode newParentingNode = null;
if (oldParentingNode is BasePropertyDeclarationSyntax)
if (oldParentingNode is BasePropertyDeclarationSyntax baseProperty)
{
var getAccessor = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration, newBody);
var accessorList = SyntaxFactory.AccessorList(SyntaxFactory.List(new[] { getAccessor }));

newParentingNode = ((BasePropertyDeclarationSyntax)oldParentingNode).RemoveNode(oldBody, SyntaxRemoveOptions.KeepNoTrivia);
newParentingNode = baseProperty.RemoveNode(oldBody, SyntaxRemoveOptions.KeepNoTrivia);

if (newParentingNode.IsKind(SyntaxKind.PropertyDeclaration))
{
Expand All @@ -256,11 +256,10 @@ private Document RewriteExpressionBodiedMemberAndIntroduceLocalDeclaration(
.WithTrailingTrivia(indexerDeclaration.SemicolonToken.TrailingTrivia);
}
}
else if (oldParentingNode is BaseMethodDeclarationSyntax)
else if (oldParentingNode is BaseMethodDeclarationSyntax baseMethod)
{
newParentingNode = ((BaseMethodDeclarationSyntax)oldParentingNode)
.RemoveNode(oldBody, SyntaxRemoveOptions.KeepNoTrivia)
.WithBody(newBody);
newParentingNode = baseMethod.RemoveNode(oldBody, SyntaxRemoveOptions.KeepNoTrivia)
.WithBody(newBody);

if (newParentingNode.IsKind(SyntaxKind.MethodDeclaration))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ protected override Task<ImmutableArray<SymbolDisplayPart>> GetInitializerSourceP
ISymbol symbol)
{
// Actually check for C# symbol types here.
if (symbol is IParameterSymbol)
if (symbol is IParameterSymbol parameter)
{
return GetInitializerSourcePartsAsync((IParameterSymbol)symbol);
return GetInitializerSourcePartsAsync(parameter);
}
else if (symbol is ILocalSymbol)
else if (symbol is ILocalSymbol local)
{
return GetInitializerSourcePartsAsync((ILocalSymbol)symbol);
return GetInitializerSourcePartsAsync(local);
}
else if (symbol is IFieldSymbol)
else if (symbol is IFieldSymbol field)
{
return GetInitializerSourcePartsAsync((IFieldSymbol)symbol);
return GetInitializerSourcePartsAsync(field);
}

return SpecializedTasks.EmptyImmutableArray<SymbolDisplayPart>();
Expand Down Expand Up @@ -151,9 +151,9 @@ private async Task<T> GetFirstDeclaration<T>(ISymbol symbol) where T : SyntaxNod
foreach (var syntaxRef in symbol.DeclaringSyntaxReferences)
{
var syntax = await syntaxRef.GetSyntaxAsync(this.CancellationToken).ConfigureAwait(false);
if (syntax is T)
if (syntax is T tSyntax)
{
return (T)syntax;
return tSyntax;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ protected override async Task<SignatureHelpItems> GetItemsWorkerAsync(Document d

var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var expressionSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).GetAnySymbol();
if (expressionSymbol is INamedTypeSymbol)
// foo?[$$]
if (expressionSymbol is INamedTypeSymbol namedType)
{
// foo?[$$]
var namedType = (INamedTypeSymbol)expressionSymbol;
if (namedType.ConstructedFrom.SpecialType == SpecialType.System_Nullable_T &&
expression.IsKind(SyntaxKind.NullableType) &&
expression.IsChildNode<ArrayTypeSyntax>(a => a.ElementType))
Expand Down Expand Up @@ -206,11 +205,11 @@ private bool TryGetIndexers(
return false;
}

if (expressionType is IErrorTypeSymbol)
if (expressionType is IErrorTypeSymbol errorType)
{
// If `expression` is a QualifiedNameSyntax then GetTypeInfo().Type won't have any CandidateSymbols, so
// we should then fall back to getting the actual symbol for the expression.
expressionType = (expressionType as IErrorTypeSymbol).CandidateSymbols.FirstOrDefault().GetSymbolType()
expressionType = errorType.CandidateSymbols.FirstOrDefault().GetSymbolType()
?? semanticModel.GetSymbolInfo(expression).GetAnySymbol().GetSymbolType();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,8 @@ private SignatureHelpItem Convert(
var position = lessThanToken.SpanStart;

SignatureHelpItem item;
if (symbol is INamedTypeSymbol)
if (symbol is INamedTypeSymbol namedType)
{
var namedType = (INamedTypeSymbol)symbol;
item = CreateItem(
symbol, semanticModel, position,
symbolDisplayService, anonymousTypeDisplayService,
Expand Down
Loading

0 comments on commit 5f014b5

Please sign in to comment.