1

Still learning code contracts. When I create a small test, I get the following message from the checker: CodeContracts: Invoking this method will always lead to an error. If this is wanted, consider adding Contract.Requires(false) to document it.

I don't understand what it is try to tell me. How would I add Contract.Requires(false) to this example so the warning is not shown?

This is the code. Note that this is a contrived example solely for the purpose of learning CC.

 void DoSomething(object test) {
        Contract.Requires(test != null);
        MessageBox.Show(test.ToString());
    }


    void InvokeDoSomething() {
        DoSomething(null);

    }
Greg Gum
  • 33,478
  • 39
  • 162
  • 233

1 Answers1

0

Code Contracts have discovered that you

  1. Require a parameter to a method to never be null
  2. Literally call it with null

CC is basically telling you that your code will always fail. The contract seems fine, but your call is bad. The solution is of course not to add Contract.Requires(false) but to not call the method with null.