SergeyTeplyakov / ReSharperContractExtensions

Set of extensions for R# that simplifies Design by Contract programming using Code Contracts.
MIT License
69 stars 10 forks source link

Error when contract validator method contains Contract.Requires/Ensures #15

Open SergeyTeplyakov opened 10 years ago

SergeyTeplyakov commented 10 years ago

Custom contract validators (methods marked with ContractArgumentValidator) should not contains any other calls of the Contract class except the call to the Contract.EndContractBlock:

[ContractArgumentValidator]
public void Guard()
{
    // error CC1054: Contract argument validator 'InstanceValidator.Guard' cannot contain
    // ordinary contracts. Only if-then-throw or validator calls are allowed.
    if (!_condition)
        throw new ArgumentException();
    Contract.Ensures(false);
    Contract.EndContractBlock();
}

Actually as part of this issue all other rules of the custom validators should be considered.

pfurini commented 9 years ago

Please consider the following scenario (tested and working):

Now the static checker will work as expected, thanks to the Contracts reference assembly augmented with standard Contract.Require calls. Otherwise you'll get weird warnings by the tool.

IMHO this enhancement should be removed or I should be able to selectively disable it.. Thanx

SergeyTeplyakov commented 9 years ago

I believe that I didn't implement this enhancement yet. When I'll start working on it, I'll definitely will consider your suggestion.