Skip to content

Commit db38854

Browse files
committed
Simplify.
1 parent 5ae611b commit db38854

2 files changed

Lines changed: 15 additions & 33 deletions

File tree

IDisposableAnalyzers/Analyzers/FieldAndPropertyDeclarationAnalyzer.cs

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,11 @@ context.Node is FieldDeclarationSyntax declaration &&
3737

3838
private static void HandleProperty(SyntaxNodeAnalysisContext context)
3939
{
40-
if (context.IsExcludedFromAnalysis())
41-
{
42-
return;
43-
}
44-
45-
var property = (IPropertySymbol)context.ContainingSymbol;
46-
if (property.IsStatic ||
47-
property.IsIndexer)
48-
{
49-
return;
50-
}
51-
52-
var declaration = (PropertyDeclarationSyntax)context.Node;
53-
if (declaration.ExpressionBody != null)
54-
{
55-
return;
56-
}
57-
58-
if (declaration.TryGetSetter(out var setter) &&
59-
setter.Body != null)
60-
{
61-
// Handle the backing field
62-
return;
63-
}
64-
65-
if (Disposable.IsPotentiallyAssignableFrom(property.Type, context.Compilation))
40+
if (!context.IsExcludedFromAnalysis() &&
41+
context.ContainingSymbol is IPropertySymbol { IsStatic: false, IsIndexer: false } property &&
42+
context.Node is PropertyDeclarationSyntax { AccessorList: { Accessors: { } accessors } } declaration &&
43+
accessors.First() is { Body: null, ExpressionBody: null } &&
44+
Disposable.IsPotentiallyAssignableFrom(property.Type, context.Compilation))
6645
{
6746
HandleFieldOrProperty(context, new FieldOrPropertyAndDeclaration(property, declaration));
6847
}

IDisposableAnalyzers/Analyzers/ReturnValueAnalyzer.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ private static void HandleLambda(SyntaxNodeAnalysisContext context)
5151
{
5252
if (!context.IsExcludedFromAnalysis() &&
5353
!IsIgnored(context.ContainingSymbol) &&
54-
context.Node is LambdaExpressionSyntax lambda &&
55-
lambda.Body is ExpressionSyntax expression)
54+
context.Node is LambdaExpressionSyntax { Body: ExpressionSyntax expression })
5655
{
5756
HandleReturnValue(context, expression);
5857
}
@@ -138,9 +137,9 @@ private static bool ShouldAwait(SyntaxNodeAnalysisContext context, ExpressionSyn
138137
return returnValue switch
139138
{
140139
InvocationExpressionSyntax invocation
141-
=> !invocation.IsSymbol(KnownSymbol.Task.FromResult, context.SemanticModel, context.CancellationToken),
140+
=> !invocation.IsSymbol(KnownSymbol.Task.FromResult, context.SemanticModel, context.CancellationToken),
142141
MemberAccessExpressionSyntax { Name: { Identifier: { ValueText: "CompletedTask" } } } memberAccess
143-
=> !memberAccess.IsSymbol(KnownSymbol.Task.CompletedTask, context.SemanticModel, context.CancellationToken),
142+
=> !memberAccess.IsSymbol(KnownSymbol.Task.CompletedTask, context.SemanticModel, context.CancellationToken),
144143
_ => true,
145144
};
146145
}
@@ -236,9 +235,13 @@ private static bool IsIgnored(ISymbol symbol)
236235
return method?.ReturnType;
237236
}
238237

239-
return (context.ContainingSymbol as IMethodSymbol)?.ReturnType ??
240-
(context.ContainingSymbol as IFieldSymbol)?.Type ??
241-
(context.ContainingSymbol as IPropertySymbol)?.Type;
238+
return context switch
239+
{
240+
{ ContainingSymbol: IFieldSymbol field } => field.Type,
241+
{ ContainingSymbol: IPropertySymbol property } => property.Type,
242+
{ ContainingSymbol: IMethodSymbol method } => method.ReturnType,
243+
_ => null,
244+
};
242245
}
243246
}
244247
}

0 commit comments

Comments
 (0)