CC 建议冗余确保

CC Suggesting Redundant Ensures

我有一段代码看起来有点像这样:

public TReturn SubRegion(TParam foo)
{
    Contract.Requires(foo!= null);
    Contract.Ensures(Contract.Result<TReturn>() != null);

    if (!CheckStuff(foo))
        foo.Blah();
    return OtherStuff(foo);
}

CC 给我警告:

Warning 301 CodeContracts: Consider adding the postcondition Contract.Ensures(Contract.Result() != null); to provide extra-documentation to the library clients

这显然是完全多余的!我有几个这样的冗余警告,它正在成为一个问题(真正的警告被淹没在大量冗余建议中)。

所以我有两个问题:

1) 我是否遗漏了什么,这意味着这不是一个多余的建议?在哪种情况下我需要做什么来修复这个警告?

2) 或者,如果这只是 CCCheck 的一个怪癖并且无法修复,我该如何隐藏或禁止显示此警告?

N.b。以防万一你认为我的例子遗漏了一些重要的东西,完整的代码是 SubRegion 方法 here.

关于2:documentation不错,看看6.6.10过​​滤警告信息:

To instruct the static contract checker not to emit a particular class of warnings for a method (a type, an assembly), annotate the method (the type, the assembly) with the attribute:

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Contracts", warningFamily)]

where warningFamily is one of: Requires, Ensures, Invariant, NonNull, ArrayCreation, ArrayLowerBound, ArrayUpperBound, DivByZero, MinValueNegation.

If necessary, the static contract checker allows filtering a single warning message (instead of an entire family) as well. To do so you can annotate a method with the attribute

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Contracts", warningFamily-ILOffset-MethodILOffset)]

where warningFamily is as above, and ILOffset and MethodILOffset are used by the static contract checker to determine the program point the warning refers to. The offsets can be obtained from the static contract checker by providing the -outputwarnmasks switch in the "Custom Options" entry in the VS pane. Check the Build Output Window for the necessary information.