当使用在其返回语句中创建特定新对象的方法时,c# – “Invariant unproven”

以下简单的代码将会产生Code Contracts的静态检查器的“不变的未经证实”警告,尽管没有办法_foo可以为null。该警告是在UncalledMethod中的返回语句。

public class Node
{
    public Node(string s1, string s2, string s3, string s4, object o,
                string s5)
    {
    }
}

public class Bar
{
    private readonly string _foo;

    public Bar()
    {
        _foo = "foo";
    }

    private object UncalledMethod()
    {
        return new Node(string.Empty, string.Empty, string.Empty, string.Empty,
                        GetNode(), string.Empty);
    }

    private Node GetNode()
    {
        return null;
    }

    public string Foo
    {
        get
        {
            Contract.Ensures(Contract.Result<string>() != null);
            return _foo;
        }
    }

    [ContractInvariantMethod]
    private void Invariants()
    {
        Contract.Invariant(_foo != null);
    }
}

除了这个事实,警告是无效的,只是在某些具体情况下才发生。更改以下任何内容将使警告消失:

> Inline GetNode,所以return语句如下所示:

return new Node(string.Empty, string.Empty, string.Empty, string.Empty, null,
                string.Empty);

>从Node的构造函数中删除s1到s5的任何参数。
>将Node的任何参数s1到s5的类型从Node的构造函数中更改为对象。
>为GetNode的结果使用临时变量:

    var node = GetNode();
    return new Node(string.Empty, string.Empty, string.Empty, string.Empty,
                    node, string.Empty);

>更改Node的构造函数的参数顺序。
>在项目设置的代码合同设置窗格中选中“显示假设”选项。

我在这里缺少一些明显的东西,还是静态检查器中的一个bug?

我的设置:

我的输出:

C:\{path}\Program.cs(20,9): message : CodeContracts: Suggested ensures: Contract.Ensures(this._foo != null);
C:\{path}\Program.cs(41,17): message : CodeContracts: Suggested ensures: Contract.Ensures(Contract.Result<System.String>() == this._foo);
C:\{path}\Program.cs(33,13): message : CodeContracts: Suggested ensures: Contract.Ensures(Contract.Result<ConsoleApplication3.Program+Node>() == null);
C:\{path}\Program.cs(27,13): message : CodeContracts: Suggested ensures: Contract.Ensures(Contract.Result<System.Object>() != null);
C:\{path}\Program.cs(55,13): message : CodeContracts: Suggested ensures: Contract.Ensures(Contract.ForAll(0, args.Length, __k__ => args[__k__] != 0));
CodeContracts: ConsoleApplication3: Validated:  92,3%
CodeContracts: ConsoleApplication3: Contract density: 1,81
CodeContracts: ConsoleApplication3: Total methods analyzed 8
CodeContracts: ConsoleApplication3: Methods with 0 warnings 7
CodeContracts: ConsoleApplication3: Total method analysis read from the cache 8
CodeContracts: ConsoleApplication3: Total time 2,522sec. 315ms/method
CodeContracts: ConsoleApplication3: Retained 0 preconditions after filtering
CodeContracts: ConsoleApplication3: Inferred 0 object invariants
CodeContracts: ConsoleApplication3: Retained 0 object invariants after filtering
CodeContracts: ConsoleApplication3: Detected 0 code fixes
CodeContracts: ConsoleApplication3: Proof obligations with a code fix: 0
C:\{path}\Program.cs(27,13): warning : CodeContracts: invariant unproven: _foo != null
C:\{path}\Program.cs(49,13): warning :   + location related to previous warning
C:\windows\system32\ConsoleApplication3.exe(1,1): message : CodeContracts: Checked 13 assertions: 12 correct 1 unknown
CodeContracts: ConsoleApplication3: 
CodeContracts: ConsoleApplication3: Background contract analysis done.
最佳答案
这不再是the latest version的问题。

转载注明原文:当使用在其返回语句中创建特定新对象的方法时,c# – “Invariant unproven” - 代码日志