Skip to content

Commit 54fa1a6

Browse files
Simplify code with patterns. (#81876)
2 parents 06dc8f5 + ce5003c commit 54fa1a6

File tree

36 files changed

+56
-76
lines changed

36 files changed

+56
-76
lines changed

src/Analyzers/Core/CodeFixes/AddParameter/AbstractAddParameterCodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ private async Task<Solution> FixAsync(
481481
var isNullLiteral = syntaxFacts.IsNullLiteralExpression(expressionOfArgument);
482482
var isDefaultLiteral = syntaxFacts.IsDefaultLiteralExpression(expressionOfArgument);
483483

484-
if (argumentTypeInfo.Type == null && argumentTypeInfo.ConvertedType == null)
484+
if (argumentTypeInfo is { Type: null, ConvertedType: null })
485485
{
486486
// Didn't know the type of the argument. We shouldn't assume it doesn't
487487
// match a parameter. However, if the user wrote 'null' and it didn't

src/EditorFeatures/CSharp/AutomaticCompletion/AutomaticLineEnderCommandHandler_Helpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ private static (SyntaxNode newRoot, int nextCaretPosition) AddBraceToIfStatement
243243
// $$
244244
// }
245245
// Print();
246-
if (ifStatementNode.Else == null && ifStatementNode.Parent is BlockSyntax)
246+
if (ifStatementNode is { Else: null, Parent: BlockSyntax })
247247
{
248248
return ReplaceStatementOwnerAndInsertStatement(
249249
services,

src/EditorFeatures/Core/IntelliSense/AsyncCompletion/CompletionSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private bool ShouldTriggerCompletion(
158158
//The user may be trying to invoke snippets through question-tab.
159159
// We may provide a completion after that.
160160
// Otherwise, tab should not be a completion trigger.
161-
if (trigger.Reason == AsyncCompletionData.CompletionTriggerReason.Insertion && trigger.Character == '\t')
161+
if (trigger is { Reason: AsyncCompletionData.CompletionTriggerReason.Insertion, Character: '\t' })
162162
{
163163
return TryInvokeSnippetCompletion(triggerLocation.Snapshot.TextBuffer, triggerLocation.Position, sourceText, document.Project.Services, completionService.GetRules(options));
164164
}

src/Features/CSharp/Portable/Completion/CompletionProviders/XmlDocCommentCompletionProvider.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ public override bool IsInsertionTrigger(SourceText text, int characterPosition,
111111
addEqualsAndQuotes: !nextToken.IsKind(SyntaxKind.EqualsToken) || nextToken.HasLeadingTrivia);
112112
}
113113

114-
var wasTriggeredAfterSpace = trigger.Kind == CompletionTriggerKind.Insertion && trigger.Character == ' ';
115-
if (wasTriggeredAfterSpace)
114+
if (trigger is { Kind: CompletionTriggerKind.Insertion, Character: ' ' })
116115
{
117116
// Nothing below this point should triggered by a space character
118117
// (only attribute names should be triggered by <SPACE>)
@@ -124,7 +123,7 @@ public override bool IsInsertionTrigger(SourceText text, int characterPosition,
124123
return GetAttributeValueItems(declaredSymbol, elementName, attributeName);
125124
}
126125

127-
if (trigger.Kind == CompletionTriggerKind.Insertion && trigger.Character != '<')
126+
if (trigger is { Kind: CompletionTriggerKind.Insertion, Character: not '<' })
128127
{
129128
// With the use of IsTriggerAfterSpaceOrStartOfWordCharacter, the code below is much
130129
// too aggressive at suggesting tags, so exit early before degrading the experience

src/Features/CSharp/Portable/EditAndContinue/SyntaxComparer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,8 +1082,8 @@ private bool TryComputeWeightedDistance(BlockSyntax leftBlock, BlockSyntax right
10821082
case SyntaxKind.CatchClause:
10831083
var leftCatch = (CatchClauseSyntax)leftBlock.Parent;
10841084
var rightCatch = (CatchClauseSyntax)rightBlock.Parent;
1085-
if (leftCatch.Declaration == null && leftCatch.Filter == null &&
1086-
rightCatch.Declaration == null && rightCatch.Filter == null)
1085+
if (leftCatch is { Declaration: null, Filter: null } &&
1086+
rightCatch is { Declaration: null, Filter: null })
10871087
{
10881088
var leftTry = (TryStatementSyntax)leftCatch.Parent!;
10891089
var rightTry = (TryStatementSyntax)rightCatch.Parent!;

src/Features/CSharp/Portable/EditAndContinue/SyntaxUtilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public static bool HasBackingField(PropertyDeclarationSyntax property)
197197
}
198198

199199
return property.ExpressionBody == null
200-
&& property.AccessorList!.Accessors.Any(e => e.Body == null && e.ExpressionBody == null);
200+
&& property.AccessorList!.Accessors.Any(e => e is { Body: null, ExpressionBody: null });
201201
}
202202

203203
/// <summary>

src/Features/CSharp/Portable/UseNamedArguments/CSharpUseNamedArgumentsCodeRefactoringProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private sealed class AttributeArgumentAnalyzer :
8181
BaseAnalyzer<AttributeArgumentSyntax, AttributeArgumentListSyntax>
8282
{
8383
protected override bool IsPositionalArgument(AttributeArgumentSyntax argument)
84-
=> argument.NameColon == null && argument.NameEquals == null;
84+
=> argument is { NameColon: null, NameEquals: null };
8585

8686
protected override SeparatedSyntaxList<AttributeArgumentSyntax> GetArguments(AttributeArgumentListSyntax argumentList)
8787
=> argumentList.Arguments;

src/Features/Core/Portable/Completion/CompletionService.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,11 @@ internal virtual bool ShouldTriggerCompletion(
146146
return false;
147147

148148
// Enter does not trigger completion.
149-
if (trigger.Kind == CompletionTriggerKind.Insertion && trigger.Character == '\n')
150-
{
149+
if (trigger is { Kind: CompletionTriggerKind.Insertion, Character: '\n' })
151150
return false;
152-
}
153151

154152
if (trigger.Kind == CompletionTriggerKind.Deletion && SupportsTriggerOnDeletion(options))
155-
{
156153
return char.IsLetterOrDigit(trigger.Character) || trigger.Character == '.';
157-
}
158154

159155
var extensionManager = languageServices.SolutionServices.GetRequiredService<IExtensionManager>();
160156

src/Features/Core/Portable/Diagnostics/Service/DiagnosticAnalyzerService_DeprioritizationCandidates.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public async Task<bool> IsAnyDeprioritizedDiagnosticIdInProcessAsync(
9999
if (telemetryInfo == null)
100100
return null;
101101

102-
if (telemetryInfo.SymbolStartActionsCount == 0 && telemetryInfo.SemanticModelActionsCount == 0)
102+
if (telemetryInfo is { SymbolStartActionsCount: 0, SemanticModelActionsCount: 0 })
103103
return null;
104104

105105
return [.. analyzer.SupportedDiagnostics.Select(d => d.Id)];

src/Features/Core/Portable/EditAndContinue/EditSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ private static void ResolveSymbols(
983983
newResolution = edit.Symbol.Resolve(newCompilation, cancellationToken: cancellationToken);
984984
Contract.ThrowIfNull(newResolution.Symbol);
985985
}
986-
else if (edit.Kind == SemanticEditKind.Delete && edit.DeletedSymbolContainer is not null)
986+
else if (edit is { Kind: SemanticEditKind.Delete, DeletedSymbolContainer: not null })
987987
{
988988
// For deletes, we use NewSymbol to reference the containing type of the deleted member
989989
newResolution = edit.DeletedSymbolContainer.Value.Resolve(newCompilation, cancellationToken: cancellationToken);

0 commit comments

Comments
 (0)