Skip to content

bug: GlobalTestHooksAnalyzer accepts CancellationToken without default value #4861

@thomhurst

Description

@thomhurst

Description

File: TUnit.Analyzers/GlobalTestHooksAnalyzer.cs (Lines 150-184)

if (parameters.Length == 1 && 
    parameters[0].Type.GloballyQualifiedNonGeneric() == "global::System.Threading.CancellationToken")
{
    return HookParameterStatus.Valid;  // Accepts without checking default value
}

The analyzer accepts CancellationToken alone as valid without checking if it has a default value. A bare CancellationToken cancellationToken parameter without a default will cause runtime issues, but the analyzer says it's valid.

Impact

Users get runtime errors instead of compile-time diagnostics.

Suggested Fix

if (parameters.Length == 1 && 
    parameters[0].Type.GloballyQualifiedNonGeneric() == "global::System.Threading.CancellationToken" &&
    parameters[0].HasExplicitDefaultValue)  // ← Add this check
{
    return HookParameterStatus.Valid;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions