CC offers redundant collateral

I have a piece of code that looks something 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 gives me a warning:

Warning 301 CodeContracts: Consider adding the postcondition Contract.Ensures (Contract.Result ()! = Null); to provide additional documentation to library customers

This is obviously completely redundant! I have several such redundant warnings, and this becomes a problem (real warnings burrow into the stream of redundant offers).

I have two questions:

1) Is there something missing that means it's not a bad recommendation? In this case, what do I need to do to fix this warning?

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

Nb Just in case, if you think that my example is missing something important, the complete code is the SubRegion method here .

+5
source share
1 answer

Regarding 2: the documentation is pretty good, see 6.6.10 Filtering warning messages:

To specify a static contract check so as not to emit a specific class of warnings for the method (type, assembly), annotate the method (type, 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, a static check of the contract allows you to filter one (not the whole family). To do this, you can annotate a method with an attribute

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

where warningFamily is equal to above, and ILOffset and MethodILOffset are used static to determine the program point that the warning points to. Offsets can be obtained from a static check of contracts for inclusion -outputwarnmasks switches to the "User Parameters" entries in the VS panel. Check the build window for the necessary information.

+1
source

Source: https://habr.com/ru/post/1246421/


All Articles