Skip to content

VSTHRD012 wrongly reported on private/internal members #1111

Closed

Description

@Nefarion

Bug description

VSTHRD012 gets reported if there is a private overload of a method with a JTF parameter.

Repro steps

static class Extensions
{
    public static void OnMainThread(Action action) => OnMainThread(Factory, action);
    
    private static void OnMainThread(JoinableTaskFactory factory, Action action) => factory.Run(async delegate
    {
        await Factory.SwitchToMainThreadAsync();
        action();
    });
}

From anywhere else:

Extensions.OnMainThread(() => { }) // <- this will report VSTHRD012

Expected behavior

No VSTRHD012 reported

Actual behavior

VSTHRD012 gets reported.

  • Version used: 17.4.27

Additional context

Changing the name of the private method to something other than the name of the public method fixes VSTHRD012

Activity

  1. sujitmahapatra commented on Oct 3, 2023

    @sujitmahapatra

    To address the issue where VSTHRD012 is reported when there is a private overload of a method with a JTF (JoinableTaskFactory) parameter, you can follow these steps:

    1. Identify the Problem: The issue seems to be related to the naming conflict between the public and private methods. When both methods have the same name, it triggers VSTHRD012.

    2. Rename the Private Method: To resolve this issue, you should rename the private method to something that does not conflict with the public method. Here's an example of how you can modify your code:

    static class Extensions
    {
        public static void OnMainThread(Action action) => OnMainThread(Factory, action);
        
        private static void RunOnMainThread(JoinableTaskFactory factory, Action action) => factory.Run(async delegate
        {
            await Factory.SwitchToMainThreadAsync();
            action();
        });
    }
    1. Update the Usage: After renaming the private method, you'll need to update any usage of the private method with the new name. In this case, you should change OnMainThread to RunOnMainThread:
    Extensions.RunOnMainThread(() => { }) // This should no longer report VSTHRD012
    1. Test and Verify: Ensure that the code now works as expected and that VSTHRD012 is no longer reported.

    By renaming the private method to something distinct from the public method, you avoid the naming conflict that was triggering the VSTHRD012 warning. This should resolve the issue as expected.

  2. added a commit that references this issue on Oct 24, 2023
    cc63319
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions