From 62a23254f6e5693ef45a07cc221c994d87ba1710 Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Tue, 2 Jan 2024 20:27:22 +0100 Subject: [PATCH] xUnit1030 should not trigger inside lambda (#177) Co-authored-by: Brad Wilson --- .../X1000/DoNotUseConfigureAwaitTests.cs | 46 ++++++++++++++++++- .../X1000/DoNotUseConfigureAwait.cs | 5 ++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/xunit.analyzers.tests/Analyzers/X1000/DoNotUseConfigureAwaitTests.cs b/src/xunit.analyzers.tests/Analyzers/X1000/DoNotUseConfigureAwaitTests.cs index 6bbe066f..162ff34a 100644 --- a/src/xunit.analyzers.tests/Analyzers/X1000/DoNotUseConfigureAwaitTests.cs +++ b/src/xunit.analyzers.tests/Analyzers/X1000/DoNotUseConfigureAwaitTests.cs @@ -63,6 +63,28 @@ public async Task TestMethod() { "booleanVar", // Reference value (we don't do lookup) }; + [Theory] + [MemberData(nameof(InvalidValues))] + public async void InvalidValue_InsideLambda_DoesNotTrigger(string argumentValue) + { + var source = @$" +using System.Threading.Tasks; +using Xunit; + +public class TestClass {{ + [Fact] + public async Task TestMethod() {{ + var booleanVar = true; + var t = Task.Run(async () => {{ + await Task.Delay(1).ConfigureAwait({argumentValue}); + }}); + await t; + }} +}}"; + + await Verify.VerifyAnalyzer(source); + } + [Theory] [MemberData(nameof(InvalidValues))] public async void InvalidValue_TaskWithAwait_Triggers(string argumentValue) @@ -239,7 +261,29 @@ public async Task TestMethod() {{ [Theory] [MemberData(nameof(InvalidValues))] - public async void InvalidValue_TaskWithAwait_DoesNotTrigger(string enumValue) + public async void InvalidValue_InsideLambda_DoesNotTrigger(string argumentValue) + { + var source = @$" +using System.Threading.Tasks; +using Xunit; + +public class TestClass {{ + [Fact] + public async Task TestMethod() {{ + var enumVar = ConfigureAwaitOptions.ContinueOnCapturedContext; + var t = Task.Run(async () => {{ + await Task.Delay(1).ConfigureAwait({argumentValue}); + }}); + await t; + }} +}}"; + + await Verify.VerifyAnalyzer(source); + } + + [Theory] + [MemberData(nameof(InvalidValues))] + public async void InvalidValue_TaskWithAwait_Triggers(string enumValue) { var source = $@" using System.Threading.Tasks; diff --git a/src/xunit.analyzers/X1000/DoNotUseConfigureAwait.cs b/src/xunit.analyzers/X1000/DoNotUseConfigureAwait.cs index b19c2f1d..817a187c 100644 --- a/src/xunit.analyzers/X1000/DoNotUseConfigureAwait.cs +++ b/src/xunit.analyzers/X1000/DoNotUseConfigureAwait.cs @@ -63,6 +63,11 @@ public override void AnalyzeCompilation( if (!invocation.IsInTestMethod(xunitContext)) return; + // Ignore anything inside a lambda expression + for (var current = context.Operation; current is not null; current = current.Parent) + if (current is IAnonymousFunctionOperation) + 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)