From 7099130a56add4c12fbf5bc8ad57ec3cb80af108 Mon Sep 17 00:00:00 2001 From: Costin Zaharia Date: Thu, 21 Mar 2024 15:08:01 +0100 Subject: [PATCH] Reduce nesting --- .../Rules/SpecifyRouteAttribute.cs | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/analyzers/src/SonarAnalyzer.CSharp/Rules/SpecifyRouteAttribute.cs b/analyzers/src/SonarAnalyzer.CSharp/Rules/SpecifyRouteAttribute.cs index 83beaf161ad..68a787a13ce 100644 --- a/analyzers/src/SonarAnalyzer.CSharp/Rules/SpecifyRouteAttribute.cs +++ b/analyzers/src/SonarAnalyzer.CSharp/Rules/SpecifyRouteAttribute.cs @@ -66,20 +66,22 @@ protected override void Initialize(SonarAnalysisContext context) => private void ReportIssues(SonarSymbolReportingContext context, ISymbol symbol, ConcurrentStack secondaryLocations) { - if (!secondaryLocations.IsEmpty) + if (secondaryLocations.IsEmpty) { - foreach (var declaration in symbol.DeclaringSyntaxReferences.Select(x => x.GetSyntax())) + return; + } + + foreach (var declaration in symbol.DeclaringSyntaxReferences.Select(x => x.GetSyntax())) + { + if (declaration.GetIdentifier() is { } identifier) { - if (declaration.GetIdentifier() is { } identifier) - { - context.ReportIssue(CSharpGeneratedCodeRecognizer.Instance, Diagnostic.Create(Rule, identifier.GetLocation(), secondaryLocations)); + context.ReportIssue(CSharpGeneratedCodeRecognizer.Instance, Diagnostic.Create(Rule, identifier.GetLocation(), secondaryLocations)); - // When a symbol was declared in multiple locations, we want to avoid reporting the same secondary locations multiple times. - // The list is cleared only if the declaration is not in generated code. Otherwise, the secondary locations are not reported at all. - if (!Language.GeneratedCodeRecognizer.IsGenerated(declaration.SyntaxTree)) - { - secondaryLocations.Clear(); - } + // When a symbol was declared in multiple locations, we want to avoid reporting the same secondary locations multiple times. + // The list is cleared only if the declaration is not in generated code. Otherwise, the secondary locations are not reported at all. + if (!Language.GeneratedCodeRecognizer.IsGenerated(declaration.SyntaxTree)) + { + secondaryLocations.Clear(); } } }