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

发布于 2024-10-03 07:12:48 字数 652 浏览 2 评论 0原文

我想在代码中禁用特定警告(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街角迷惘 2024-10-10 07:12:48

您无法全局禁用此警告,但可以使用 {$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}

you can't disable this warning globally, but you can use the {$WARN NO_RETVAL OFF} to disable locally the warning.

{$WARN NO_RETVAL OFF}
function TfrmNagScreen.Run: TOption;
begin
  if ShowModal = mrOk then
    Result := TOption(rdgAction.EditValue)
  else
    Abort
end;
{$WARN NO_RETVAL ON}
请爱~陌生人 2024-10-10 07:12:48

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

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

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

另请参阅 如何禁用 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:

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

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

See also How to disable a warning in Delphi about “return value … might be undefined”?.

北方。的韩爷 2024-10-10 07:12:48

您可以使用一个巧妙的技巧来愚弄编译器。如下定义库函数:

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

然后您可以将函数编写为:

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

编译器认为您已写入 Result,因为它是 var 参数,并且它会停止发出警告。

You can use a neat trick to fool the compiler. Define a library function as so:

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

You can then write your function as:

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

The compiler thinks you've written to Result since it's a var parameter and it stops bleating.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文