告诉resharper一个Func 永远不会返回null

问题描述 投票:1回答:1
[NotNull]
private readonly Func<string> FunctionThatWillNeverBeNullNorReturnNull;

void Test(){

    string thisStringIsNotNull = FunctionThatWillNeverBeNullNorReturnNull();
}

我怎么告诉resharper上面的函数永远不会返回null?设置[NotNull]意味着Function引用不能为null,但我不确定如何告诉resharper它返回的内容也不会为null。

c# null resharper code-analysis static-analysis
1个回答
0
投票

我所做的是创建一个可以注释的委托。

但是,ReSharper不会显示返回值的警告。它仅适用于委托参数。

[CanBeNull]
public delegate string ReturnMaybeNull();

[NotNull]
public delegate string ReturnNotNull([NotNull]string someParam);

[NotNull]
private readonly ReturnMaybeNull FunctionThatMayReturnNull = () => null;

[NotNull]
private readonly ReturnNotNull FunctionThatNeverReturnsNull = someParam => null; // no warning

void Test()
{
    bool test = FunctionThatMayReturnNull().Equals(""); // no warning
    string thisStringIsNotNull = FunctionThatNeverReturnsNull(null); // parameter warning here
    if (thisStringIsNotNull == null) // no warning
    {
        test = test ^ true;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.