Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeContracts: Boolean condition evaluates to a constant value, why?

I'm getting this warning but can't figure out the problem...

CodeContracts: warning: The Boolean condition d1.Count != d2.Count always evaluates to a constant value. If it (or its negation) appear in the source code, you may have some dead code or redundant check

The code is as follows:

public static bool DictionaryEquals<TKey, TValue>(IDictionary<TKey, TValue> d1, IDictionary<TKey, TValue> d2)
{
    if (d1 == d2) return true;
    if (d1 == null || d2 == null) return false;
    if (d1.Count != d2.Count) return false; // <-- warning here

    // Equality check goes here

    return true;
}

The // Equality check goes here part can be as is, or replaced by a proper implementation and I still get the same warning.

like image 722
antak Avatar asked Feb 11 '15 11:02

antak


1 Answers

This is simply a bug in Code Contracts. It is easy to concoct inputs that make this condition true or false. The warning is bogus.

From personal experience I know that bugs in CC are not rare.

How to fix? Since this is a bug there is no official/intended course of action. Report the bug. Jiggle the code around until the warning goes away (for example, try ReferenceEquals which is better style anyway). Suppress the warning. Things like that.

like image 84
usr Avatar answered Nov 04 '22 01:11

usr