C# Null Check false positive

1,264 views
Skip to first unread message

joe.a.he...@gmail.com

unread,
Jun 6, 2018, 3:45:50 PM6/6/18
to SonarQube
Hello!

I observed a false positive report:  S2259:  'variable' is null on at least one execution path.  This occurs within a unit test.  Here's a snippet of the code

var result = serviceToBeTested.PerformAction();

Assert.IsNotNull(result?.Details);
Assert.AreEqual(myExpectedResult, result.Details.DetailProperty); // 'result' is null on at least one execution path.

because I'm using the conditional member operator (?.), if either result or result.detail is null, then the Assert fails, and the next line will not be executed


Love your product, and I hope my feedback helps make it better.

valeri....@sonarsource.com

unread,
Jun 8, 2018, 4:08:48 AM6/8/18
to SonarQube
Hi Joe,

Thanks for the feedback, we appreciate it!

Regarding the issue, you are right, it is indeed a false positive. When you "check" result for null using the conditional operator, our analyzer notes that result could be null and raises if you directly dereference it without check. This particular false positive occurs because the analyzer is not aware that the Assert methods stop the execution of the code.

What version of SonarC# or SonarLint are you using? As far as I can see, this rule should not run on unit tests and it is surprising that you get an issue there.

Kind regards,
Valeri


joe.a.he...@gmail.com

unread,
Jun 8, 2018, 2:24:35 PM6/8/18
to SonarQube
Thank you for your quick reply.  I appologize that I forgot to provide my version information with my post.

I am building with the current VSTS extension,  and our locally hosted SonarQube server is Version 7.0 (build 36138).

Hope this helps.

joe.a.he...@gmail.com

unread,
Jun 8, 2018, 2:35:05 PM6/8/18
to SonarQube
Regarding your concern about the analyzer running against the unit tests,  I'm building a .NET Core 2.0 application.  Here's a sample of our CSPROJ file for the test assembly.  Hope this helps...

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<ProjectGuid>{ProjectGuid}</ProjectGuid>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.1" />
<PackageReference Include="Moq" Version="4.8.2" />
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MyAssembly.csproj" />
</ItemGroup>

</Project>

valeri....@sonarsource.com

unread,
Jun 11, 2018, 5:07:48 AM6/11/18
to SonarQube
If you haven't upgraded your SonarC# plugin in SonarQube, then you are using relatively old version of the analyzers (SonarQube 7.0 was shipped with SonarC# 6.7 if my memory is correct). I think that if you upgrade SonarC# to the latest version the issues reported in the tests will disappear (you need to login in SonarQube with admin account, then go to Administration -> Marketplace, then find Sonar C# and click Update).
Reply all
Reply to author
Forward
0 new messages