代码合同+代码分析

Dan*_*rth 20 .net c# code-contracts visual-studio visual-studio-2012

我想在我的代码库中开始使用代码契约.

我已经使用了代码分析,启用了所有规则,并且目标是零警告.

但是,在使用时,Contract.Requires(parameter != null)我收到代码分析的警告,即CA1062:

CA1062:Microsoft.Design:在外部可见的方法"富",用它之前验证参数"参数".

这很不幸,我不想禁用该规则,因为我发现它很有用.但我也不想压制它的每一个错误发生.

有解决方案吗?

Dan*_*rth 14

要解决此问题,需要执行以下步骤:

  1. 禁用代码分析中的CA1062以消除代码分析中的警告.目前无法使代码分析理解Contract.Requires.
  2. 在项目的"代码约定"窗格中启用"执行静态合同检查".
  3. 启用"隐式非空义务"
  4. 将警告级别设置为"hi"(重要的是,这就是我所缺少的!)

步骤1取消CA警告,步骤2到4启用代码合同中的警告,该警告至少相当于.


Pie*_*ens 4

从框架版本 4.5.2(甚至可能是 4.5)开始,可以向代码分析告知代码合约正在执行的合约。首先创建以下扩展方法和标记属性

  using System;
  using System.Diagnostics;
  using System.Diagnostics.CodeAnalysis;
  using System.Diagnostics.Contracts;

  /// <summary>Extension methods to enhance Code Contracts and integration with Code Analysis.</summary>
  public static class ContractExtensions {
#if RUNTIME_NULL_CHECKS
    /// <summary>Throws <c>ArgumentNullException{name}</c> if <c>value</c> is null.</summary>
    /// <param name="value">Value to be tested.</param>
    /// <param name="name">Name of the parameter being tested, for use in the exception thrown.</param>
    [ContractArgumentValidator]  // Requires Assemble Mode = Custom Parameter Validation
    public static void ContractedNotNull<T>([ValidatedNotNull]this T value, string name) where T : class {
      if (value == null) throw new ArgumentNullException(name);
      Contract.EndContractBlock();
    }
#else
    /// <summary>Throws <c>ContractException{name}</c> if <c>value</c> is null.</summary>
    /// <param name="value">Value to be tested.</param>
    /// <param name="name">Name of the parameter being tested, for use in the exception thrown.</param>
    [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value")]
    [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "name")]
    [ContractAbbreviator] // Requires Assemble Mode = Standard Contract Requires
    public static void ContractedNotNull<T>([ValidatedNotNull]this T value, string name) where T : class {
      Contract.Requires(value != null,name);
    }
#endif
  }

/// <summary>Decorator for an incoming parameter that is contractually enforced as NotNull.</summary>
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class ValidatedNotNullAttribute : global::System.Attribute {}
Run Code Online (Sandbox Code Playgroud)

现在将您的条目空测试转换为以下格式:

/// <summary>IForEachable2{TItem} implementation</summary>
public   void  ForEach(FastIteratorFunctor<TItem> functor) {
  functor.ContractedNotNull("functor"); // for Code Analysis

  TItem[] array = _array;
  for (int i = 0; i < array.Length; i++)    functor.Invoke(array[i]);
}
Run Code Online (Sandbox Code Playgroud)

方法名称ContractedNotNull和编译开关RUNTIME_NULL_CHECKS当然可以更改为适合您的命名风格的任何名称。

这是告诉我这种技术的原始博客,我对其进行了稍微改进;非常感谢 Terje Sandstrom 发表他的研究成果。

Rico Suter 在此通过使用附加属性对此进行了扩展以便调试器和内联器也更加智能: