Skip to content

Commit

Permalink
Move lambda opt-out logic to IsInTestMethod extension method since it…
Browse files Browse the repository at this point in the history
…'s always desired
  • Loading branch information
bradwilson committed Dec 22, 2024
1 parent 96de16a commit b55799f
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/xunit.analyzers/Utility/CodeAnalysisExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public static bool IsInTestMethod(

for (var parent = operation.Parent; parent is not null; parent = parent.Parent)
{
if (parent is IAnonymousFunctionOperation or ILocalFunctionOperation)
return false;
if (parent is not IMethodBodyOperation methodBodyOperation)
continue;
if (methodBodyOperation.Syntax is not MethodDeclarationSyntax methodSyntax)
Expand Down
5 changes: 0 additions & 5 deletions src/xunit.analyzers/X1000/DoNotUseBlockingTaskOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,6 @@ invocation.Arguments[0].Value is IArrayCreationOperation arrayCreationOperation
if (!foundSymbol)
return;

// Ignore anything inside a lambda expression or a local function
for (var current = context.Operation; current is not null; current = current.Parent)
if (current is IAnonymousFunctionOperation or ILocalFunctionOperation)
return;

if (foundSymbolName == nameof(Task<int>.Result) &&
reference.Instance is ILocalReferenceOperation localReferenceOperation &&
TaskIsKnownToBeCompleted(reference, [localReferenceOperation.Local], taskType, xunitContext))
Expand Down
5 changes: 0 additions & 5 deletions src/xunit.analyzers/X1000/DoNotUseConfigureAwait.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ public override void AnalyzeCompilation(
if (!invocation.IsInTestMethod(xunitContext))
return;

// Ignore anything inside a lambda expression or a local function
for (var current = context.Operation; current is not null; current = current.Parent)
if (current is IAnonymousFunctionOperation or ILocalFunctionOperation)
return;

// invocation should be two nodes: "(some other code).ConfigureAwait" and the arguments (like "(false)")
var invocationChildren = invocation.Syntax.ChildNodes().ToList();
if (invocationChildren.Count != 2)
Expand Down
5 changes: 0 additions & 5 deletions src/xunit.analyzers/X1000/UseCancellationToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ public override void AnalyzeCompilation(
if (!invocationOperation.IsInTestMethod(xunitContext))
return;

// Ignore anything inside a lambda expression or a local function
for (var current = context.Operation; current is not null; current = current.Parent)
if (current is IAnonymousFunctionOperation or ILocalFunctionOperation)
return;

var invokedMethod = invocationOperation.TargetMethod;
var parameters = invokedMethod.Parameters;

Expand Down

0 comments on commit b55799f

Please sign in to comment.