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

Use pattern matching

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