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

Use pattern matching

上级 e8607c1d
......@@ -26,8 +26,7 @@ public static IEnumerable<T> Do<T>(this IEnumerable<T> source, Action<T> action)
}
// perf optimization. try to not use enumerator if possible
var list = source as IList<T>;
if (list != null)
if (source is IList<T> list)
{
for (int i = 0, count = list.Count; i < count; i++)
{
......@@ -168,14 +167,12 @@ public static bool IsSingle<T>(this IEnumerable<T> list)
public static bool IsEmpty<T>(this IEnumerable<T> source)
{
var readOnlyCollection = source as IReadOnlyCollection<T>;
if (readOnlyCollection != null)
if (source is IReadOnlyCollection<T> readOnlyCollection)
{
return readOnlyCollection.Count == 0;
}
var genericCollection = source as ICollection<T>;
if (genericCollection != null)
if (source is ICollection<T> genericCollection)
{
return genericCollection.Count == 0;
}
......
......@@ -28,8 +28,7 @@ internal static async Task<DebugDataTipInfo> GetInfoAsync(Document document, int
var token = root.FindToken(position);
var expression = token.Parent as ExpressionSyntax;
if (expression == null)
if (!(token.Parent is ExpressionSyntax expression))
{
return token.IsKind(SyntaxKind.IdentifierToken)
? new DebugDataTipInfo(token.Span, text: null)
......
......@@ -65,12 +65,11 @@ private static CSharpProjectShim GetProjectSite(ICSharpProjectRoot project)
{
// Get the host back for the project
var projectSiteGuid = typeof(ICSharpProjectSite).GUID;
var projectSite = project.GetProjectSite(ref projectSiteGuid) as CSharpProjectShim;
// We should have gotten a ProjectSite back. If we didn't, that means we're being given
// a project site that we didn't get BindToProject called on first which is a no-no by
// the project system.
if (projectSite == null)
if (!(project.GetProjectSite(ref projectSiteGuid) is CSharpProjectShim projectSite))
{
throw new ArgumentException($"{project} was not properly sited with the language service.", nameof(project));
}
......
......@@ -157,8 +157,7 @@ public static bool IsSemicolonOfEmbeddedStatement(this SyntaxToken token)
return false;
}
var statement = token.Parent as StatementSyntax;
if (statement == null ||
if (!(token.Parent is StatementSyntax statement) ||
statement.GetLastToken() != token)
{
return false;
......@@ -184,8 +183,7 @@ public static bool IsCloseBraceOfEmbeddedBlock(this SyntaxToken token)
return false;
}
var block = token.Parent as BlockSyntax;
if (block == null ||
if (!(token.Parent is BlockSyntax block) ||
block.CloseBraceToken != token)
{
return false;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册