Using Resharper's code-annotation attributes, I'm trying to write a ContractAnnotation attribute for a method that will always return null if the input is null, but will return either null or a value if the input is not null. Something like:
[ContractAnnotation("null => null; notnull => null||notnull")]
I would just write this on its own:
[ContractAnnotation("null => null")]
Except that according to Contract Annotations in ReSharper 7, this would be automatically complemented with something that is invalid:
null => nullomits the parameter name in cases there is only one parameter. Basically,null => nullmeans that if the parameter has the valuenull, the method return value is alsonull. Note also that this annotation is automatically complemented withnotnull => notnull.
How might I write the ContractAnnotation attribute to indicate that it cannot be certain what the return value is when the input is notnull?
Or alternatively, how can I stop it from automatically complementing my null => null annotation with notnull => notnull
Bonus question:
How could I write something like the following:
[ContractAnnotation("null => true; notnull => true||false")]
Or in that case, would this be sufficient, since it is not automatically complemented with the inverse?
[ContractAnnotation("null => true")]
You could use
canbenull:The full grammar is:
As for the bonus question,
[ContractAnnotation("null => true")]should be sufficient. Saying that abool-returning function can returntrueorfalseis redundant, as it can't possibly return anything else.