[csharpsquid:S1172] Direct references to methods of parameters are not always recognized as usages

Version: SonarLint 6.12.0.59751 / Visual Studio / not connected
Rule: C# / S1172

The rule (do not include unused parameters) is triggered with this code:

private static void  BuildSection(
    IReadOnlyDictionary<int, string> items, // <- considered unused
    IEnumerable<int> values
)
{
    var groups = values.Select(items.GetValueOrDefault); // <- actually used here
    // ...
}

This version of the code does NOT trigger the rule:

private static void  BuildSection(
    IReadOnlyDictionary<int, string> items,
    IEnumerable<int> values
)
{
    var groups = values.Select(v => items.GetValueOrDefault(v));
    // ...
}

The difference is that the first version passes a method directly as the argument of .Select(), while the second wraps it in a lambda.
Note that GetValueOrDefault also has an overload with two parameters: the compiler seems to pick the correct one in the first case, but i guess Sonar gets confused about them.
See here: CollectionExtensions.GetValueOrDefault Method (System.Collections.Generic) | Microsoft Learn

Hello M Gallesio

I could not reproduce the issue in the latest version of our analyzer.
I tried this test code and S1172 is not triggered:

    public class PassedAsMethodGroup
    {
        public void ParameterPassedAsMethodGroup(
                IReadOnlyDictionary<int, string> items,
                IEnumerable<int> values)
        {
            var x = values.Select(items.GetValueOrDefault);
        }
    }

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

Duplicate post - see here