ReSharper claims my expression is always true? Is this correct or is it a mistake?

ReSharper claims my null check on the serviceProvider is always correct, which is odd. I figured out that serviceProvider can be null very well. 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 app and the following code gives a warning too (from ReSharper) Possible null assignment to entity marked with 'NotNull' attribute.

var binding = new CustomBinding();
binding.ProvideValue(null);

      

+3


source to share


2 answers


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

You can pass null for the serviceProvider, but only if this TypeExtension instance was set with an initial true type in the constructor and not typeName. Otherwise, this markup extension implementation depends on services based on the passed service provider. It shouldn't be null. The service provider is expected to provide services for the IXamlTypeResolver.



Also you can look at When Shared Parameter is Never Null for more information on this.

Apparently, if I'm right, this method has a [NotNull] contract for its parameter, because some implementations throw errors on null arguments.

+3


source


Since ProvideValue is a function that needs an IServiceProvider, Resharper correctly assumes that if serviceProvider is NULL, it is checked before calling ProvideValue, so the expression is always true.



-4


source







All Articles