From bcc7b3d7c4e729d536bb0ffde7df586282d43e79 Mon Sep 17 00:00:00 2001 From: jsrustad Date: Tue, 28 Oct 2025 19:59:18 -0500 Subject: [PATCH] Fixed 9 instance of code causing IDE0038 Refactor: Use pattern matching for type checks Refactored code to utilize C# pattern matching with the `is` keyword for type checking and casting, eliminating explicit casts. - Updated `ElementOrderCodeFixProvider.cs` to use pattern matching for `TypeDeclarationSyntax` and `CompilationUnitSyntax`. - Refactored `Equals` method in `UsingCodeFixProvider.TreeTextSpan.cs` to use pattern matching. - Applied pattern matching in `DeclarationModifiersHelper.cs` for various syntax types, improving readability and maintainability. --- .../ElementOrderCodeFixProvider.cs | 8 +++---- .../UsingCodeFixProvider.TreeTextSpan.cs | 2 +- .../Helpers/DeclarationModifiersHelper.cs | 24 +++++++++---------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/ElementOrderCodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/ElementOrderCodeFixProvider.cs index 0339c3a69..a97657a12 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/ElementOrderCodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/ElementOrderCodeFixProvider.cs @@ -80,9 +80,9 @@ private static SyntaxNode UpdateSyntaxRoot(MemberDeclarationSyntax memberDeclara var parentDeclaration = memberDeclaration.Parent; var memberToMove = new MemberOrderHelper(memberDeclaration, elementOrder); - if (parentDeclaration is TypeDeclarationSyntax) + if (parentDeclaration is TypeDeclarationSyntax syntax) { - return HandleTypeDeclaration(memberToMove, (TypeDeclarationSyntax)parentDeclaration, elementOrder, syntaxRoot, indentationSettings); + return HandleTypeDeclaration(memberToMove, syntax, elementOrder, syntaxRoot, indentationSettings); } if (BaseNamespaceDeclarationSyntaxWrapper.IsInstance(parentDeclaration)) @@ -90,9 +90,9 @@ private static SyntaxNode UpdateSyntaxRoot(MemberDeclarationSyntax memberDeclara return HandleBaseNamespaceDeclaration(memberToMove, (BaseNamespaceDeclarationSyntaxWrapper)parentDeclaration, elementOrder, syntaxRoot, indentationSettings); } - if (parentDeclaration is CompilationUnitSyntax) + if (parentDeclaration is CompilationUnitSyntax syntax1) { - return HandleCompilationUnitDeclaration(memberToMove, (CompilationUnitSyntax)parentDeclaration, elementOrder, syntaxRoot, indentationSettings); + return HandleCompilationUnitDeclaration(memberToMove, syntax1, elementOrder, syntaxRoot, indentationSettings); } return syntaxRoot; diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.TreeTextSpan.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.TreeTextSpan.cs index 8abd78c18..0288d1b72 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.TreeTextSpan.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.TreeTextSpan.cs @@ -85,7 +85,7 @@ public bool Equals(TreeTextSpan other) /// public override bool Equals(object obj) { - return (obj is TreeTextSpan) && this.Equals((TreeTextSpan)obj); + return (obj is TreeTextSpan span) && this.Equals(span); } /// diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Helpers/DeclarationModifiersHelper.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Helpers/DeclarationModifiersHelper.cs index cdda0397d..14de8b6b9 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Helpers/DeclarationModifiersHelper.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Helpers/DeclarationModifiersHelper.cs @@ -99,29 +99,29 @@ internal static SyntaxTokenList AddModifiers(SyntaxTokenList modifiers, ref Synt internal static SyntaxTokenList GetModifiers(this MemberDeclarationSyntax syntax) { - if (syntax is BaseMethodDeclarationSyntax) + if (syntax is BaseMethodDeclarationSyntax syntax1) { - return ((BaseMethodDeclarationSyntax)syntax).Modifiers; + return syntax1.Modifiers; } - else if (syntax is BasePropertyDeclarationSyntax) + else if (syntax is BasePropertyDeclarationSyntax syntax2) { - return ((BasePropertyDeclarationSyntax)syntax).Modifiers; + return syntax2.Modifiers; } - else if (syntax is BaseTypeDeclarationSyntax) + else if (syntax is BaseTypeDeclarationSyntax syntax3) { - return ((BaseTypeDeclarationSyntax)syntax).Modifiers; + return syntax3.Modifiers; } - else if (syntax is BaseFieldDeclarationSyntax) + else if (syntax is BaseFieldDeclarationSyntax syntax4) { - return ((BaseFieldDeclarationSyntax)syntax).Modifiers; + return syntax4.Modifiers; } - else if (syntax is DelegateDeclarationSyntax) + else if (syntax is DelegateDeclarationSyntax syntax5) { - return ((DelegateDeclarationSyntax)syntax).Modifiers; + return syntax5.Modifiers; } - else if (syntax is IncompleteMemberSyntax) + else if (syntax is IncompleteMemberSyntax syntax6) { - return ((IncompleteMemberSyntax)syntax).Modifiers; + return syntax6.Modifiers; } return default;