Resharper Erroneously Thinks Properties are Not Nullable

Not sure when it started, but it was about a month ago.  Maybe with the 2020.1 release.

Using the RestSharp Nuget library, Resharper thinks that several of the RestSharp object's properties are Not Nullable.  I get 3 laughably wrong warnings from Resharper that I have had to turn off at the class level.

[SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalse", Justification = "Resharper is wrong.")]
[SuppressMessage("ReSharper", "HeuristicUnreachableCode", Justification = "Resharper is wrong.")]

These two spawn from the fact that Reshaper seems to think that the IRestResponse.ErrorException property is Not Nullable, when in fact it will be null most of the time.  I've checked the source on GitHub, even the comments imply it will be null unless an HttpClient exception is thrown.

[SuppressMessage("ReSharper", "ConstantConditionalAccessQualifier", Justification = "Resharper is wrong.")]

This one is caused by Resharper thinking that the IRestRequest.Body property and its child property Value will never be null.  When, in fact, they will be null if there is no body passed on the request.

I'm currently on 2020.1.3 and these warnings are still happening.




Sorry for the delay in responding.

Could you please provide some sample solution demonstrating the problem? You can share it privately via "Submit a request" form at the top of the page.

Thank you.


I have submitted the request with an example project.


This still happens with ReSharper 2023.3.4. Older project with RestSharp 106.11.7, ReSharper believes that both ErrorMessage and ErrorException are non-nullable, when they will actually usually be null. The first two WriteLines have “Nothing” grayed out with “?? left operand is never null” (even though it prints out “Nothing”), and both the IF statements below are highlighted with “Expression is always true”, even though ErrorMessage2 doesn't log.

var client = new RestClient();
var request = new RestRequest(new Uri(""));
var response = client.Execute(request, Method.GET);
Console.WriteLine($"ErrorMessage: {response.ErrorMessage ?? "Nothing"}");
Console.WriteLine($"ErrorException.Message: {response.ErrorException?.Message ?? "Nothing"}");
if (response.ErrorMessage != null)
    Console.WriteLine($"ErrorMessage2: {response.ErrorMessage}");
if (response.ErrorException != null)
    Console.WriteLine($"ErrorException.Message2: {response.ErrorException.Message}");

The response is an IRestResponse, which has MessageException defined as a System.Exception, and MessageError is just a string.
We do NOT have <Nullable>annotations</Nullable> in our .csproj files, so I don't know why this is happening. Everything works as expected, we just have these false warnings. It doesn't seem to happen with our own interfaces.



thank you for contacting us.

What we have discovered earlier, the issue is related to the following: 

RestSharp is compiled with "Nullable=enable" option and IRestResponce<T> while ErrorException is declared as non-nullable property of type "Exception", so ReSharper considers it not null.

Thank you!



Please sign in to leave a comment.