为什么 Resharper / Jetbrains [NotNull] 带注释的接口会警告我 nullreferenceexceptions?

Why do Resharper / Jetbrains [NotNull] annotated interfaces warn me of nullreferenceexceptions?

我正在尝试完成我的项目并使用相应的 Resharper/Jetbrains 注释来注释我的所有方法,因此当我不小心尝试传递不属于某个地方的东西时,我会得到一些帮助和一些打击。

然而,这是我无法理解的。

考虑以下几点:

public interface ITestClass
{
    Task<int?> TestMethod([CanBeNull] int? testInput);
}

public class TestClass : ITestClass
{
    public async Task<int?> TestMethod(int? testInput)
    {
        return await Task.FromResult(testInput);
    }
}

public class TestConsumer
{
    [NotNull]
    private readonly ITestClass m_tester;

    [NotNull]
    private readonly TestClass m_tester2;

    public TestConsumer([NotNull] ITestClass tester, [NotNull] TestClass tester2)
    {
        m_tester = tester;
        m_tester2 = tester2;
    }

    public async Task TestMethod([NotNull] int? testInput)
    {
        var test1 = await m_tester.TestMethod(testInput);
        var test2 = await m_tester2.TestMethod(testInput);
    }
}

我重建它是为了展示我现在面临的问题。通常,我正在使用依赖注入,我希望在我的构造函数中使用某些服务的接口(在这种情况下让 'ITestClass' 成为服务 'TestClass' 的接口)。控制器(参见 'TestConsumer')。

然而,R# / Jetbrains Annotations 显然不喜欢这样。但是,当我尝试使用该接口的实现时 - 没问题。

这真的有点烦人。我不想让我的整个代码现在摆动。显然这也只是处理 await / async 代码时的一个问题,因为如果我省略 'await' 这两种情况都适用,但这不是一个选项。

有什么解决办法吗?最后,R# 在其实现中所具有的接口中可能缺少什么?方法定义在那里。

编辑:

更多信息:

我是 运行 Resharper Ultimate 2018.3.1 Visual Studio Enterprise 2017 Version 15.6.1.

实现 (TestClass.TestMethod) 是一种异步方法,它 return 是 Task 的实例并且永远不会 return 为 null。接口方法 "ITestClass.TestMethod" 的签名不保证结果不为 null(您可以使用简单 return 为 null 的非异步方法来实现它)。因此,在 "pessimistic" 分析模式下,ReSharper 假设 "ITestClass.TestMethod" 可以 return null。要修复它,您可以在接口的方法中添加 "NotNull" 注释:

public interface ITestClass
{
    [NotNull] Task<int?> TestMethod([CanBeNull] int? testInput);
}