如何暂时禁用“返回值可能未定义”警告?

时间:2010-11-17 12:27:22

标签: delphi delphi-2007 suppress-warnings compiler-directives

我想在我的代码中禁用特定警告(W1035),因为我认为编译器对此警告是错误的:

function TfrmNagScreen.Run: TOption;
begin
  if ShowModal = mrOk then
    Result := TOption(rdgAction.EditValue)
  else
    Abort
end;

由于Abort抛出EAbort,因此无法定义结果。

我试过了:

  • {$WARN 1035 Off}:显然这仅适用于某些特定错误(请参阅Documentation
  • {$W-1035}:什么都不做

我知道我可以在项目选项中全局关闭警告,或使用{$WARNINGS OFF},但这不是此处的目的。

编辑:我现在已将此问题调整为#89744

3 个答案:

答案 0 :(得分:13)

您无法全局禁用此警告,但您可以使用{$WARN NO_RETVAL OFF}在本地禁用警告。

{$WARN NO_RETVAL OFF}
function TfrmNagScreen.Run: TOption;
begin
  if ShowModal = mrOk then
    Result := TOption(rdgAction.EditValue)
  else
    Abort
end;
{$WARN NO_RETVAL ON}

答案 1 :(得分:9)

我目前没有可用的Delphi编译器,但重新排列代码以删除if..else可能会使警告消失:

function TfrmNagScreen.Run: TOption;
begin
  if ShowModal <> mrOk then
    Abort;

  Result := TOption(rdgAction.EditValue);
end;

另见How to disable a warning in Delphi about “return value … might be undefined”?

答案 2 :(得分:1)

您可以使用巧妙的技巧来欺骗编译器。如下定义库函数:

procedure Abort(var X);
begin
  SysUtils.Abort;
end;

然后您可以将您的功能编写为:

if ShowModal = mrOk then
  Result := TOption(rdgAction.EditValue)
else
  Abort(Result)

编译器认为你已写入Result,因为它是一个var参数,它会停止咩咩声。

相关问题