JetBrains Rider 2021.3 Help

Code Inspection: Problem in contract annotation definition

This inspection verifies the syntax of contract annotations.

Syntax of contract annotations

Use the following syntax to specify input-output dependencies for contract annotations:

[ContractAnnotation("[paramName:][input] => output [; [paramName:][input] => output]", [forceFullStates:true])]

The input can be:

  • null/notnull for reference type parameters

  • true/false for boolean parameters.

The output can be

  • null/notnull/canbenull for the return value of reference type

  • true/false for the return value of boolean type

  • halt|stop|void|nothing (which are interchangeable) to indicate that the function does not return normally. That is, it throws an exception or halts program execution.

The optional boolean forceFullStates parameter, which is false by default, allows you force the pessimistic mode for the nullability analysis. That is if the method return value is not defined by the contract condition, JetBrains Rider will assume that it might be null.

Remarks:

  • You can omit paramName if there is only one parameter (see the example above)

  • You can omit both paramName and input if there are no parameters:

    [ContractAnnotation("=> halt")] public void TerminationMethod()

    or if the function has the same output independently on the input:

    [ContractAnnotation("=> halt")] public void TerminationMethod(object data, bool flag)
  • You can add several conditions for the same parameter:

    [ContractAnnotation("input:null => null; input:notnull=>notnull")] public object Transform(object input, bool flag)
  • You can reverse conditions, that is input => output is equal to output <= input:

    [ContractAnnotation("null <= surname:null")] public string GetName(string surname)
  • You can also specify expected values for 'out' parameters. If you want to specify both a return value and an 'out' parameter for the same input condition, use the comma:

    [ContractAnnotation("s:null => false,result:null")] public bool TryParse(string s, out object result)
Last modified: 07 April 2022