FP - S1144 - Unused private types or members should be removed - Extension Syntax

  • What language is this for? c# 14
  • Which rule? S1144 - Unused private types or members should be removed
  • Why do you believe it’s a false-positive/false-negative? My method is clearly being used
  • Are you using
    • SonarQube for IDE - which IDE/version? - VS2026 / SonarQube for Visual Studio 2022 8.30.0.15605
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)

Code

using Microsoft.Extensions.DependencyInjection;

namespace MyNameSpace;

public sealed class MyService(object key) 
{
    public void LogKey() => Console.WriteLine(key);
}

public static class ServiceCollectionExtensions 
{
    extension(IServiceCollection services)
    {
        public IServiceCollection AddService() => services
            .AddKeyedSingleton("abc", ServiceFactory);

        private static MyService ServiceFactory(IServiceProvider sp, object key) => new(key);
    }
}

I get an S1144 warning for the method ServiceFactory claiming it is unused. However it is being used two lines above.

I suspect this is because of the new extension syntax that I’m using.

2 Likes

Hi @RJM,

Thanks for the report!
I can reproduce this on my local and agree its an FP.

I’ve added a reproducer to our codebase and a ticket to fix.

2 Likes