Warning "propertyAccessor != null" when using Expression<T> with generic base types


  • Hi,

    Using v 1.4.30903.0 and given the following interfaces:

    public interface IBaseInterface<TId>
    	string BaseProp { get; }
    public interface IDerivedInterface : IBaseInterface<long>
    	string DerivedProp { get; }

    And a usage like:

    Expression<Func<IDerivedInterface, object>> expression = x => x.BaseProp;

    I receive a warning: "CodeContracts: requires unproven: propertyAccessor != null"

    If I remove the generic parameter (TId) above, the warning goes away. Is this a known issue?

    I saw a similar issue here:

    But there was no specific mention of a generic parameter triggering the issue and it was also supposed to be resolved anyway





    Friday, October 29, 2010 11:46 AM


All replies

  • I'm seeing the same issue.
    Thursday, November 10, 2011 2:10 AM
  • I also experience this issue, but not with a generic interface (code contracts 1.4.40602)

    public abstract class ObservableObject : DispatcherObject, INotifyPropertyChanged
            protected void NotifyPropertyChanged<T>(Expression<Func<T>> property)
                Contract.Requires(property != null);
                // omitted for brevity

    Any reference to this in a derived class, e.g.

    NotifyPropertyChanged(() => this.SomeSimpleProperty);

    Will cause code contracts to raise a warning:

    Warning 540 CodeContracts: requires unproven: propertyAccessor != null

    It's quite an annoying one as this function is used all over our code and really don't want to have to suppress hundreds of calls only to later reinstate them.

    Is there any confirmation of this as a defect, or a timescale?



    Thursday, December 08, 2011 11:23 AM
  • I would just like to find a way to suppress it. I asked a StackOverflow question about how to do that.

    With the -outputwarnmasks flag in "Extra Static Checker Options" (on the Code Contracts tab of the project), I can see:

            add the attribute: [SuppressMessage("Microsoft.Contracts", "RequiresAtCall-propertyAccessor != null")] to the method System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression,System.Reflection.MethodInfo)

    But I obviously can't put the attribute there because that's in the Framework itself. If there were some other place to put that, I'd love to know.

    • Edited by ranomore Friday, December 23, 2011 6:16 PM
    Friday, December 23, 2011 6:15 PM
  • This STILL is a problem with the latest release (1.4.51019.0)!

    An official reaction to this would be most welcome.

    I am seeing it in my FluentNhibernate auto-mapping overrides:

        public class VatRateOverride : IAutoMappingOverride<VatRate>
            public void Override(AutoMapping<VatRate> mapping)
                Contract.Assume(mapping != null); 
                mapping.Id(x => x.Id).Column("VATVALUES_ID"); // <-- Here

    Thursday, November 08, 2012 5:43 PM
  • Bump.

    Please don't ignore this issue. It is a real show-stopper.

    Sunday, December 16, 2012 3:19 PM
  • Can we please have some official feedback here? 1.4.60228.0 didn't fix this either.

    At least I finally found a way to disable this warning. I am not sure if this is new in the latest release or not, but SuppressMessage actually does work. Putting the following attributes on the Override method I posted above gets rid of the warnings I get.

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Contracts""Nonnull"Justification = "Guaranteed to be not null.")]
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Contracts""Requires"Justification = "Bug in static checker")]

    Thursday, February 21, 2013 2:12 PM
  • Sorry for the long silence. We will take a look at

    what is causing this.

    Cheers, -MaF (Manuel Fahndrich)

    Monday, March 18, 2013 2:41 AM
  • Okay, I finally fixed this. It will be in the next release.

    Cheers, -MaF (Manuel Fahndrich)

    Monday, March 18, 2013 7:49 PM
  • Thanks MaF,

    I never posted in this thread, but was also affected by this issue, will be nice to have a clear output window.


    Tuesday, March 19, 2013 9:06 AM
  • Thanks! :)
    Tuesday, March 19, 2013 11:32 AM