Skip to content

Commit 5792346

Browse files
committed
xunit/xunit#3439: xUnit1051 diagnostic does not take into account [Obsolete]
1 parent 69fee65 commit 5792346

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/xunit.analyzers.tests/Analyzers/X1000/UseCancellationTokenTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,28 @@ public async ValueTask TestMethod() {
188188

189189
await Verify.VerifyAnalyzerV3(LanguageVersion.CSharp7, source);
190190
}
191+
192+
[Fact]
193+
public async Task WhenOverloadIsObsolete_DoesNotTrigger()
194+
{
195+
var source = /* lang=c#-test */ """
196+
using System;
197+
using System.Threading;
198+
using System.Threading.Tasks;
199+
using Xunit;
200+
201+
class TestClass {
202+
[Fact]
203+
public void TestMethod() {
204+
FunctionWithOverload(42);
205+
}
206+
207+
void FunctionWithOverload(int _) {{ }}
208+
[Obsolete]
209+
void FunctionWithOverload(int _1, CancellationToken _2) {{ }}
210+
}
211+
""";
212+
213+
await Verify.VerifyAnalyzerV3(source);
214+
}
191215
}

src/xunit.analyzers/X1000/UseCancellationToken.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public override void AnalyzeCompilation(
3333
TypeSymbolFactory.Record(context.Compilation),
3434
}.WhereNotNull().ToImmutableHashSet(SymbolEqualityComparer.Default);
3535

36+
var attributeUsageAttribute = TypeSymbolFactory.AttributeUsageAttribute(context.Compilation);
37+
var obsoleteAttribute = TypeSymbolFactory.ObsoleteAttribute(context.Compilation);
38+
3639
context.RegisterOperationAction(context =>
3740
{
3841
if (context.Operation is not IInvocationOperation invocationOperation)
@@ -82,6 +85,13 @@ public override void AnalyzeCompilation(
8285
foreach (var member in invokedMethod.ContainingType.GetMembers(invokedMethod.Name))
8386
if (member is IMethodSymbol method)
8487
{
88+
if (attributeUsageAttribute is not null && obsoleteAttribute is not null)
89+
{
90+
var attributes = method.GetAttributesWithInheritance(attributeUsageAttribute);
91+
if (attributes.Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, obsoleteAttribute)))
92+
continue;
93+
}
94+
8595
var methodParameterTypes = method.Parameters.Select(p => p.Type).ToArray();
8696
if (methodParameterTypes.Length != targetParameterTypes.Length)
8797
continue;

0 commit comments

Comments
 (0)