6

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;
    }
}
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443

2 Answers2

1

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

Based on this question, I'd say you need to specify what happens if the cast fails.

Try return this as TQuery since the as operator will return null if the cast fails, whereas the explicit cast will throw an error.

Tyler Lee
  • 2,736
  • 13
  • 24