SonarLint for C# doesn't complain about Nullable access

  • Operating system: Windows 10
  • SonarLint plugin version: 6.13.0.62767
  • Programming language you’re coding in: C#
  • Is connected mode used: No
    • Connected to SonarCloud or SonarQube (and which version): No

And a thorough description of the problem / question:

I have a simple function, taking a nullable parameter:

private static bool IsTen(int? value)
{
	return value.Value == 10;
}

I can call it with a null value, like this:
var gotTen = IsTen(null);

I expected SonarLint to complain about the “value.Value” access, in the IsTen function, but it doesn’t.
Tried in a separate project too, just to check nobody disabled the rule by mistake.
Is this a bug? If not, please consider it a feature request.

Hey there.

Can you clarify which rule you’re discussing, as noted in this post?

Sorry, it seems I missed the report guide somehow…
The rule is S3655: Empty nullable value should not be accessed
Also, SonarLint will complain as soon as the commented out line is enabled:

private static bool IsTen(int? value)
{
	//var v = value != null ? value.Value : 0;
	return value.Value == 10;
}

Hey @mBardos76

Been a while!

Can you confirm that you can reproduce this on the latest version of SonarLint for Visual Studio! We’ve had a few releases since March. :slight_smile:

First, Happy New Year! :wink:
And yes, the code I added in my previous comment still reproduces in VS 2022 and latest SonarLint.

Hi @mBardos76, and a happy new year!
We currently consider this example to be compliant. We do not make assumptions about the state of a nullable value type unless the code checks explicitly for null. The reasoning is that the developer might know that this method never gets passed a null argument, and maybe they cannot change the method signature for some reason.
However, I see value in raising here and will discuss changing the rule’s behavior with the team.

Maybe also of interest to you: While investigating this issue, I found that we have a similar situation with nullable reference types. If nullable context is enabled, the arguments for treating this as Noncompliant become much more substantial.

Update: I discussed it with the team. We will not change the rule’s behavior but introduce a new rule covering this and similar cases in the future.