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

Hi @RJM
Just letting you know we just released version 10.18 of our analyzers which fixes this issue.

2 Likes

I tried using an extension block in JetBrains Rider and have the same issue.

My SonrQube for IDE version is 11.12.0.84044 which seems to be the up-to-date version of the plugin.

Anything I have to change on my end for it to work properly and don’t notice it as an issue?

Hi @MLefebvre_progi

Welcome to the community!

From what I can tell that version of SonarQube for IDE should have the fix bundled with it.

Are you able to provide a code snippet that raises for you in Rider?

Oh sorry, just notice it’s not the same error.

The message is Remove unused constructor of private type ‘WireMockTestContextSomethingSetupExtensions’` and that errors spans on the whole extension block.

If I apply the fix, it removes all the extension block altogether

Here’s a redacted version of our class that has the issue :

Summary
public static class WireMockTestContextSomethingSetupExtensions
{
    private static readonly Fixture Fixture = new();

    extension(WireMockTestContext testContext)
    {
        public SomeDto SetupSomething()
        {
            ...
        }

        public void SetupSomethingElse(
            Guid someId,
            IReadOnlyCollection<SomethingElseDto> response)
        {
            ...
        }

        public void SetupFoo(
            Guid someId,
            int someInt,
            IReadOnlyCollection<FooDto> response)
        {
            ...
        }

        public void SetupBar(
            int someInt,
            Guid someId,
            IReadOnlyCollection<BarDto> response)
        {
            ...
        }

        public void SetupFooBar(
            Guid someId,
            IReadOnlyCollection<FooBarDto> response)
        {
            ...
        }
    }
}
1 Like

Awesome, thanks for the reproducer!

It is not an issue on Visual Studio, but I can reproduce in Rider!

I’ll pass this on to our IDE team

1 Like

Hi. Looks like we need to update the analysis engine to support new versions of C#, I bumped the priority on it