Skip to content

Commit

Permalink
respect more nullability in EvaluatePropertiesForSimplicityAnalyzer a…
Browse files Browse the repository at this point in the history
…nd EvaluateManagedBackingFieldsAnalayzer (#4511)

and fixes some null warnings
  • Loading branch information
SimonCropp authored Feb 18, 2025
1 parent 33cf514 commit fd0b6b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ private static bool DetermineIfPropertyUsesField(SyntaxNodeAnalysisContext conte
return DetermineIfPropertyUsesField(
context, fieldSymbol, classProperty,
propertyNode => propertyNode.ExpressionBody as SyntaxNode ??
propertyNode.AccessorList.Accessors.Single(
propertyNode.AccessorList?.Accessors.Single(
_ => _.IsKind(SyntaxKind.GetAccessorDeclaration)));
}

if (classProperty.SetMethod != null)
{
return DetermineIfPropertyUsesField(
context, fieldSymbol, classProperty,
propertyNode => propertyNode.AccessorList.Accessors.Single(
propertyNode => propertyNode.AccessorList?.Accessors.Single(
_ => _.IsKind(SyntaxKind.SetAccessorDeclaration)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ private static void AnalyzePropertyGetter(PropertyDeclarationSyntax propertyNode

private static void AnalyzePropertyGetterWithExpressionBody(PropertyDeclarationSyntax propertyNode, SyntaxNodeAnalysisContext context)
{
if (propertyNode.ExpressionBody == null)
{
return;
}

var getterExpression = propertyNode.ExpressionBody.Expression;
var getterChildren = getterExpression.DescendantNodes(_ => true).ToArray();

Expand All @@ -94,7 +99,7 @@ private static void AnalyzePropertyGetterWithExpressionBody(PropertyDeclarationS
getterExpression, context.SemanticModel);

if (getterWalker.Invocation != null &&
getterChildren.Contains(getterWalker.Invocation))
getterChildren.Contains(getterWalker.Invocation))
{
context.ReportDiagnostic(Diagnostic.Create(
onlyUseCslaPropertyMethodsInGetSetRule,
Expand All @@ -105,6 +110,11 @@ private static void AnalyzePropertyGetterWithExpressionBody(PropertyDeclarationS

private static void AnalyzePropertyGetterWithGet(PropertyDeclarationSyntax propertyNode, SyntaxNodeAnalysisContext context)
{
if (propertyNode.AccessorList == null)
{
return;
}

var accessor = propertyNode.AccessorList.Accessors.Single(_ => _.IsKind(SyntaxKind.GetAccessorDeclaration));
var getterBody = accessor.Body;
var getterExpression = accessor.ExpressionBody;
Expand Down Expand Up @@ -134,7 +144,7 @@ private static void AnalyzePropertyGetterWithGet(PropertyDeclarationSyntax prope
{

if (!(returnNode.ChildNodes().SingleOrDefault(
_ => _.IsKind(SyntaxKind.InvocationExpression)) is InvocationExpressionSyntax invocation) || invocation != getterWalkerBody.Invocation)
_ => _.IsKind(SyntaxKind.InvocationExpression)) is InvocationExpressionSyntax invocation) || invocation != getterWalkerBody.Invocation)
{
context.ReportDiagnostic(Diagnostic.Create(
onlyUseCslaPropertyMethodsInGetSetRule,
Expand Down Expand Up @@ -162,6 +172,11 @@ private static void AnalyzePropertyGetterWithGet(PropertyDeclarationSyntax prope

private static void AnalyzePropertySetter(PropertyDeclarationSyntax propertyNode, SyntaxNodeAnalysisContext context)
{
if (propertyNode.AccessorList == null)
{
return;
}

var accessor = propertyNode.AccessorList.Accessors.Single(_ => _.IsKind(SyntaxKind.SetAccessorDeclaration));
var setterBody = accessor.Body;
var setterExpression = accessor.ExpressionBody;
Expand Down Expand Up @@ -191,7 +206,7 @@ private static void AnalyzePropertySetter(PropertyDeclarationSyntax propertyNode
{

if (!(expressionNode.ChildNodes().SingleOrDefault(
_ => _.IsKind(SyntaxKind.InvocationExpression)) is InvocationExpressionSyntax invocation) || invocation != setterWalkerBody.Invocation)
_ => _.IsKind(SyntaxKind.InvocationExpression)) is InvocationExpressionSyntax invocation) || invocation != setterWalkerBody.Invocation)
{
context.ReportDiagnostic(Diagnostic.Create(
onlyUseCslaPropertyMethodsInGetSetRule,
Expand Down

0 comments on commit fd0b6b2

Please sign in to comment.