Is there a common PureAttribute that Resharper and Code Contracts can both use?

ReSharper already understands System.Diagnostics.Contracts.PureAttribute and treats it the same way as JetBrains.Annotations.PureAttribute, so you can just use the one from Code Contracts, and both tools will be happy.


Approach 3 offers the solution: Jetbrains.Annotations.PureAttribute IS recognized by Contracts.

However, you still will encounter the name conflict problem when using Contracts and PureAttribute in your code. This can be shorthanded with a using statement:using RPure = Jetbrains.Annotation.PureAttribute;

Here's some code that demonstrates the attribute successfully working for Contracts and ReSharper.

public class StatefulExample {

    public int value { get; private set; }

    public StatefulExample() { value = 1; }

    //Example method that would need to be annotated
    [Jetbrains.Annotations.PureAttribute]
    public int negativeValue() { return -value; }

    public static void useSpecificState(StatefulExample test) {
        Contract.Requires(test.value == 1);
    }

    // ---

    public static void pureMethodRequirementDemonstration() {
        StatefulExample initialState = new StatefulExample();
        useSpecificState(initialState);

        StatefulExample possiblyChangedState = new StatefulExample();
        //"Return value of Pure method is not used" here if annotated.
        possiblyChangedState.negativeValue();

        // "Requires unproven: test.value == 1" if NOT annotated.
        useSpecificState(possiblyChangedState);
    }

}