CC Suggesting Redundant Ensures

时间:2016-04-04 16:48:56

标签: c# code-contracts

I have a piece of code which looks a little like this:

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

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

CC is giving me a warning:

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

Which is obviously completely redundant! I have several such redundant warnings and it's becoming a problem (real warnings getting buried in a torrent of redundant suggestions).

So I have two questions:

1) Am I missing something which means this is not a redundant recommendation? In which case what do I need to do to fix this warning?

2) Alternatively, if this is just a quirk of CCCheck and cannot be fixed how can I hide or suppress this warning?

N.b. Just in case you think my example is missing something important, the full code is the SubRegion method here.

1 个答案:

答案 0 :(得分:1)

关于2:documentation非常好,请查看 6.6.10过​​滤警告信息

  

指示静态合同检查器不发出特定类   方法(类型,程序集)的警告,注释方法   (类型,程序集)具有属性:

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

其中warningFamily是以下之一:Requires, Ensures, Invariant, NonNull, ArrayCreation, ArrayLowerBound, ArrayUpperBound, DivByZero, MinValueNegation

     

如有必要,静态合同检查器允许过滤单个   警告信息(而不是整个家庭)也是如此。这样做你   可以使用属性

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

其中warningFamily为   上面,静态使用ILOffsetMethodILOffset   合同检查器确定警告所指的程序点。   可以通过静态合同检查器获得偏移量   在&#34;自定义选项&#34;中提供-outputwarnmasks开关。进入   VS窗格。检查构建输出窗口是否有必要   信息。