Skip to content

NET-3004 Fix S2696 FP: New extension method format triggers FP when accessing static fields #9783

@dougwaldron

Description

@dougwaldron

Description

The new C# 14 extension method syntax introduces a false positive for S2696 if the method updates a static field. This warning was not raised with the old syntax.

Reproducer

public static class ProvidersExtensions
{
    private static IEnumerable<string>? _providers;

    extension(IConfiguration configuration)
    {
        public IEnumerable<string> GetProvidersNewFormat()
        {
            // The following line triggers the S2696 warning.
            _providers ??= configuration.GetSection("Providers").Get<string[]>() ?? [];
            return _providers;
        }
    }

    public static IEnumerable<string> GetProvidersOldFormat(this IConfiguration configuration)
    {
        // The following line does not trigger the warning.
        _providers ??= configuration.GetSection("Providers").Get<string[]>() ?? [];
        return _providers;
    }
}

Product and Version

SonarAnalyzer.CSharp 10.18.0.131500

Metadata

Metadata

Assignees

No one assigned

    Labels

    False PositiveRule IS triggered when it shouldn't be.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions