Skip to content

Commit adc4a56

Browse files
apply comments
1 parent a025665 commit adc4a56

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

analyzers/src/SonarAnalyzer.CSharp/Rules/InfiniteRecursion.RoslynCfg.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,12 @@ private sealed class RoslynChecker : IChecker
3030
public void CheckForNoExitProperty(SonarSyntaxNodeReportingContext c, PropertyDeclarationSyntax property, IPropertySymbol propertySymbol) =>
3131
CheckForNoExit(c,
3232
propertySymbol,
33-
property.ExpressionBody,
34-
property.AccessorList,
35-
property.Identifier.GetLocation(),
3633
"property's recursion",
3734
"property accessor's recursion");
3835

3936
public void CheckForNoExitIndexer(SonarSyntaxNodeReportingContext c, IndexerDeclarationSyntax indexer, IPropertySymbol propertySymbol) =>
4037
CheckForNoExit(c,
4138
propertySymbol,
42-
indexer.ExpressionBody,
43-
indexer.AccessorList,
44-
indexer.ThisKeyword.GetLocation(),
4539
"indexer's recursion",
4640
"indexer accessor's recursion");
4741

@@ -71,12 +65,26 @@ public void CheckForNoExitMethod(SonarSyntaxNodeReportingContext c, SyntaxNode b
7165

7266
private static void CheckForNoExit(SonarSyntaxNodeReportingContext c,
7367
IPropertySymbol propertySymbol,
74-
ArrowExpressionClauseSyntax expressionBody,
75-
AccessorListSyntax accessorList,
76-
Location location,
7768
string arrowExpressionMessageArg,
7869
string accessorMessageArg)
7970
{
71+
ArrowExpressionClauseSyntax expressionBody = null;
72+
AccessorListSyntax accessorList = null;
73+
Location location = null;
74+
75+
if (c.Node is PropertyDeclarationSyntax propertyDeclaration)
76+
{
77+
expressionBody = propertyDeclaration.ExpressionBody;
78+
accessorList = propertyDeclaration.AccessorList;
79+
location = propertyDeclaration.Identifier.GetLocation();
80+
}
81+
else if (c.Node is IndexerDeclarationSyntax indexerDeclaration)
82+
{
83+
expressionBody = indexerDeclaration.ExpressionBody;
84+
accessorList = indexerDeclaration.AccessorList;
85+
location = indexerDeclaration.ThisKeyword.GetLocation();
86+
}
87+
8088
if (expressionBody?.Expression is not null)
8189
{
8290
var cfg = ControlFlowGraph.Create(expressionBody, c.SemanticModel, c.Cancel);

analyzers/src/SonarAnalyzer.CSharp/Rules/InfiniteRecursion.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,23 @@ protected override void Initialize(SonarAnalysisContext context)
7474
c =>
7575
{
7676
var property = (PropertyDeclarationSyntax)c.Node;
77-
if (c.SemanticModel.GetDeclaredSymbol(property) is { } propertySymbol)
78-
{
79-
checker.CheckForNoExitProperty(c, property, propertySymbol);
80-
}
77+
checker.CheckForNoExitProperty(c, property, c.SemanticModel.GetDeclaredSymbol(property));
8178
},
8279
SyntaxKind.PropertyDeclaration);
8380

8481
context.RegisterNodeAction(
8582
c =>
8683
{
8784
var indexer = (IndexerDeclarationSyntax)c.Node;
88-
if (c.SemanticModel.GetDeclaredSymbol(indexer) is { } indexerSymbol)
89-
{
90-
checker.CheckForNoExitIndexer(c, indexer, indexerSymbol);
91-
}
85+
checker.CheckForNoExitIndexer(c, indexer, c.SemanticModel.GetDeclaredSymbol(indexer));
9286
},
9387
SyntaxKind.IndexerDeclaration);
9488

9589
context.RegisterNodeAction(
9690
c =>
9791
{
9892
var eventDeclaration = (EventDeclarationSyntax)c.Node;
99-
if (c.SemanticModel.GetDeclaredSymbol(eventDeclaration) is { } eventSymbol)
100-
{
101-
checker.CheckForNoExitEvent(c, eventDeclaration, eventSymbol);
102-
}
93+
checker.CheckForNoExitEvent(c, eventDeclaration, c.SemanticModel.GetDeclaredSymbol(eventDeclaration));
10394
},
10495
SyntaxKind.EventDeclaration);
10596
}

0 commit comments

Comments
 (0)