From f63985e6e512b9746b10d466a6d7a92924216a66 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Fri, 28 Jun 2019 14:37:27 -0700 Subject: [PATCH] Use pattern matching --- .../Portable/InternalUtilities/EnumerableExtensions.cs | 9 +++------ .../CSharp/Impl/Debugging/DataTipInfoGetter.cs | 3 +-- .../CSharpProjectShim.ICSharpVenusProjectSite.cs | 3 +-- .../CSharp/Portable/Formatting/FormattingHelpers.cs | 6 ++---- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/Compilers/Core/Portable/InternalUtilities/EnumerableExtensions.cs b/src/Compilers/Core/Portable/InternalUtilities/EnumerableExtensions.cs index 81037fe886c..8b1db335973 100644 --- a/src/Compilers/Core/Portable/InternalUtilities/EnumerableExtensions.cs +++ b/src/Compilers/Core/Portable/InternalUtilities/EnumerableExtensions.cs @@ -26,8 +26,7 @@ public static IEnumerable Do(this IEnumerable source, Action action) } // perf optimization. try to not use enumerator if possible - var list = source as IList; - if (list != null) + if (source is IList list) { for (int i = 0, count = list.Count; i < count; i++) { @@ -168,14 +167,12 @@ public static bool IsSingle(this IEnumerable list) public static bool IsEmpty(this IEnumerable source) { - var readOnlyCollection = source as IReadOnlyCollection; - if (readOnlyCollection != null) + if (source is IReadOnlyCollection readOnlyCollection) { return readOnlyCollection.Count == 0; } - var genericCollection = source as ICollection; - if (genericCollection != null) + if (source is ICollection genericCollection) { return genericCollection.Count == 0; } diff --git a/src/VisualStudio/CSharp/Impl/Debugging/DataTipInfoGetter.cs b/src/VisualStudio/CSharp/Impl/Debugging/DataTipInfoGetter.cs index 3f6ccc8aff0..9eb8eaed234 100644 --- a/src/VisualStudio/CSharp/Impl/Debugging/DataTipInfoGetter.cs +++ b/src/VisualStudio/CSharp/Impl/Debugging/DataTipInfoGetter.cs @@ -28,8 +28,7 @@ internal static async Task 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) diff --git a/src/VisualStudio/CSharp/Impl/ProjectSystemShim/CSharpProjectShim.ICSharpVenusProjectSite.cs b/src/VisualStudio/CSharp/Impl/ProjectSystemShim/CSharpProjectShim.ICSharpVenusProjectSite.cs index 772bea04b11..8288fe89dc7 100644 --- a/src/VisualStudio/CSharp/Impl/ProjectSystemShim/CSharpProjectShim.ICSharpVenusProjectSite.cs +++ b/src/VisualStudio/CSharp/Impl/ProjectSystemShim/CSharpProjectShim.ICSharpVenusProjectSite.cs @@ -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)); } diff --git a/src/Workspaces/CSharp/Portable/Formatting/FormattingHelpers.cs b/src/Workspaces/CSharp/Portable/Formatting/FormattingHelpers.cs index 4e9ac9d8ace..b959c025be1 100644 --- a/src/Workspaces/CSharp/Portable/Formatting/FormattingHelpers.cs +++ b/src/Workspaces/CSharp/Portable/Formatting/FormattingHelpers.cs @@ -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; -- GitLab