After reading over some of the other responses to this type of question I am still left with the warning. In this snippet I pull a UserActivation from my database. By this point there will always be at least one UserActivation. If there is more then one something has gone all pear shaped... I followed some other instructions on how to squelch the warning regarding an unproven source but to no avail.The warning is: Warning 83 CodeContracts: requires unproven: source != null on line 161, see method below for the specific line.
This is the method
private static UserActivation GetUserActivation(Guid userId)
{
UserActivations userActivations = UserActivation.GetUserActivationsByUser(userId: userId);
Contract.Assume(userActivations != null);
if (userActivations.Count() > 1) // Line Number 161
throw new Exception("More then one user action found in database");
return userActivations[0];
}
I am using CC version 1.4.40602.0, As requested here is the UserActivations declaration.
public class UserActivations : BusinessListBase<UserActivation>
{
#region Constructors
internal UserActivations()
{
}
internal UserActivations(IList<UserActivation> list)
: base(list)
{
}
internal UserActivations(IEnumerable<UserActivation> list)
: base(list)
{
}
And here is the GetUserActivationByUser method
public static UserActivations GetUserActivationsByUser(User user = null, Guid userId = new Guid())
{
Contract.Requires(user != null || userId != null, "Either user or userId must have a value");
Contract.Ensures(Contract.Result<UserActivations>() != null);
Guid id = new Guid();
if (user != null)
id = user.Id;
else
id = userId;
return new UserActivations(StorageManager.SelectAll(
Criteria.And(
Criteria.EqualTo("UserId", id),
Criteria.EqualTo("Deleted", false))));
}
The original code was:
Public static UserActivations GetUserActivationsByUser(User user = null, Guid userId = new Guid())
{
Guid id = new Guid();
if (user != null)
id = user.Id;
else
if (userId != Guid.Empty)
id = userId;
else
throw new Exception("Either user or userId must have a value");
UserActivations uas = new UserActivations(StorageManager.SelectAll(
Criteria.And(
Criteria.EqualTo("UserId", id),
Criteria.EqualTo("Deleted", false))));
Contract.Ensures(Contract.Result<UserActivations>() != null);
return uas;
}
userActivations[0];would trip a (different) warning. What does theCount()signature look like? And isn't there a Count property? - Henk Holtermanint System.Linq.Enumerable.Count<T>(IEnumerable<T> source). I believe it hasrequires(source != null). Yet I don't understand why it cannot be proven: there is an assumption... - Pavel GatilovUserActivationslooks. - Henk HoltermanContract.Ensuresmust be put in the contract block - before any other code. Perhaps this is the reason of the verifier's failure. In my case your code won't even compile 8) - Pavel Gatilov