提交 f5f980f0 编写于 作者: C Cyrus Najmabadi

Use pattern matching

上级 fcc69778
......@@ -32,15 +32,17 @@ public bool CanNavigateToPosition(Workspace workspace, DocumentId documentId, in
public bool TryNavigateToSpan(Workspace workspace, DocumentId documentId, TextSpan textSpan, OptionSet options)
{
var interactiveWorkspace = workspace as InteractiveWorkspace;
if (interactiveWorkspace == null)
using (var interactiveWorkspace = workspace as InteractiveWorkspace)
{
Debug.Fail("InteractiveDocumentNavigationService called with incorrect workspace!");
return false;
}
if (interactiveWorkspace == null)
{
Debug.Fail("InteractiveDocumentNavigationService called with incorrect workspace!");
return false;
}
var textView = interactiveWorkspace.Window.TextView;
var document = interactiveWorkspace.CurrentSolution.GetDocument(documentId);
var textView = interactiveWorkspace.Window.TextView;
var document = interactiveWorkspace.CurrentSolution.GetDocument(documentId);
}
var textSnapshot = document.GetTextSynchronously(CancellationToken.None).FindCorrespondingEditorTextSnapshot();
if (textSnapshot == null)
......
......@@ -28,8 +28,7 @@ internal abstract class AbstractDirectiveTriviaBraceMatcher<TDirectiveTriviaSynt
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var token = root.FindToken(position, findInsideTrivia: true);
var directive = token.Parent as TDirectiveTriviaSyntax;
if (directive == null)
if (!(token.Parent is TDirectiveTriviaSyntax directive))
{
return null;
}
......
......@@ -91,8 +91,7 @@ private bool TryGetMethodReturnType(SyntaxNode node, SemanticModel model, Cancel
{
methodReturnType = null;
var symbol = model.GetEnclosingSymbol(node.Span.Start, cancellationToken);
var method = symbol as IMethodSymbol;
if (method == null || method.ReturnsVoid)
if (!(symbol is IMethodSymbol method) || method.ReturnsVoid)
{
return false;
}
......
......@@ -313,8 +313,7 @@ private OperationStatus CheckActiveStatements(IEnumerable<StatementSyntax> state
foreach (var statement in statements)
{
var declarationStatement = statement as LocalDeclarationStatementSyntax;
if (declarationStatement == null)
if (!(statement is LocalDeclarationStatementSyntax declarationStatement))
{
// if given statement is not decl statement.
yield return statement;
......
......@@ -41,8 +41,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var propertySymbol = semanticModel.GetDeclaredSymbol(property) as IPropertySymbol;
if (propertySymbol == null)
if (!(semanticModel.GetDeclaredSymbol(property) is IPropertySymbol propertySymbol))
{
return;
}
......
......@@ -26,8 +26,7 @@ public LiveDiagnosticUpdateArgsId(DiagnosticAnalyzer analyzer, object key, int k
public override bool Equals(object obj)
{
var other = obj as LiveDiagnosticUpdateArgsId;
if (other == null)
if (!(obj is LiveDiagnosticUpdateArgsId other))
{
return false;
}
......
......@@ -182,9 +182,8 @@ private bool CompareExpressions(ExpressionSyntax oldExpression, ExpressionSyntax
&& CompareExpressions(oldBinaryExpression.Right, newBinaryExpression.Right);
}
if (oldExpression is AssignmentExpressionSyntax)
if (oldExpression is AssignmentExpressionSyntax oldAssignmentExpression)
{
var oldAssignmentExpression = (AssignmentExpressionSyntax)oldExpression;
var newAssignmentExpression = (AssignmentExpressionSyntax)newExpression;
return CompareExpressions(oldAssignmentExpression.Left, newAssignmentExpression.Left)
......
......@@ -1163,9 +1163,7 @@ private EnvDTE.vsCMAccess GetDefaultAccessibility(SyntaxNode node)
public override SyntaxNode SetAccess(SyntaxNode node, EnvDTE.vsCMAccess newAccess)
{
var member = node as MemberDeclarationSyntax;
if (member == null)
if (!(node is MemberDeclarationSyntax member))
{
throw Exceptions.ThrowEFail();
}
......
......@@ -98,8 +98,7 @@ private BlockSyntax GetImmediatelyContainingBlock()
private bool IsFirstBlockStatement()
{
var parentBlockOpt = _parentStatement.Parent as BlockSyntax;
return parentBlockOpt != null && parentBlockOpt.Statements.FirstOrDefault() == _parentStatement;
return _parentStatement.Parent is BlockSyntax parentBlockOpt && parentBlockOpt.Statements.FirstOrDefault() == _parentStatement;
}
private void AddCurrentDeclaration()
......
......@@ -207,8 +207,7 @@ protected void RemoveFile(string filename)
protected void RefreshBinOutputPath()
{
var storage = Hierarchy as IVsBuildPropertyStorage;
if (storage == null)
if (!(Hierarchy is IVsBuildPropertyStorage storage))
{
return;
}
......
......@@ -121,8 +121,7 @@ private void SetFocusToSelectedRow()
{
if (CodeStyleMembers.SelectedIndex >= 0)
{
var row = CodeStyleMembers.ItemContainerGenerator.ContainerFromIndex(CodeStyleMembers.SelectedIndex) as DataGridRow;
if (row == null)
if (!(CodeStyleMembers.ItemContainerGenerator.ContainerFromIndex(CodeStyleMembers.SelectedIndex) is DataGridRow row))
{
CodeStyleMembers.ScrollIntoView(CodeStyleMembers.SelectedItem);
row = CodeStyleMembers.ItemContainerGenerator.ContainerFromIndex(CodeStyleMembers.SelectedIndex) as DataGridRow;
......
......@@ -2201,8 +2201,7 @@ public override SyntaxNode GetType(SyntaxNode declaration)
private static TypeSyntax NotVoid(TypeSyntax type)
{
var pd = type as PredefinedTypeSyntax;
return pd != null && pd.Keyword.IsKind(SyntaxKind.VoidKeyword) ? null : type;
return type is PredefinedTypeSyntax pd && pd.Keyword.IsKind(SyntaxKind.VoidKeyword) ? null : type;
}
public override SyntaxNode WithType(SyntaxNode declaration, SyntaxNode type)
......@@ -2383,8 +2382,7 @@ public override IReadOnlyList<SyntaxNode> GetSwitchSections(SyntaxNode switchSta
public override SyntaxNode InsertSwitchSections(SyntaxNode switchStatement, int index, IEnumerable<SyntaxNode> switchSections)
{
var statement = switchStatement as SwitchStatementSyntax;
if (statement == null)
if (!(switchStatement is SwitchStatementSyntax statement))
{
return switchStatement;
}
......
......@@ -344,8 +344,7 @@ private IndentationResult GetIndentationForQueryExpression(SyntaxToken token)
}
// find query body that has a token that is a first token on the line
var queryBody = queryExpressionClause.Parent as QueryBodySyntax;
if (queryBody == null)
if (!(queryExpressionClause.Parent is QueryBodySyntax queryBody))
{
return GetIndentationOfToken(firstToken);
}
......
......@@ -114,9 +114,8 @@ public static SemanticModel CreateSpeculativeSemanticModelForNode(SyntaxNode nod
semanticModel = semanticModel.ParentModel;
}
var statementNode = nodeToSpeculate as StatementSyntax;
SemanticModel speculativeModel;
if (statementNode != null)
if (nodeToSpeculate is StatementSyntax statementNode)
{
semanticModel.TryGetSpeculativeSemanticModel(position, statementNode, out speculativeModel);
return speculativeModel;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册