[英]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 . 在此示例中,resharper预测task.Exception.Flatten()中可能存在空指针异常,因为它假定task.Exception可能为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. 但是对于所有实际场景,它不会为null,因为参数TaskContinuationOptions.OnlyOnFaulted确保仅在发生异常时才调用该方法。
So How do I tell Resharper to ignore all similar warnings ? 那么我如何告诉Resharper忽略所有类似的警告?
I think you have several options: 我想你有几个选择:
null
check. null
检查。 I don't like #4, you would be making your code less readable just so that ReSharper is happy. 我不喜欢#4,你会让你的代码不那么可读,这样ReSharper很高兴。 I also don't like #3, that could pollute your code with those comments a lot.
我也不喜欢#3,这可能会大量污染你的代码。 #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”.
#2更好,但我认为#1是最好的选择:“Posible NullReferenceException”总是会出现误报,所以你应该用它作为指导:“小心点,有些东西可能是错的”,而不是严格的“你必须解决这个问题。“
Resharper支持团队已接受此错误,可在此处跟踪http://youtrack.jetbrains.com/issue/RSRP-316492
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.