问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/28452850/codecontracts-boolean-condition-evaluates-to-a-constant-value-why