Skip to content

Commit 6a94716

Browse files
CopilotYoussef1313
andauthored
Fix MSTEST0057 false positive on static constructors (#6937)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Youssef1313 <[email protected]> Co-authored-by: Youssef Victor <[email protected]>
1 parent eec0de8 commit 6a94716

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

src/Analyzers/MSTest.Analyzers/TestMethodAttributeShouldPropagateSourceInformationAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
6666
return;
6767
}
6868

69-
foreach (IMethodSymbol constructor in namedTypeSymbol.Constructors)
69+
foreach (IMethodSymbol constructor in namedTypeSymbol.InstanceConstructors)
7070
{
7171
// Check if constructor has CallerFilePath and CallerLineNumber parameters
7272
bool hasCallerFilePath = false;

test/UnitTests/MSTest.Analyzers.UnitTests/TestMethodAttributeShouldPropagateSourceInformationAnalyzerTests.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,4 +481,88 @@ public DerivedTestMethodAttribute([CallerFilePath] string callerFilePath = "", [
481481

482482
await VerifyCS.VerifyCodeFixAsync(code, code);
483483
}
484+
485+
[TestMethod]
486+
public async Task WhenDerivedTestMethodAttributeWithStaticConstructor_NoDiagnostic()
487+
{
488+
string code = """
489+
using Microsoft.VisualStudio.TestTools.UnitTesting;
490+
using System.Runtime.CompilerServices;
491+
492+
public class MyTestMethodAttribute : TestMethodAttribute
493+
{
494+
static MyTestMethodAttribute()
495+
{
496+
}
497+
498+
public MyTestMethodAttribute([CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1)
499+
: base(callerFilePath, callerLineNumber)
500+
{
501+
}
502+
}
503+
""";
504+
505+
await VerifyCS.VerifyCodeFixAsync(code, code);
506+
}
507+
508+
[TestMethod]
509+
public async Task WhenDerivedTestMethodAttributeWithStaticField_NoDiagnostic()
510+
{
511+
string code = """
512+
using Microsoft.VisualStudio.TestTools.UnitTesting;
513+
using System;
514+
using System.Runtime.CompilerServices;
515+
516+
public class MyTestMethodAttribute : TestMethodAttribute
517+
{
518+
static DateTimeOffset s_field = new();
519+
520+
public MyTestMethodAttribute([CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1)
521+
: base(callerFilePath, callerLineNumber)
522+
{
523+
}
524+
}
525+
""";
526+
527+
await VerifyCS.VerifyCodeFixAsync(code, code);
528+
}
529+
530+
[TestMethod]
531+
public async Task WhenDerivedTestMethodAttributeWithStaticConstructorAndMissingCallerInfo_Diagnostic()
532+
{
533+
string code = """
534+
using Microsoft.VisualStudio.TestTools.UnitTesting;
535+
536+
public class SomeTestMethodAttribute : TestMethodAttribute
537+
{
538+
static SomeTestMethodAttribute()
539+
{
540+
}
541+
542+
public [|SomeTestMethodAttribute|]()
543+
: base()
544+
{
545+
}
546+
}
547+
""";
548+
549+
string fixedCode = """
550+
using System.Runtime.CompilerServices;
551+
using Microsoft.VisualStudio.TestTools.UnitTesting;
552+
553+
public class SomeTestMethodAttribute : TestMethodAttribute
554+
{
555+
static SomeTestMethodAttribute()
556+
{
557+
}
558+
559+
public SomeTestMethodAttribute([CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1)
560+
: base(callerFilePath, callerLineNumber)
561+
{
562+
}
563+
}
564+
""";
565+
566+
await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
567+
}
484568
}

0 commit comments

Comments
 (0)