Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 12, 2025

MSTEST0057 was incorrectly flagging static constructors in TestMethodAttribute-derived classes, suggesting impossible [CallerFilePath] and [CallerLineNumber] parameter additions.

Changes

  • Analyzer: Filter out static constructors in TestMethodAttributeShouldPropagateSourceInformationAnalyzer.cs (line 72)
    • Static constructors cannot have parameters, so caller info attributes don't apply
  • Tests: Added coverage for static constructor scenarios
    • Explicit static constructor with valid instance constructor
    • Implicit static constructor (from static field) with valid instance constructor
    • Static constructor with invalid instance constructor (verifies instance constructor still triggers diagnostic)

Example

Both cases below now correctly emit no diagnostic:

// Case 1: Static field initialization
public class MyTestMethodAttribute : TestMethodAttribute
{
    static DateTimeOffset s_field = new();

    public MyTestMethodAttribute([CallerFilePath] string path = "", [CallerLineNumber] int line = -1)
        : base(path, line) { }
}

// Case 2: Explicit static constructor
public class SomeTestMethodAttribute : TestMethodAttribute
{
    static SomeTestMethodAttribute() { }

    public SomeTestMethodAttribute([CallerFilePath] string path = "", [CallerLineNumber] int line = -1)
        : base(path, line) { }
}

Fixes #[issue_number]

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • l7avsblobprodcus319.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/11.0.0-beta.25560.1.txt (dns block)
    • Triggering command: dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --no-restore (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>MSTEST0057 triggers on static constructors</issue_title>
<issue_description>## Describe the bug

When a class deriving from TestMethodAttribute has a static constructor, MSTEST0057 is triggered suggesting adding [CallerFilePath] and [CallerLineNumber], despite the class already having the suggested constructor.

Steps To Reproduce

In this case the fixer suggest adding a constructor identical to the existing constructor.

public class MyTestMethodAttribute : TestMethodAttribute // <-- MSTEST0057 is triggered
{
    static DateTimeOffset s_field = new();

    public MyTestMethodAttribute([CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1)
        : base(callerFilePath, callerLineNumber)
    {
    }
}

This this case the fixer suggests adding [CallerFilePath] and [CallerLineNumber] to the static constructor.

public class SomeTestMethodAttribute : TestMethodAttribute
{
    static SomeTestMethodAttribute() // <-- MSTEST0057 is triggered
    {
    }

    public SomeTestMethodAttribute([CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1)
        : base(callerFilePath, callerLineNumber)
    {
    }
}

Expected behavior

MSTEST0057 should not be triggered.

Actual behavior

MSTEST0057 is triggered.

Additional context

</issue_description>

Comments on the Issue (you are @copilot in this section)

@Youssef1313 Thanks for bringing this to our attention!

It's indeed a false positive that you can suppress for now until this is fixed.</comment_new>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix MSTEST0057 trigger for static constructors Fix MSTEST0057 false positive on static constructors Nov 12, 2025
Copilot AI requested a review from Youssef1313 November 12, 2025 09:35
Copilot finished work on behalf of Youssef1313 November 12, 2025 09:35
@Youssef1313 Youssef1313 marked this pull request as ready for review November 12, 2025 10:19
@Youssef1313 Youssef1313 enabled auto-merge (squash) November 12, 2025 16:25
@Youssef1313 Youssef1313 merged commit 6a94716 into main Nov 14, 2025
9 checks passed
@Youssef1313 Youssef1313 deleted the copilot/fix-mstest0057-issue branch November 14, 2025 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MSTEST0057 triggers on static constructors

3 participants