代码合约:如何抑制这种“需要未经证实”的情况警告?
我有一段代码,cccheck
告诉我 Requires()
未经验证,我应该添加 !string.IsNullOrWhitespace(...)
。当我调用我自己在 .Net 3.5 时代编写的扩展方法时,已经检查了该条件:
public static bool IsEmpty(this string s)
{
if (s == null) return true;
if (s.Length == 0) return true;
for (int i = 0; i < s.Length; i++)
if (!char.IsWhitespace(s[i]))
return false;
return true;
}
public static bool IsNotEmpty(this string s)
{
return !IsEmpty(s);
}
我的代码已经要求 value
为 IsNotEmpty
:
Contract.Requires(value.IsNotEmpty(), "The parameter 'value' cannot be null or empty.");
如何我可以告诉 cccheck
(以及代码契约框架的其余部分)IsNotEmpty()
已经检查 !string.IsNullOrWhitespace(...)
?
I have a piece of code for which cccheck
tells me that a Requires()
is unproven and that I should add a !string.IsNullOrWhitespace(...)
. That condition is already checked for as I call my own extension method that I have written back in the days of .Net 3.5:
public static bool IsEmpty(this string s)
{
if (s == null) return true;
if (s.Length == 0) return true;
for (int i = 0; i < s.Length; i++)
if (!char.IsWhitespace(s[i]))
return false;
return true;
}
public static bool IsNotEmpty(this string s)
{
return !IsEmpty(s);
}
My code already requires that value
is IsNotEmpty
:
Contract.Requires(value.IsNotEmpty(), "The parameter 'value' cannot be null or empty.");
How can I tell cccheck
(and the rest of the Code Contracts framework) that IsNotEmpty()
already checks for !string.IsNullOrWhitespace(...)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
Contract.Ensures(Contract.Result() == !string.IsNullOrWhitespace(s))
编辑:
是的,我意识到这会导致“确保未经证实”,当我发布它时,我希望找到有时间更彻底地回答。如果您可以忍受扔掉旧代码,则可以使用一种(有点微不足道的)方法来修复它:
Try
Contract.Ensures(Contract.Result() == !string.IsNullOrWhitespace(s))
EDIT:
Yes, I realized this would cause "ensures unproven" when I posted it and I was hoping to find some time to answer more thoroughly. One (somewhat trivial) way to fix it, if you can stand throwing away your old code: