Skip to content

Commit 5f38076

Browse files
committed
Use pattern matching
1 parent cbf276b commit 5f38076

58 files changed

Lines changed: 120 additions & 262 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1609SA1610CodeFixProvider.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ private async Task<Document> GetTransformedDocumentAsync(Document document, Diag
101101
return document;
102102
}
103103

104-
XmlElementSyntax summaryElement = documentationComment.Content.GetFirstXmlElement(XmlCommentHelper.SummaryXmlTag) as XmlElementSyntax;
105-
if (summaryElement == null)
104+
if (!(documentationComment.Content.GetFirstXmlElement(XmlCommentHelper.SummaryXmlTag) is XmlElementSyntax summaryElement))
106105
{
107106
return document;
108107
}
@@ -163,8 +162,7 @@ private async Task<Document> GetTransformedDocumentAsync(Document document, Diag
163162
private bool TryRemoveSummaryPrefix(ref SyntaxList<XmlNodeSyntax> summaryContent, string prefix)
164163
{
165164
XmlNodeSyntax firstContent = summaryContent.FirstOrDefault(IsContentElement);
166-
XmlTextSyntax firstText = firstContent as XmlTextSyntax;
167-
if (firstText == null)
165+
if (!(firstContent is XmlTextSyntax firstText))
168166
{
169167
return false;
170168
}

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1615SA1616CodeFixProvider.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
125125

126126
if (returnsElement != null)
127127
{
128-
XmlEmptyElementSyntax emptyElement = returnsElement as XmlEmptyElementSyntax;
129-
if (emptyElement != null)
128+
if (returnsElement is XmlEmptyElementSyntax emptyElement)
130129
{
131130
XmlElementSyntax updatedReturns = XmlSyntaxFactory.Element(XmlCommentHelper.ReturnsXmlTag, content)
132131
.WithLeadingTrivia(returnsElement.GetLeadingTrivia())
@@ -179,8 +178,7 @@ private static bool IsAsynchronousTestMethod(SemanticModel semanticModel, Method
179178

180179
foreach (AttributeSyntax attribute in attributeList.Attributes)
181180
{
182-
IMethodSymbol methodSymbol = semanticModel.GetSymbolInfo(attribute.Name, cancellationToken).Symbol as IMethodSymbol;
183-
if (methodSymbol == null)
181+
if (!(semanticModel.GetSymbolInfo(attribute.Name, cancellationToken).Symbol is IMethodSymbol methodSymbol))
184182
{
185183
continue;
186184
}

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1617CodeFixProvider.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
8383
List<SyntaxNode> nodesToFix = new List<SyntaxNode>();
8484
nodesToFix.Add(returnsElement);
8585

86-
var previousAsTextSyntax = previous as XmlTextSyntax;
87-
if (previousAsTextSyntax != null && XmlCommentHelper.IsConsideredEmpty(previousAsTextSyntax))
86+
if (previous is XmlTextSyntax previousAsTextSyntax && XmlCommentHelper.IsConsideredEmpty(previousAsTextSyntax))
8887
{
8988
nodesToFix.Add(previous);
9089
}

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1642SA1643CodeFixProvider.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
5656
}
5757

5858
var node = root.FindNode(diagnostic.Location.SourceSpan, findInsideTrivia: true, getInnermostNodeForTie: true);
59-
var xmlElementSyntax = node as XmlElementSyntax;
6059

61-
if (xmlElementSyntax != null)
60+
if (node is XmlElementSyntax xmlElementSyntax)
6261
{
6362
context.RegisterCodeFix(
6463
CodeAction.Create(
@@ -121,8 +120,7 @@ internal static ImmutableArray<string> GenerateStandardText(Document document, B
121120
internal static SyntaxList<XmlNodeSyntax> BuildStandardTextSyntaxList(BaseTypeDeclarationSyntax typeDeclaration, string newLineText, string preText, string postText)
122121
{
123122
TypeParameterListSyntax typeParameterList;
124-
ClassDeclarationSyntax classDeclaration = typeDeclaration as ClassDeclarationSyntax;
125-
if (classDeclaration != null)
123+
if (typeDeclaration is ClassDeclarationSyntax classDeclaration)
126124
{
127125
typeParameterList = classDeclaration.TypeParameterList;
128126
}
@@ -172,8 +170,7 @@ private static Task<Document> GetTransformedDocumentAsync(Document document, Syn
172170
bool isStruct = typeDeclaration.IsKind(SyntaxKind.StructDeclaration);
173171

174172
TypeParameterListSyntax typeParameterList;
175-
ClassDeclarationSyntax classDeclaration = typeDeclaration as ClassDeclarationSyntax;
176-
if (classDeclaration != null)
173+
if (typeDeclaration is ClassDeclarationSyntax classDeclaration)
177174
{
178175
typeParameterList = classDeclaration.TypeParameterList;
179176
}
@@ -294,8 +291,7 @@ private static TypeArgumentListSyntax ParameterToArgumentListSyntax(TypeParamete
294291

295292
private static SyntaxList<XmlNodeSyntax> RemoveTrailingEmptyLines(SyntaxList<XmlNodeSyntax> content)
296293
{
297-
var xmlText = content[content.Count - 1] as XmlTextSyntax;
298-
if (xmlText == null)
294+
if (!(content[content.Count - 1] is XmlTextSyntax xmlText))
299295
{
300296
return content;
301297
}

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1651CodeFixProvider.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
5757
continue;
5858
}
5959

60-
XmlElementSyntax xmlElementSyntax = syntax as XmlElementSyntax;
61-
if (xmlElementSyntax == null)
60+
if (!(syntax is XmlElementSyntax xmlElementSyntax))
6261
{
6362
// We continue even for placeholders if they are empty elements (XmlEmptyElementSyntax)
6463
continue;
@@ -121,9 +120,7 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
121120

122121
foreach (var diagnostic in diagnostics)
123122
{
124-
var xmlElement = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, findInsideTrivia: true, getInnermostNodeForTie: true) as XmlElementSyntax;
125-
126-
if ((xmlElement != null)
123+
if ((syntaxRoot.FindNode(diagnostic.Location.SourceSpan, findInsideTrivia: true, getInnermostNodeForTie: true) is XmlElementSyntax xmlElement)
127124
&& (xmlElement.Content.Count > 0)
128125
&& !string.IsNullOrWhiteSpace(xmlElement.Content.ToString()))
129126
{

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/CustomBatchFixAllProvider.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,7 @@ public virtual async Task<Solution> TryMergeFixesAsync(Solution oldSolution, IEn
249249
ApplyChangesOperation singleApplyChangesOperation = null;
250250
foreach (var operation in operations)
251251
{
252-
ApplyChangesOperation applyChangesOperation = operation as ApplyChangesOperation;
253-
if (applyChangesOperation == null)
252+
if (!(operation is ApplyChangesOperation applyChangesOperation))
254253
{
255254
continue;
256255
}

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/FormattingHelper.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ public static SyntaxTrivia WithoutFormatting(this SyntaxTrivia trivia)
8686
// GetStructure() returns SyntaxNode instead of StructuredTriviaSyntax. For C# code, this should always
8787
// be an actual instance of StructuredTriviaSyntax, but we handle the case where it is not by leaving
8888
// the structure node unaltered rather than throwing some sort of exception.
89-
StructuredTriviaSyntax structure = trivia.GetStructure() as StructuredTriviaSyntax;
90-
if (structure != null)
89+
if (trivia.GetStructure() is StructuredTriviaSyntax structure)
9190
{
9291
result = SyntaxFactory.Trivia(structure.WithoutFormatting());
9392
}

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/RenameHelper.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public static async Task<bool> IsValidNewMemberNameAsync(SemanticModel semanticM
5656
if (symbol.Kind == SymbolKind.TypeParameter)
5757
{
5858
// If the symbol is a type parameter, the name can't be the same as any type parameters of the containing type.
59-
var parentSymbol = containingSymbol?.ContainingSymbol as INamedTypeSymbol;
60-
if (parentSymbol != null
59+
if (containingSymbol?.ContainingSymbol is INamedTypeSymbol parentSymbol
6160
&& parentSymbol.TypeParameters.Any(t => t.Name == name))
6261
{
6362
return false;
@@ -67,8 +66,7 @@ public static async Task<bool> IsValidNewMemberNameAsync(SemanticModel semanticM
6766
containingSymbol = containingSymbol?.ContainingSymbol;
6867
}
6968

70-
var containingNamespaceOrTypeSymbol = containingSymbol as INamespaceOrTypeSymbol;
71-
if (containingNamespaceOrTypeSymbol != null)
69+
if (containingSymbol is INamespaceOrTypeSymbol containingNamespaceOrTypeSymbol)
7270
{
7371
if (containingNamespaceOrTypeSymbol.Kind == SymbolKind.Namespace)
7472
{

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/TaskHelper.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public static bool IsTaskReturningMethod(SemanticModel semanticModel, DelegateDe
2424
public static bool IsTaskType(SemanticModel semanticModel, TypeSyntax typeSyntax, CancellationToken cancellationToken)
2525
{
2626
SymbolInfo symbolInfo = semanticModel.GetSymbolInfo(typeSyntax, cancellationToken);
27-
INamedTypeSymbol namedTypeSymbol = symbolInfo.Symbol as INamedTypeSymbol;
28-
if (namedTypeSymbol == null)
27+
if (!(symbolInfo.Symbol is INamedTypeSymbol namedTypeSymbol))
2928
{
3029
return false;
3130
}

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1501CodeFixProvider.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
5959
{
6060
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
6161
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
62-
var statement = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true) as StatementSyntax;
63-
if (statement == null)
62+
if (!(syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true) is StatementSyntax statement))
6463
{
6564
return document;
6665
}
@@ -307,8 +306,7 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
307306

308307
foreach (var diagnostic in diagnostics.Sort(DiagnosticComparer.Instance))
309308
{
310-
var statement = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true) as StatementSyntax;
311-
if (statement == null)
309+
if (!(syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true) is StatementSyntax statement))
312310
{
313311
continue;
314312
}

0 commit comments

Comments
 (0)