5

I would like to implement the following suggestion from CodeContracts:

CodeContracts: MyModule: Method MyModule.MyClass.MyMethod: 
To mask *all* warnings issued like the precondition add the attribute: 

[SuppressMessage("Microsoft.Contracts", "RequiresAtCall-propertyAccessor != null")] 

to the method 

System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression,System.Reflection.MethodInfo)

It feels like I should be able to use SupressMessage with the Target attribute to make this happen. However, because this is a Framework method, I'm not sure.

//doesn't work
[module: SuppressMessage("Microsoft.Contracts", "RequiresAtCall-propertyAccessor != null", Scope = "Member", Target = "System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression,System.Reflection.MethodInfo)", Justification = "This isn't covered by Linq Contracts yet.")]

How can I globally suppress this warning, so I don't have to baseline or suppress all of the callsite warnings?

EDIT: The specific usage that requires this measure is:

void mymethod()
{
    var myObserver = new PropertyObserver<MyViewModel>();
    //this line throws the error, within the n => n.Value expression
    myObserver.RegisterHandler(n => n.Value, OnValueChanged);
}

public class PropertyObserver<TPropertySource> where TPropertySource : INotifyPropertyChanged
{
    public PropertyObserver<TPropertySource> RegisterHandler(
        Expression<Func<TPropertySource, object>> expression,
        Action<TPropertySource> handler)
    {
        //what this does is irrelevant; the violation  occurs in the method call
    }
}

//n => n.Value decompiles to the following
public static MemberExpression Property (Expression expression, MethodInfo   propertyAccessor)
{
    //and this line is the message I want to suppress, but it's in the .NET framework.
    ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor");
    ValidateMethodInfo(propertyAccessor);
    return Property (expression, GetProperty(propertyAccessor));
}
6
  • Is there a reason that you're not using Contract.Assume? Just too many occurrences?
    – porges
    Jan 12, 2012 at 22:11
  • We've tried to stay away from Contract.Assume generally, but yes, there are quite a few occurrences, and we continue to add more. Jan 12, 2012 at 22:14
  • I'm guessing that the problem then is that the various ways to acquire Expressions/MethodInfos don't Ensure that the result is non-null. Have you considered using some wrapper methods such as those given in: social.msdn.microsoft.com/Forums/en-NZ/codecontracts/thread/… ? That way you only need to Assume in one place, so your Assume usage is minimized.
    – porges
    Jan 12, 2012 at 22:22
  • That's a great idea, but the problem is the contract violation occurs in the call to the method, not within the method (because the method expects to be passed an expression). I'll add an example. Jan 12, 2012 at 22:47
  • In your real code, are you building up the n=>n.Value by expression trees? Your error mentions Expression.Property but that doesn't appear in the sample.
    – porges
    Jan 12, 2012 at 23:11

3 Answers 3

3
+50

After some more investigation with ranomore, there seems to be a bug in Code Contracts.

The class being accessed via n => n.Value has a generic T Value property. If the class is changed to a non-generic class (with object Value) the warning disappears. (A generic class with object Value also gives the warning).

Of course, this doesn't answer the original question, but I don't think it's possible to do that.

1
  • It must not be, or else someone would have answered it already. ;) Jan 13, 2012 at 0:14
0
  1. Add GlobalSuppressions.cs to root of project.

  2. Add your [module...

  3. Replace the word module with assembly.

Does that work?

1
  • I had high hopes, but alas, it does not work. I removed the Scope = "Member" too, just in case it mattered. Jan 10, 2012 at 0:06
0

It actually works. You can add a SupressMessageAttribute to the method that contains the expression. Just don't use RequiresAtCall. Instead, use Requires:

[SuppressMessage("Microsoft.Contracts", "Requires",
                 Justification = "Bug in static checker")]
public void Override(AutoMapping<Bookings> mapping)
{
    Contract.Assume(mapping != null);

    mapping.Id(x => x.Id);
}

The obvious downside is that you will have to plaster your code with these...

3
  • Isn't that pretty dangerous though? That's all requires you're suppressing there. I was hoping to suppress just the one message. Feb 22, 2013 at 22:41
  • @ranomore: Yes, you also can suppress just this one message. Instead of Requires you have to use Requires-11-10 where the numbers specify the exact location of the warning. You get those numbers from Code Contracts when you add the following to the additional commands for the static checker on the Code Contracts project properties page: -outputwarnmask. For every warning it will output (in the Output window, not in the error list) a message that gives you the complete SupressMessage attribute you can use to suppress that specific warning. Feb 22, 2013 at 22:49
  • I didn't use this for my NHibernate mapping classes, because I was too lazy :) Feb 22, 2013 at 22:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.