简体   繁体   English

Resharper false可能带有任务并行库的nullReferenceException警告

[英]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: 我想你有几个选择:

  1. Ignore the warning in this instance. 忽略此实例中的警告。
  2. Lower the severity of all “Posible NullReferenceException” to something like Hint, or even Do not show. 将所有“Posible NullReferenceException”的严重性降低到类似提示,甚至不显示。
  3. Disable this instance of the warning by a comment. 通过注释禁用此警告实例。
  4. Pretend ReSharper is right and add the null check. 假装ReSharper是正确的并添加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.

相关问题 任务并行库使用IEnumerable引发NullReferenceException - Task Parallel Library throws NullReferenceException with IEnumerable 尽管存在隐式null检查,但可能存在System.NullReferenceException的ReSharper警告 - ReSharper warning for Possible System.NullReferenceException despite implicit null check ReSharper:如何删除“可能的‘System.NullReferenceException’”警告 - ReSharper: how to remove "Possible 'System.NullReferenceException'" warning ReSharper:Enumerator可能出现NullReferenceException? - ReSharper: Possible NullReferenceException with Enumerator? Resharper-可能将Null分配给not-null属性的错误肯定警告 - Resharper - False positive warning on possible assignment of Null to not-null Attribute ReSharper“可能的NullReferenceException”错误的FileInfo? - ReSharper “Possible NullReferenceException” wrong with FileInfo? 可能的NullReferenceException ReSharper代码分析C# - Possible NullReferenceException ReSharper code analysis C# 为什么ReSharper在动态类型上建议可能的NullReferenceException? - Why is ReSharper suggesting a possible NullReferenceException on a dynamic type? 为什么 Resharper 告诉我可能存在 NullReferenceException? - Why does Resharper tell me there is a possible NullReferenceException? Resharper可能为空参考警告 - Resharper possible null reference warning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM