Search code examples
c#resharpercode-contractsresharper-8.0resharper-7.1

How can I indicate result ambiguity with Resharper CodeAnnotation attributes?


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 => null omits the parameter name in cases there is only one parameter. Basically, null => null means that if the parameter has the value null, the method return value is also null. Note also that this annotation is automatically complemented with notnull => 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")]

Solution

  • You could use canbenull:

    [ContractAnnotation("null => null; notnull => canbenull")]
    

    The full grammar is:

    FDT      ::= FDTRow [;FDTRow]*
    FDTRow   ::= Input => Output | Output <= Input
    Input    ::= ParameterName: Value [, Input]*
    Output   ::= [ParameterName: Value]* {halt|stop|void|nothing|Value}
    Value    ::= true | false | null | notnull | canbenull
    

    As for the bonus question, [ContractAnnotation("null => true")] should be sufficient. Saying that a bool-returning function can return true or false is redundant, as it can't possibly return anything else.