Vasudevan Kannan
Vasudevan Kannan

Reputation: 951

Resharper false possible nullReferenceException warning with task parallel library

Consider this example

var task =Task.Factory.StartNew(()=>Console.WriteLine("test"));

task.ContinueWith(antecendent =>
        {
            ExceptionProcessor.HandleError(task.Exception.Flatten());
        }, TaskContinuationOptions.OnlyOnFaulted);

In this example resharper predicts that there is a possible null pointer exception in task.Exception.Flatten() as it assumes task.Exception could be null .

But for all realistic scenarios it is not going to be null as the parameter TaskContinuationOptions.OnlyOnFaulted ensures the method gets called only when an exception occurs.

So How do I tell Resharper to ignore all similar warnings ?

Upvotes: 3

Views: 1159

Answers (2)

Vasudevan Kannan
Vasudevan Kannan

Reputation: 951

Resharper support team has accepted this as a bug and it can be tracked here http://youtrack.jetbrains.com/issue/RSRP-316492

Upvotes: 3

svick
svick

Reputation: 244968

I think you have several options:

  1. Ignore the warning in this instance.
  2. Lower the severity of all “Posible NullReferenceException” to something like Hint, or even Do not show.
  3. Disable this instance of the warning by a comment.
  4. Pretend ReSharper is right and add the null check.

I don't like #4, you would be making your code less readable just so that ReSharper is happy. I also don't like #3, that could pollute your code with those comments a lot. #2 is better, but I think #1 is the best option: “Posible NullReferenceException” will always have false positives and so you should use it as a guidance: “be careful here, something may be wrong”, not as a strict “you have to fix this”.

Upvotes: 3

Related Questions