Skip to content

Commit

Permalink
Ensure the secondary locations are reported even if the first analyze…
Browse files Browse the repository at this point in the history
…d file is a generated one
  • Loading branch information
costin-zaharia-sonarsource committed Mar 21, 2024
1 parent 31fa2b9 commit 8ad9358
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@
namespace SonarAnalyzer.Rules.CSharp;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class SpecifyRouteAttribute : SonarDiagnosticAnalyzer
public sealed class SpecifyRouteAttribute() : SonarDiagnosticAnalyzer<SyntaxKind>(DiagnosticId)
{
private const string DiagnosticId = "S6934";
private const string MessageFormat = "Specify the RouteAttribute when an HttpMethodAttribute is specified at an action level.";

private static readonly DiagnosticDescriptor Rule = DescriptorFactory.Create(DiagnosticId, MessageFormat);

private static readonly ImmutableArray<KnownType> RouteTemplateAttributes =
ImmutableArray.Create(KnownType.Microsoft_AspNetCore_Mvc_Routing_HttpMethodAttribute, KnownType.Microsoft_AspNetCore_Mvc_RouteAttribute);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
protected override string MessageFormat => "Specify the RouteAttribute when an HttpMethodAttribute is specified at an action level.";
protected override ILanguageFacade<SyntaxKind> Language { get; } = CSharpFacade.Instance;

protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterCompilationStartAction(compilationStart =>
Expand Down Expand Up @@ -66,7 +64,7 @@ protected override void Initialize(SonarAnalysisContext context) =>
}, SymbolKind.NamedType);
});

private static void ReportIssues(SonarSymbolReportingContext context, ISymbol symbol, ConcurrentStack<Location> secondaryLocations)
private void ReportIssues(SonarSymbolReportingContext context, ISymbol symbol, ConcurrentStack<Location> secondaryLocations)
{
if (!secondaryLocations.IsEmpty)
{
Expand All @@ -77,7 +75,11 @@ private static void ReportIssues(SonarSymbolReportingContext context, ISymbol sy
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.
secondaryLocations.Clear();
// 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();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,24 @@ public partial class HomeController : Controller // Noncompliant
public void SpecifyRouteAttribute_PartialClasses_OneGenerated_CS() =>
builder
.AddSnippet("""
// <auto-generated/>
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;

public partial class HomeController : Controller // Noncompliant
public partial class HomeController : Controller
{
[HttpGet("Test")]
public IActionResult Index() => View(); // Secondary
public IActionResult ActionInGeneratedCode() => View(); // Secondary
}
""")
.AddSnippet("""
// <auto-generated/>
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;

public partial class HomeController : Controller
public partial class HomeController : Controller // Noncompliant
{
[HttpGet("Test")]
public IActionResult Index() => View(); // Secondary
}
""")
.Verify();
Expand Down

0 comments on commit 8ad9358

Please sign in to comment.