I'm writing in C# with ReSharper 8.0 and VS2012 for .NET 4.0.
ReSharper includes an attribute: JetBrains.Annotations.PureAttribute. This is used to provide the inspection "Return value of Pure method is not used".
Code Contracts includes an attribute: System.Diagnostics.Contracts.PureAttribute. This is used by the code contract checking to ensure that the call will produce no visible state changes, and therefore will not require re-checking the object's state.
Currently, to get the functionality of both of these tools, methods need to be annotated with attribute from each. Worse, because they both share the same type name, you need to qualify each attribute.
[Pure]
[Jetbrains.Annotations.Pure]
public bool isFinished() {
...
To avoid this situation, there should be three approaches:
Are any of these possible?
Use of the PureAttribute Class indicates that a type or method is functionally "pure", i.e. it does not make any visible state changes. The idea was that Pure and other attributes/functions in the System.
Code contracts provide a way to specify preconditions, postconditions, and object invariants in . NET Framework code. Preconditions are requirements that must be met when entering a method or property. Postconditions describe expectations at the time the method or property code exits.
Posted by: Pravinkumar Dabade , on 8/27/2015, in Category C# Views: 83700. Abstract: Code Contracts API includes classes for static and runtime checks of code and allows you to define preconditions, postconditions, and invariants within a method. The Contracts class is found in the System.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With