简体   繁体   中英

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 ?

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”.

Resharper支持团队已接受此错误,可在此处跟踪http://youtrack.jetbrains.com/issue/RSRP-316492

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM