- 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.