Critical code smell when using CallerArgumentExpression

SonarQube Version

9.5.0.56709

Description of the issue

We want to retrieve the name of a parameter. It is tedious for the user to enter it himself using nameof(parameter). Therefore, we decided to use the new CallerArgumentExpression introduced in C# 10. That being said, this feature is limited, so we want the user to be able to override this and provide the parameter name himself. (See code example below of what we are trying to achieve)

public string GiveBackParameter<T>(T value, [CallerArgumentExpression("value")] string? parameterName = default)
{
    return parameterName;
}

This works fine, but when scanning our code with Sonar, the following issue is being thrown: “Use the overloading mechanism instead of the optional parameters.”

In this particular case, we can’t overload the method with simply the value parameter, since then we wouldn’t have the parameterName (and CallerArgumentExpression may only be applied to parameters with default values)

What I have tried so far

We resolved the issue as a false positive.

Hello @len067,

We can confirm this as a false positive. We created an issue on Github and put it into our backlog:

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

The issue is fixed and scheduled for the next release.