locked
Code Contracts & Static Code Analysis(FxCop) RRS feed

  • Question

  • Hello,

    I want to improve the quality of my code and fix SCA warnings. One of the warnings I'm trying to solve are CA1062 warnings and I would like to do this with Code Contracts. Unfortunately writing preconditions like:

    Contract.Requires<ArgumentNullException>(txt != null@"txt");
    

    Does not solve this SCA warning

    warning CA1062: Microsoft.Design : In externally visible method 'XXX', validate parameter 'txt' before using it.

    Is there any update if this is addressed in Visual Studio 2015, or that this will be addressed in the future? By looking at different threads this problem seems to be active for quite some years now and its quite disappointing that this hasn't been addressed.

    Regards Niek

      


    Wednesday, July 15, 2015 6:38 AM

All replies

  • Originally posted  (by myself) on StackOverflow here: Code Contracts + Code Analysis

    As of Version 4.5.2 of the framework (possibly even 4.5) it is possible to tell Code Analysis about the contracts being enforced by Code Contracts. First create the following extension method and marker attribute

          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 {}

    and now convert your entry null-tests to the following format:

        /// <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]);
        }


    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    We are here to learn, to share knowledge, and to earn points; all in about equal measure.


    Tuesday, August 4, 2015 4:08 AM