Skip to content

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

Closed

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

Activity

  1. Corniel commented on Jan 10, 2026

    @Corniel
    Contributor
  2. changed the title [-]Fix S2696 FP: New extension method format triggers FP when accessing static fields[/-] [+]NET-3004 Fix S2696 FP: New extension method format triggers FP when accessing static fields[/+] on Jan 20, 2026
  3. github-actions commented on Jan 20, 2026

    @github-actions
    Contributor

    Internal ticket NET-3004

  4. alex-meseldzija-sonarsource commented on Jan 20, 2026

    @alex-meseldzija-sonarsource
    Contributor

    Hi @dougwaldron,
    Thanks for the report, this is an FP.

    While it is similar to the S2325 FP, unfortunately they do not share an implementation so that fix is specific to that rule.

  5. Tim-Pohlmann commented on Feb 5, 2026

    @Tim-Pohlmann
    Contributor

    Hi @dougwaldron, this False Positive has been fixed and is now available on SonarQube Cloud and will soon come to SonarQube Server 2026.2 and SonarQube Community Build 26.3.

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

    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