Skip to content

False negative when a Task-returning method contains a non-Task returning lambda that contains a synchronous wait #1427

@zjgeary

Description

@zjgeary

Bug description

A Task returning method containing a non-Task returning lambda containing a synchronous wait does not trip any of the VSTHRD analyzers

Repro steps

public static class Example
{
    // VSTHRD002
    public static void VoidVoidExample()
    {
        var action = () => Task.Run(() => { }).Wait();
    }

    // VSTHRD002 and VSTHRD103
    public static void VoidTaskExample()
    {
        var funcTask = () =>
        {
            Task.Run(() => { }).Wait();
            return Task.CompletedTask;
        };
    }

    // VSTHRD103
    public static Task TaskTaskExampleAsync()
    {
        var funcTask = () =>
        {
            Task.Run(() => { }).Wait();
            return Task.CompletedTask;
        };
        return Task.CompletedTask;
    }

    // No violations
    public static Task TaskVoidExampleAsync()
    {
        var action = () => Task.Run(() => { }).Wait();
        return Task.CompletedTask;
    }
}

Expected behavior

At least one of the analyzers should be tripped by TaskVoidExampleAsync()

Actual behavior

No errors are reported

  • Version used: 17.13.2

Edit: simplified example and added syntax highlighting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions