You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
NET-3468 Fix S1144 FP: Erroneously flagged Add() method used in Collection Initializer #9778
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)
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:
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
Activity
jilles-sg commentedon Dec 24, 2025
This seems like a valid FP. It does not seem Roslyn's fault, since Find All References on the
Addmethod 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:
[-]Fix S1144 FP: Erroneously flagged Add() method used in Collection Initializer[/-][+]NET-3468 Fix S1144 FP: Erroneously flagged Add() method used in Collection Initializer[/+]github-actions commentedon Mar 25, 2026
Internal ticket NET-3468
alex-meseldzija-sonarsource commentedon Mar 25, 2026
Hi @markusschaber thanks for the report!
I can confirm this is an FP, we'll add it to our backlog.
alex-meseldzija-sonarsource commentedon Apr 14, 2026
Hi @markusschaber ,
this has been fixed in the 10.23 release of our analyzers.