Sonar does not accept null check via method

Sonar does not accept the control when we check for null via method. What can be the solution?

It only accepts when you do as follows:

if (callID == null) 
{
 throw new ArgumentNullException(nameof(callID));
}

It doesn’t see it when I do it like below.

private static void a(string callID) {
            if (callID == null) {
                throw new ArgumentNullException(nameof(callID));
            }
        }

Is this a general problem for sonar? Thanks in advance.

Rule: S3900
image

Hey there.

I’ve moved your post to the section on reporting false-positives. Please have a look at this post, and adjust your topic as necessary (adding details like the Rule ID, and exact versions of products used).

Hello again, I updated my problem.

Thanks.

SonarLint for Visual Studio v4.30 is pretty old at this point (released January 2021) with many improvements since then. I can suggest that you upgrade to the latest version, v6.1, and see if the issue persists.

Hi Colin.
So if we update the version, we can do null check(S3900) via mehods (sending objects to method and checking in it) ?
Like below:

private static void NullCheckMethod(string callID) {
            if (callID == null) {
                throw new ArgumentNullException(nameof(callID));
            }
        }

Improvements have been made to this rule, including being allowed to use extension methods to perform the null check. I recommend upgrading and coming back if the issue persists.

Thanks for your support Colin. You are the best manager I have ever seen.

1 Like