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

筅森魡賤 提交于 2019-12-19 05:20:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!