3

ReSharper is claiming that my null check against serviceProvider is always true, which is odd. I figured that serviceProvider very well might be null. Am I wrong here, or is this a ReSharper bug?

public override object ProvideValue(IServiceProvider serviceProvider)
{
    switch (Mode)
    {
        case BindingMode.TwoWay:
            throw new InvalidOperationException("Invalid binding mode.");

        case BindingMode.OneWayToSource:
            throw new InvalidOperationException("Invalid binding mode.");

        case BindingMode.OneWay:
        case BindingMode.OneTime:
            break;

        case BindingMode.Default:
            if (serviceProvider != null) // Expression is always true?? O.o
            {
                // Returns something possibly...
            }

            throw new InvalidOperationException("Invalid binding mode.");

        default:
            throw new InvalidOperationException("Unexpected binding mode.");
    }

    return base.ProvideValue(serviceProvider);
}

Update:

I created a console application, and the following code gives a warning as well (from ReSharper) Possible null assignment to entity marked with 'NotNull' attribute.

var binding = new CustomBinding();
binding.ProvideValue(null);
myermian
  • 31,823
  • 24
  • 123
  • 215

2 Answers2

3

Taken from the documentation at http://msdn.microsoft.com/en-us/library/system.windows.markup.typeextension.providevalue%28v=vs.110%29.aspx

You can pass null for serviceProvider, but only if this TypeExtension instance was established with an initial true type in the constructor rather than a typeName. Otherwise, this markup extension implementation relies on services based on the passed serviceProvider. It must not be null. The serviceProvider is expected to provide a service for IXamlTypeResolver.

Also you can view When can a generic parameter never be null for more information about this.

Apparently, if i am correct, this method has a contract [NotNull] for its parameter because some implementations throw errors upon null arguments.

Community
  • 1
  • 1
austin wernli
  • 1,801
  • 12
  • 15
-4

Since ProvideValue is a function that needs IServiceProvider Resharper correctly assumes that if the serviceProvider is null is being checked prior to calling the ProvideValue function thus the espression is always true

elasticrash
  • 1,181
  • 1
  • 12
  • 30
  • Resharper makes no such assumptions, someone could call the method with `ProvideValue(null);` – Ben Robinson Dec 23 '14 at 16:50
  • I've seen it quite frequently in older versions so Ill stick to my answer in 9.0 though that particular code doesn't evaluate as always true. Kill me Bad judgement :) – elasticrash Dec 23 '14 at 16:54