Skip to content

NET-3468 Fix S1144 FP: Erroneously flagged Add() method used in Collection Initializer #9778

Closed

Description

@markusschaber

Description

When a custom Add(...) Method is only used in a collection initializer, SonarQube regards it as unused, although it's called in the initialization code.

Reproducer

using Xunit;

public class Class1
{
    private static readonly SettingsDict TheoryData = new()
    {
        new MySettings(1, 2), new MySettings(3, 4)
    };

    // Theory data in xUnit is restricted to serializable values, so we need to
    // pack our data into a dictionary, and use the keys as theory data.
    public static readonly TheoryData<string> Settings = new(TheoryData.Keys);

    [Theory]
    [MemberData(nameof(Settings))]
    public void SomeTest(string key)
    {
        var (alpha, beta) = TheoryData[key];
        // some fake test
        Assert.NotEqual(alpha, beta);
    }

    private class SettingsDict : Dictionary<string, MySettings >
    {
        // This "Add" method triggers csharpsquid:S1144
        // Remove the unused private method Add.
        internal void Add(MySettings settings)
        {
            Add($"{settings.Alpha}/{settings.Beta}", settings);
        }
    }

    // Some arbitrary data type which we (have to) use as (part of) our theory data.
    private record MySettings(int Alpha, int Beta);
}

Product and Version

SonarQube for IDE 11.6.0.83783 (in JetBrains Rider)

Activity

  1. jilles-sg commented on Dec 24, 2025

    @jilles-sg

    This seems like a valid FP. It does not seem Roslyn's fault, since Find All References on the Add method in Visual Studio Code ms-dotnettools.csharp version 2.110.4 correctly shows two uses in the collection initializer.

    However, this dictionary can be created in fewer lines of code, also avoiding the FP:

        private static readonly Dictionary<string, MySettings> TheoryData = new MySettings[] {
            new(1, 2), new(3, 4)
        }.ToDictionary(settings => $"{settings.Alpha}/{settings.Beta}");
  2. changed the title [-]Fix S1144 FP: Erroneously flagged Add() method used in Collection Initializer[/-] [+]NET-3468 Fix S1144 FP: Erroneously flagged Add() method used in Collection Initializer[/+] on Mar 25, 2026
  3. github-actions commented on Mar 25, 2026

    @github-actions
    Contributor

    Internal ticket NET-3468

  4. alex-meseldzija-sonarsource commented on Mar 25, 2026

    @alex-meseldzija-sonarsource
    Contributor

    Hi @markusschaber thanks for the report!

    I can confirm this is an FP, we'll add it to our backlog.

  5. alex-meseldzija-sonarsource commented on Apr 14, 2026

    @alex-meseldzija-sonarsource
    Contributor

    Hi @markusschaber ,
    this has been fixed in the 10.23 release of our analyzers.

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