S2259 reported after an NUnit Is.Not.Null check

SonarQube 9.5
Visual Studio 17.2

SonarQube does not seem to recognise that an NUnit assertion has already validated a value is not null:

HttpResponse response = someObject As HttpResponse;
Assert.That(response, Is.Not.Null);
Assert.That(response.StatusCode, Is.EqualTo(StatusCodes.Status200OK));

Given that the first Assert Is.Not.Null ensures the value cannot be null, S2259 should not bedetected in the second assert since it is not possible to reach that line in any execution path

similar problem if I do:

if (response == null) Assert.Fail("Response is null");
Assert.That(response.StatusCode, Is.EqualTo(StatusCodes.Status200OK));

Again, there is no way to get to the second line after the previous assertion

Hello @tbutler!

Indeed this is a false positive.
There’s already an open issue in our repository for this.
I’m aware that the issue has been open for quite some time, but given that this rule is currently getting migrated to a new symbolic execution engine, it should be tackled in the near future.

Best Regards,
Mary

Good to know, thanks @Mary_Georgiou
Do you have any sort of timescales on this?

thanks

Tony

Hi @tbutler!

I’m sorry but we don’t have an ETA currently for this.
I suggest you follow the GitHub issue for any updates.

Best Regards,
Mary

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