S1172 false-positive when param used with null-conditional or null-coalescing in local function

  • Language: C#
  • Rule: S1172
  • Product: SonarQube for Visual Studio 8.12.0.12239
  • IDE: Visual Studio Community 2022 17.13.2
  • Connected mode: No

The rule S1172 (“Unused method parameters should be removed”) is triggered when a parameter is actually used in a local function with null-conditional or null-coalescing operators. For example, here text and action trigger the rule, but they are used in the local function Do:

public class MyClass
{
    private void SomeMethod(string text, Action action)  // False-positives for "text" and "action"
    {
        Do();

        void Do()
        {
            Console.WriteLine(text ?? "Nothing");
            action?.Invoke();

            // If these are enabled, the rule does not trigger:
            // Console.WriteLine(text);
            // action();
        }
    }
}

Hello @Trisibo and thanks for reporting this.

I confirm it’s a false positive.
I’ve added a ticket to our backlog to tackle in a future sprint.

Thanks!
Mary