Skip to content

bug: InstanceValuesInTestClassAnalyzer uses return instead of continue #4859

Closed

Description

@thomhurst

Description

File: TUnit.Analyzers/InstanceValuesInTestClassAnalyzer.cs (Lines 53-60)

foreach (var fieldOrProperty in fieldsAndProperties)
{
    var targetSymbol = GetTarget(assignmentOperation);

    if (!SymbolEqualityComparer.Default.Equals(targetSymbol?.ContainingType, testClass))
    {
        return;  // ← Returns from entire method, not just loop iteration
    }

The return statement exits the entire method when the first field/property doesn't match, instead of continuing to check other fields. This means only the first field/property is analyzed per assignment operation — assignments to later fields go undetected.

Impact

False negatives — real problems in user code won't be caught by this analyzer.

Suggested Fix

if (!SymbolEqualityComparer.Default.Equals(targetSymbol?.ContainingType, testClass))
{
    continue;  // ← Should be continue, not return
}

Activity

  1. added a commit that references this issue on Feb 19, 2026
    118bd54
  2. added a commit that references this issue on Feb 19, 2026
    a9bae7c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions