CanBeNull and ReSharper - using it with async Tasks?

匿名 (未验证) 提交于 2019-12-03 00:46:02

问题:

I recently figured out that you can use the [CanBeNull] annotation in C# to tell ReSharper (and other addons) that a method can return null. This is great, because it makes ReSharper remind me when I don't handle those situations.

However, for async methods that return a Task or a Task<T>, the behavior is unexpected.

For instance, consider this example:

[CanBeNull] public async Task<string> GetSomeName() {     var time = DateTime.Now;     if(time.Second == 30) {          return "Jimmy";      } else {         return null;     } } 

I know that this scenario is a bit weird, but for simplicity, bear with me. If I (with ReSharper enabled) then try to invoke the method elsewhere, it warns incorrectly. For instance:

var myValue = await GetSomeName(); var subValue = myValue.Trim(); //here, ReSharper should warn me that subValue is null. 

Here, ReSharper warns me at the incorrect place. The first line generates a warning (and it claims that the task itself can actually be null, which is wrong). The second line doesn't generate a warning, which is where the warning should have been.

If I were to comply with ReSharper entirely, this code would have to be written:

var myTask = GetSomeName(); if(myTask != null) {      //this is silly, and is always true, but ReSharper thinks that the Task can be null due to the CanBeNull attribute.      var myValue = await myTask;     var subValue = myValue.Trim(); //this could generate an error, but ReSharper doesn't warn me.  } 

Is this a bug with ReSharper that I should submit? Or am I using the annotation incorrectly? I guess we can all agree that the task itself can't ever be null, so I don't know how this makes sense.

回答1:

Ivan Serduk said: "Starting from ReSharper 9.2 EAP4 attributes [ItemCanBeNull] and [ItemNotNull] can be applied to entities of type "Task" and "Lazy". It works perfect!

P.S Please don't forget to update Jetbrains Annotations.



回答2:

You've hit a limitation of ReSharper's null value analysis. It is trying to treat the return value (the task) as potential null, rather than the result. However, that's a great feature request - I'd suggest voting for this issue: http://youtrack.jetbrains.com/issue/RSRP-376091



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!