Skip to content

Conversation

@Vogel612
Copy link
Contributor

Initial implementation of an analyzer that verifies MemberData has an associated initializer expression or static constructor.

This PR is in Draft to solicit feedback on test cases that still need covering and guidance on how to preferrably handle existing test cases generating new analyzer results now that there is a stricter analyzer.

@Vogel612

This comment was marked as resolved.

@Vogel612 Vogel612 marked this pull request as ready for review October 4, 2025 12:56
@Vogel612
Copy link
Contributor Author

Vogel612 commented Oct 4, 2025

@bradwilson Would appreciate some feedback on this. As a note: this was developed on linux, and I haven't had the chance to do a verification run on windows

@bradwilson
Copy link
Member

Sorry for the delay getting to this, I haven't really had much personal time for xUnit.net in the last month.

@bradwilson bradwilson merged commit 71bb98c into xunit:main Oct 12, 2025
4 checks passed
@bradwilson
Copy link
Member

Thanks!

@bradwilson
Copy link
Member

Something odd is happening: this analyzer will report with the compiler, but does not report inside Visual Studio.

Using this as my test:

public class UnitTest1
{
    public static TheoryData<int> DataSource;

    [Theory]
    [MemberData(nameof(DataSource))]
    public void TestMethod(int x)
    {
        Assert.NotEqual(0, x);
    }
}

I get nothing in Visual Studio:

image

But when I build, the warning shows:

UnitTest1.cs(3,35,3,45): warning xUnit1053: The member DataSource referenced by MemberData is not initialized before use. Add an inline initializer or initialize the value in the static constructor. (https://xunit.net/xunit.analyzers/rules/xUnit1053)

I've never seen anything like this, and none of the other analyzers are affected. Is there any reason you can think of that your analyzer implementation doesn't work inside Visual Studio?

@Vogel612
Copy link
Contributor Author

@bradwilson honestly no idea why that difference would show up like that. If I was forced to guess, I would try to figure out whether the same version of roslyn is used between the two analysis modes, but from the little experience I have with writing analyzers, these should behave the same...

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.

2 participants