File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
xunit.analyzers.tests/Analyzers/X1000 Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments