Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeContracts: false warning "Possibly unboxing a null reference"

enter image description here

In the image above, you can see a warning from Code Contracts. I don't think this is legit, as this can never be null.

Is this a bug or am I missing something?


This property is a member of the following class:

public class NHibernateIQueryableQueryBase<TEntity, TQuery, TQueryInterface>
    : IQuery<TEntity>, IFluentQueryInterface<TEntity, TQueryInterface>
    where TQuery : NHibernateIQueryableQueryBase<TEntity, TQuery,
                                                 TQueryInterface>,
                   TQueryInterface
    where TQueryInterface : IQuery<TEntity>

Update:
Changing the property to the following still shows the warning - on the line return result;:

public TQueryInterface And
{
    get
    {
        var result = this as TQuery;
        return result;
    }
}
like image 901
Daniel Hilgarth Avatar asked Nov 07 '12 16:11

Daniel Hilgarth


1 Answers

The analysis doesn't understand that this is guaranteed to implement TQuery.
Therefore, it's warning you that you might end up taking a null reference to the interface type, and returning it as a struct that implements that interface:

You need to add : class to the constraints of the TQueryInterface parameter.

like image 162
SLaks Avatar answered Oct 14 '22 00:10

SLaks