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:
The input
can be:
null
/notnull
for reference type parameterstrue
/false
for boolean parameters.
The output
can be
null
/notnull
/canbenull
for the return value of reference typetrue
/false
for the return value of boolean typehalt
|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
andinput
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 tooutput <= 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)