/ReSharperContractExtensions

Set of extensions for R# that simplifies Design by Contract programming using Code Contracts.

Primary LanguageC#MIT LicenseMIT

R# support for Code Contracts

Are you using Code Contracts library in your project? Do you want to simplify some basic steps, like adding preconditions, postconditions or object invariant? Maybe you hate current Code Contract approach for adding assertions to the abstract classes and interfaces. If you fond of Design by Contract but hate typing, this R# plug-in is what you need.

Download

Currently supported ReSharper versions are 8.0-8.2

This plugin is available for download in ReSharper extensions gallery;

Support for Code Contract Compiler warnings and errors inside the editor

  1. Inconsistent preconditions visibility
  • Warn on Contract.Requires<CustomException>() when CustomException does not have ctor(string) or ctor(string, string) (#4).
  • Warn on Contract.Requires<CustomException>() when CustomException is less visible then the enclosing method (#3).
  • Error when Contract.Requires less visible members in the predicate than the enclosing method.
  1. Errors for malformed method contracts
  • Error for Requires/Ensures after EndContractBlock (#21)
  • Error for Requires/Ensures in the middle of the method (#26, #22)
  • Error when Ensures placed before Requires (#29, #9)
  • Error for calling void-return method in the contract block (#5)
  • Warning for calling non-void return method in the contract block (#25)
  • Error on method calls as a source of contract error message (Contract.Requires(false, GetMessage())
  • Error on non-static internal strings used as a source of contract error message (#2)
  • Error on assignments in the contract block (#9)
  • Error on Assert/Assume calls in the contract block (#6)
  • Error on using Requires/Ensures in the try block (#10)
  • Warning on redundant EndContractBlock (for instance used after Contract.Requires/Ensures).
  1. Postcondition checks
  • Error for inconsistent method return type with Contract.Result<T> (#34)
  1. Other features
  • Generated contract classes would be marked with ExlucdeFromCodeCoverageAttribute if this option is enabled in the plug-in options page. (#31)
  • Warn on redundant Contract.Requires on nullable arguments

Basic use cases

Add preconditions

Add postconditions

Add object invariants for fields or properties

Add contract class for interface (the same feature exists for abstract classes)

“Combo” actions for abstract class (the same feature exists for interfaces)

Convert null-check preconditions from if-throw to Contract.Requires (new in v.0.7.5)

Convert range check from if-throw to Contract.Requires (new in v.0.7.5)

Add precondition check for enum argument (new in v.0.7.5)

Add postcondition check for enum argument (new in v.0.7.5)

Add preconditions for all arguments (new in v.0.7)

Check string arguments with string.IsNullOrEmpty (new in v.0.7)

(Note, this feature should be turned on at plug-in settings page, available at R# Options)

What next for you?

Download sources and play with them or Download this plug-in via R# Gallery - https://resharper-plugins.jetbrains.com/packages/ReSharper.ContractExtensions/0.7.51

What next for me?

  1. Add support for R# 8.0 and 8.1 (Done)

  2. Add quick fixes and code analysis rules that will show some common errors in the IDE directly, instead of waiting for compilation time.

  3. Add support for Intelli Sense and show contract in the tool tips (I know about “Code Contracts Editor Extension” but it never worked for me). P.S. This tool was written with the previous version of this extension. So, if you want to look at the sample code with a lot of contracts, this source of the project could be useful as well.

P.S. This tool was written with the previous version of this extension. So, if you want to look at the sample code with a lot of contracts, this source of the project could be useful as well.