Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CC Suggesting Redundant Ensures

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.

like image 772
Martin Avatar asked Nov 09 '22 17:11

Martin


1 Answers

Regarding 2: The documentation is pretty good, take a look at 6.6.10 Filtering Warning Messages:

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.

like image 88
Phonolog Avatar answered Nov 14 '22 22:11

Phonolog