none
How can I suppress a compiler warning "CC1036: Detected call to method 'Microsoft.Crm.Sdk.PropertyBagCollection.Contains(System.String)' without [Pure] in contracts of method"

    Question

  • I have full runtime checking enabled. And a precondition like this
    Contract.Requires(context.InputParameters.Properties.Contains(ParameterName.Target));
    

    Static checking is NOT enabled.  The compiler gives me the following warning.  How can I suppress the warning?

    C:\Focus\CRM\Main\Bop.Crm.BusinessLogic\Plugins\IncidentPluginOperations.cs(14,13): warning CC1036: Detected call to method 'Microsoft.Crm.Sdk.PropertyBagCollection.Contains(System.String)' without [Pure] in contracts of method 'Bop.Crm.BusinessLogic.Plugins.IncidentPluginOperations.OnPreUpdate(Microsoft.Crm.Sdk.IPluginExecutionContext)'.
    Best Regards, David K Allen http://codecontracts.info Minneapolis, Minnesota, USA
    Friday, March 12, 2010 4:43 PM

Answers

  • Yes, we need to make our checker sensitive to the disable pragma... Sigh. We currently don't have that implemented, but I've added it to our work list.

    In the meantime, a workaround is to do this (and I'm *not* saying this is nice, so don't slag me for it):

    [Pure]
    bool MethodJustToGetAroundProblem(x, y){
      return x.InputParameters.Properties.Contains(y);
    }
    
    ... ExistingMethod(...) {
      Contract.Requires(MethodJustToGetAroundProblem(context, ParameterName.Target));
    }
    
    Friday, March 12, 2010 10:54 PM

All replies

  • Hi David,

    It looks like PropertyBagCollection doesn't implement IList or ICollection<T>, both of which have a Contains method that is marked Pure.

    http://msdn.microsoft.com/en-us/library/bb959676.aspx 

    Anyway, I don't know if it's possible to suppress this warning - you may just have to refrain from using this non-Pure method in your contracts.

    - Dave
    http://davesexton.com/blog
    Friday, March 12, 2010 10:07 PM
  • Hi David,

    Just to be clear, I believe this is not a compiler warning but is a CC rewriter warning.  If it's a compiler warning, then the following should work:

    #pragma warning disable 1036
    Contract.Requires(context.InputParameters.Properties.Contains(ParameterName.Target));
    #pragma warning restore 1036

    http://msdn.microsoft.com/en-us/library/441722ys.aspx

    But it's doubtful.

    - Dave
    http://davesexton.com/blog
    Friday, March 12, 2010 10:10 PM
  • Yes, we need to make our checker sensitive to the disable pragma... Sigh. We currently don't have that implemented, but I've added it to our work list.

    In the meantime, a workaround is to do this (and I'm *not* saying this is nice, so don't slag me for it):

    [Pure]
    bool MethodJustToGetAroundProblem(x, y){
      return x.InputParameters.Properties.Contains(y);
    }
    
    ... ExistingMethod(...) {
      Contract.Requires(MethodJustToGetAroundProblem(context, ParameterName.Target));
    }
    
    Friday, March 12, 2010 10:54 PM
  • Actually, this is fine. It's easy to do and easy to read. I can't believe I didn't think of it.  I'm sure I was fixated on something more "clever."
    Best Regards, David K Allen http://codecontracts.info Minneapolis, Minnesota, USA
    Thursday, March 18, 2010 12:49 AM
  • I'm getting this with:

          Contract.Requires(System.IO.File.Exists(fileNameArg), "The file name must exist");

    Should I just change this to an Contract.Assert or is this sort of thing supposed to work?

     

    Robert

    Thursday, October 21, 2010 2:25 AM
  • "supposed" is a big word... I've just added the [Pure] marking to File.Exists, but until that shows up in a release, you'll have to do the same sort of trick as shown in the answer to this thread or else change it to an assert.
    Mike Barnett
    Thursday, December 02, 2010 12:50 AM