Skip to content

Commit

Permalink
Add exception for CancellationToken parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
zsolt-kolbay-sonarsource committed May 1, 2024
1 parent 6b9097b commit 73a972f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private static void ProcessCodeBlock(SonarCodeBlockStartAnalysisContext<SyntaxKi
{
if (codeBlockContext.CodeBlock is MethodDeclarationSyntax methodDeclaration
&& codeBlockContext.OwningSymbol is IMethodSymbol methodSymbol
&& methodSymbol.Parameters.Length > 0
&& methodSymbol.Parameters.Any(x => !x.Type.Is(KnownType.System_Threading_CancellationToken))
&& methodSymbol.IsControllerActionMethod()
&& !HasActionFilterAttribute(methodSymbol))
{
Expand Down
1 change: 1 addition & 0 deletions analyzers/src/SonarAnalyzer.Common/Helpers/KnownType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ public sealed partial class KnownType
public static readonly KnownType System_Text_RegularExpressions_Regex = new("System.Text.RegularExpressions.Regex");
public static readonly KnownType System_Text_RegularExpressions_RegexOptions = new("System.Text.RegularExpressions.RegexOptions");
public static readonly KnownType System_Text_StringBuilder = new("System.Text.StringBuilder");
public static readonly KnownType System_Threading_CancellationToken = new("System.Threading.CancellationToken");
public static readonly KnownType System_Threading_CancellationTokenSource = new("System.Threading.CancellationTokenSource");
public static readonly KnownType System_Threading_Monitor = new("System.Threading.Monitor");
public static readonly KnownType System_Threading_Mutex = new("System.Threading.Mutex");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

Expand Down Expand Up @@ -90,6 +92,9 @@ public class NonCompliantController : ControllerBase

[HttpGet("/[controller]/list")]
public string[] List() => null; // Compliant

[HttpGet("/[controller]/listasync")]
public Task<string[]> ListAsync(CancellationToken token) => null; // Compliant
}

[ApiController]
Expand Down

0 comments on commit 73a972f

Please sign in to comment.