Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Contracts rewriter misuse detection #1

Open
sharwell opened this issue Nov 14, 2014 · 0 comments
Open

Code Contracts rewriter misuse detection #1

sharwell opened this issue Nov 14, 2014 · 0 comments

Comments

@sharwell
Copy link
Member

Diagnostic

Certain methods of the Contract class only behave properly when the code contracts rewriter is enabled during project builds. The simplest way to identify that the rewriter is considered is to check the following build property:

<CodeContractsAssemblyMode>1</CodeContractsAssemblyMode>

The proposed analyzer would operate when the rewriter is disabled, and locate uses of the problematic methods. The following methods are always a problem when the rewriter is disabled.

  • Contract.Requires<TException>

In addition, the following methods are a problem only when the CONTRACTS_FULL is defined during the build.

  • Contract.Ensures
  • Contract.EnsuresOnThrow
  • Contract.Invariant
  • Contract.Requires

Code Fix

A code fix probably only needs to be implemented for uses of Contract.Requires<TException>. However, implementing the code fix methods is somewhat complicated, because the rewriter automatically adds verification code not only at the location of the call to Requires<TException>, but also in any method which overrides or implements the method containing that call. The following case is the most common usage:

Contract.Requires<ArgumentNullException>(arg != null, "arg");

This would get translated to the following code, and added to all relevant places:

if (!(arg != null))
    throw new ArgumentNullException("arg");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant