Skip to content

False Positive for Warning TUnit0046 When Returning Func<T> from Data Source #2337

@samtrion

Description

@samtrion

Description

We are encountering a likely false positive for warning TUnit0046 when returning an array of Func<IConstraint> from a [MethodDataSource]-annotated method. The intention of this rule—to ensure lazy evaluation of test cases—is already fulfilled by the use of Func<T>. However, the analyzer still raises the warning.

Reproduction Example

[Test]
[MethodDataSource(nameof(ChainedInvalidOperationsData))]
public void Value_ChainedInvalidOperations_ShouldThrowInvalidOperationException(Func<IConstraint> funcConstraint) =>
    _ = Assert.Throws<InvalidOperationException>(() =>
    {
        var _ = funcConstraint.Invoke();
    });

public static Func<IConstraint>[] ChainedInvalidOperationsData() =>
    [
        () => Value.Not.Not,
        () => Value.Null.And.And,
        () => Value.Null.And.Or,
        () => Value.Null.And.Xor,
        () => Value.Null.Or.And,
        () => Value.Null.Or.Or,
        () => Value.Null.Or.Xor,
        () => Value.Null.Xor.And,
        () => Value.Null.Xor.Or,
        () => Value.Null.Xor.Xor,
    ];

Expected Behavior

No warning should be emitted, as the method correctly returns an array of deferred Func<IConstraint> instances, not evaluated values.

Related repository: dailydevops/fluentvalue#126

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions