C#: False positive '@object' is null on at least one execution path

Hi,

could you please take a look at this code:

ObjectInstance object = null;
        if (object?.Value is not Table || (object.Value is Table t && t.isModified()))
        {
            string falsePositive = "I'll never check second object.Value";
        }

In SonarCloud I get this error:
obraz

According to my knowledge and VisualStudio it’s a false positive. If object is null in the first condition, then it will never check the second one, because it is already checked here object?.Value is not Table.

obraz

Could you please help me find a solution to that issue?

Hi @M_Wtyhk , thanks for reporting this issue.

I confirm I can reproduce the issue, which is indeed a False Positive case for rule S2259.

A GitHub issue has been created in the GitHub repo of the .NET Analyzers, to fix the False Positive case.

Waiting for the issue to be fixed, you can mark the issue in SonarCloud as a False Positive through the issues interface.

Best regards,
Antonio

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