确保的 CodeContract 问题
我得到以下代码:
protected virtual string FormatException(Exception exception, int intendation)
{
Contract.Requires(intendation >= 0);
Contract.Requires<ArgumentNullException>(exception != null);
Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()));
var msg = exception.ToString().Replace("\r\n", "\r\n".PadRight(intendation, '\t'));
string text = string.Format("\r\n******* EXCEPTION ********\r\n\t{0}", msg);
return text;
}
它给了我
警告 19 CodeContracts:确保未经验证:!String.IsNullOrEmpty(Contract.Result())
为什么?
I got the following code:
protected virtual string FormatException(Exception exception, int intendation)
{
Contract.Requires(intendation >= 0);
Contract.Requires<ArgumentNullException>(exception != null);
Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()));
var msg = exception.ToString().Replace("\r\n", "\r\n".PadRight(intendation, '\t'));
string text = string.Format("\r\n******* EXCEPTION ********\r\n\t{0}", msg);
return text;
}
It gives me
Warning 19 CodeContracts: ensures unproven: !String.IsNullOrEmpty(Contract.Result())
Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道
String.Format()
函数是否有任何约定,但它只能承诺result != null
,即空字符串是有效结果。我检查过: String.Format() 仅确保结果!= null
您可以通过插入
Assume()
来简单地修复它:但我会认真重新考虑制作结果此处的合同部分不为空。这对来电者真的很重要吗?
I don't know if the
String.Format()
function has any contracts but it could only promise that theresult != null
, an empty string is a valid result.I checked: String.Format() only ensures result != null
You can simply fix it by inserting an
Assume()
:But I would seriously reconsider making result is not empty part of your contract here. Does it really matter to the callers?