Since this solves the problem and is still not that ugly as one might have thought initially, I'm posting it as an answer (if one has better ideas I'm open for your suggestions though):
static public int Q()
{
var e = Enumerable.Range(0, 100)
.Select(i => i);
Contract.Assume(e.Any());
return e.First();
}
So I should not have to split the whole expression, but the part that the static analyzer was afraid of, and for that part I could assure it that it's "all fine, trust me, I know what I'm doing".
A note:
for some reason neither
Contract.Assert(e.Count() > 0);
or
Contract.Assert(e.Any());
work.
Important: as other people mention, this might be not suitable for all cases, since the additional e.Any()
call would materialize the collection, which may be undesirable in some cases (eg: when it's a LINQ from a 3rd party source).