CodeContracts:布尔条件计算结果为恒定值,为什么呢? [英] CodeContracts: Boolean condition evaluates to a constant value, why?

查看:203
本文介绍了CodeContracts:布尔条件计算结果为恒定值,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个警告,但不能找出问题...

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

CodeContracts:警告:布尔条件D1 .Count之间!=
d2.Count始终计算为一个恒定值。如果它(或它的否定)
出现在源代码中,你可能有一些死码或冗余
检查

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

中的代码如下:

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.

推荐答案

这简直是在代码契约的错误。这是很容易炮制的投入,使这种情况真的还是假的。该警告是假的。

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.

从个人的经验,我知道,在CC的错误并不少见。

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

如何固定?由于这是一个错误有动作没有官方/意课程。报告的bug。围绕轻摇代码,直到警告消失(例如,试着的ReferenceEquals 这是更好的风格反正)。抑制警告。这样的事情。

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.

这篇关于CodeContracts:布尔条件计算结果为恒定值,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆