如何暂时禁用“返回值可能未定义”警告?
我想在代码中禁用特定警告(W1035),因为我认为编译器对此警告是错误的:
function TfrmNagScreen.Run: TOption;
begin
if ShowModal = mrOk then
Result := TOption(rdgAction.EditValue)
else
Abort
end;
结果不可能是未定义的,因为 Abort
抛出 EAbort
。
我尝试过:
{$WARN 1035 Off}
:显然这仅适用于某些特定错误(请参阅文档){$W-1035}
:什么都不做
我知道我可以在项目选项中全局关闭警告,或者使用{$WARNINGS OFF }
,但这不是这里的目的。
编辑:我现在已将其质量控制为 #89744。
I want to disable a specific warning (W1035) in my code, since I think that the compiler is wrong about this warning:
function TfrmNagScreen.Run: TOption;
begin
if ShowModal = mrOk then
Result := TOption(rdgAction.EditValue)
else
Abort
end;
There is no way the result could be undefined, since Abort
throws EAbort
.
I tried:
{$WARN 1035 Off}
: Apparently this only works for some specific errors (see Documentation){$W-1035}
: Does nothing at all
I know I can switch off the warning globally in the project options, or using {$WARNINGS OFF}
, but that is not what is intended here.
Edit: I have QC'ed this now as #89744.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法全局禁用此警告,但可以使用
{$WARN NO_RETVAL OFF}
在本地禁用此警告。you can't disable this warning globally, but you can use the
{$WARN NO_RETVAL OFF}
to disable locally the warning.我目前没有可用的 Delphi 编译器,但重新排列代码以删除
if..else
可能会使警告消失:另请参阅 如何禁用 Delphi 中有关“返回值……可能未定义”的警告?。
I don't have a Delphi compiler available at the moment, but rearranging the code to remove the
if..else
might make the warning go away:See also How to disable a warning in Delphi about “return value … might be undefined”?.
您可以使用一个巧妙的技巧来愚弄编译器。如下定义库函数:
然后您可以将函数编写为:
编译器认为您已写入 Result,因为它是 var 参数,并且它会停止发出警告。
You can use a neat trick to fool the compiler. Define a library function as so:
You can then write your function as:
The compiler thinks you've written to Result since it's a var parameter and it stops bleating.